/**
  * Register any application services.
  *
  * @return void
  */
 public function register()
 {
     $this->app->singleton(MailiumOauthClient::class, function ($app) {
         $oauthClient = new MailiumOauthClient(null, true);
         $oauthClient->setClientID(config('mailium-oauth.client_id'));
         $oauthClient->setClientSecret(config('mailium-oauth.client_secret'));
         $oauthClient->setScopes(config('mailium-oauth.required_scopes'));
         $oauthClient->setTokenStoreCallbackFunction(config('mailium-oauth.model') . '::saveOauthToken');
         $oauthClient->setRedirectUri(config('mailium-oauth.redirect_uri'));
         $oauthClient->setAppType(config('mailium-oauth.app_type'));
         return $oauthClient;
     });
 }
Пример #2
0
// function to remove the token
function removeToken($resourceOwner)
{
    unset($_SESSION['token']);
}
// Start Session
session_start();
// Create Oauth client
$oauthClient = new MailiumOauthClient();
$oauthClient->setClientID(getenv('MAILIUM_APP_CLIENT_ID'));
$oauthClient->setClientSecret(getenv('MAILIUM_APP_CLIENT_SECRET'));
$oauthClient->setRedirectUri("http://" . $_SERVER['HTTP_HOST'] . "/oauthcallback.php");
$oauthClient->addScope(MailiumOauthClient::SCOPE_BASIC);
$oauthClient->addScope(MailiumOauthClient::SCOPE_CAMPAIGN_READ);
$oauthClient->addScope(MailiumOauthClient::SCOPE_SUBSCRIBER_LIST_READ);
$oauthClient->setTokenStoreCallbackFunction("storeToken");
print "<h3>Session:</h3>";
print "<pre>";
print_r($_SESSION);
print "</pre>";
print "<hr>";
// Token is stored in the session, if we don't have it we should redirect to authorization URL to get the token
if (!isset($_SESSION['token'])) {
    $state = $oauthClient->generateState();
    unset($_SESSION['state']);
    $_SESSION['state'] = $oauthClient->getState();
    $authorizationUrl = $oauthClient->createAuthorizationUrl();
    print "<h3>Authorization URL To Redirect:</h3>";
    print "<pre>";
    print_r($authorizationUrl);
    print "</pre>";
Пример #3
0
require __DIR__ . '/../vendor/autoload.php';
use MailiumOauthClient\MailiumOauthClient;
function storeToken($resourceOwner, $token)
{
    $_SESSION['token'] = $token;
}
session_start();
$oauthClient = new MailiumOauthClient();
$oauthClient->setClientID(getenv('MAILIUM_APP_CLIENT_ID'));
$oauthClient->setClientSecret(getenv('MAILIUM_APP_CLIENT_SECRET'));
$oauthClient->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauthcallback.php', true);
$oauthClient->addScope(MailiumOauthClient::SCOPE_BASIC);
$oauthClient->addScope(MailiumOauthClient::SCOPE_CAMPAIGN_READ);
$oauthClient->addScope(MailiumOauthClient::SCOPE_SUBSCRIBER_LIST_READ);
$oauthClient->setTokenStoreCallbackFunction('storeToken');
if (!isset($_GET['code'])) {
    $authorizationUrl = $oauthClient->createAuthorizationUrl();
    // Generate JavaScript Code for redirection
    print $oauthClient->createEmbeddedAppHtmlForRedirection($authorizationUrl);
} else {
    print_r($_GET);
    $authorizationCode = $_GET['code'];
    $authorizationState = $_GET['state'];
    if ($_SESSION['state'] !== $authorizationState) {
        print "<h3>State is not same!!!</h3>";
        exit;
    }
    $oauthClient->authorize($authorizationCode);
    unset($_GET['state']);
    // Redirect to Index