Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confused about $auth->accessTokenUpdated() #32

Closed
davout1806 opened this issue Apr 25, 2016 · 2 comments
Closed

Confused about $auth->accessTokenUpdated() #32

davout1806 opened this issue Apr 25, 2016 · 2 comments

Comments

@davout1806
Copy link

davout1806 commented Apr 25, 2016

I'm a little confused regarding the OAuth authentication, specifically the call $auth->accessTokenUpdated(). It never seems to return true. As a result I have make a call to $auth->getAccessTokenData(); and store the results before the call to accessTokenUpdated. Am i missing something? Thanks

My sample code based on sample from https://github.com/mautic/api-library:


session_name("oauthtester");
session_start();

require dirname(__DIR__).'/vendor/autoload.php';
require 'settings.php';

use Mautic\Auth\ApiAuth;
use Mautic\MauticApi;

// ApiAuth::initiate will accept an array of OAuth settings
$settings = array(
  'baseUrl' => $baseUrl,       // Base URL of the Mautic instance
  'version' => $version,  // Version of the OAuth can be OAuth2 or OAuth1a. OAuth2 is the default value.
  'clientKey' => $clientKey,       // Client/Consumer key from Mautic
  'clientSecret' => $clientSecret,       // Client/Consumer secret key from Mautic
  'callback' => $callback        // Redirect URI/Callback URI for this script
);

if (isset($_GET['oauth_token']) && isset($_GET['oauth_verifier'])) {
  $settings['accessToken'] = $_GET['oauth_token'];
  $settings['accessTokenSecret'] = $_GET['oauth_verifier'];
}

// Initiate the auth object
$auth = ApiAuth::initiate($settings);

if (isset($_SESSION['accessTokenData'])) { //todo read from more permanent
  $auth->setAccessTokenDetails(json_decode($_SESSION['accessTokenData'], true));
}

if ($auth->validateAccessToken()){
  $accessTokenData = $auth->getAccessTokenData();
  $_SESSION['accessTokenData'] = json_encode($accessTokenData); //todo save more permanently

  if ($auth->accessTokenUpdated()) {
    echo '333<br>';
    $accessTokenData = $auth->getAccessTokenData();

    //store access token data however you want

  }

  $leadApi = MauticApi::getContext("leads", $auth, $baseUrl .'/api/');
    $leads = $leadApi->getList();
  echo '$leads =' . print_r($leads, true);

Even if I make a call to $auth->getAccessTokenData(); and store the results before the call to accessTokenUpdated, I get token_rejected [code] => 401 when I call $leadApi->getList(). So I'm doing something wrong.

@pavel-janicek
Copy link

I tried to contact you on the forums, but I will try also here:

First, delete this part:
if (isset($_GET['oauth_token']) && isset($_GET['oauth_verifier'])) { $settings['accessToken'] = $_GET['oauth_token']; $settings['accessTokenSecret'] = $_GET['oauth_verifier']; }

Second, you do not need to name the session, so this can be deleted too:
session_name("oauthtester");

and thirdly, if this does not do the thing, dump out the session and try to figure out what happened

@escopecz
Copy link
Sponsor Member

Since there hasn't been any answer for a while, I'll close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants