Getting error “Authorized token from Service Provi

2019-09-08 19:37发布

问题:

I want to implement 3 legged oauthentication in google api.

Code :-

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

define('REDIRECT_URI',  'http://example.com/gmail/index.php');
require_once 'Zend/Oauth/Consumer.php';
session_start();

$CONSUMER_KEY = 'xxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com';
$CONSUMER_SECRET = 'xxxxxxxxxxxxxxxxxxxx';

// Multi-scoped token.
$SCOPES = array(
  'https://docs.google.com/feeds/',
  'https://spreadsheets.google.com/feeds/'
);

$oauthOptions = array(
  'requestScheme' => Zend_Oauth::REQUEST_SCHEME_HEADER,
  'version' => '2.0',
  'consumerKey' => $CONSUMER_KEY,
  'consumerSecret' => $CONSUMER_SECRET,
  'signatureMethod' => 'HMAC-SHA1',
  'callbackUrl' => REDIRECT_URI,
  'requestTokenUrl' => 'https://www.google.com/accounts/OAuthGetRequestToken',
  'userAuthorizationUrl' => 'https://www.google.com/accounts/OAuthAuthorizeToken',
  'accessTokenUrl' => 'https://www.google.com/accounts/OAuthGetAccessToken'
);

$consumer = new Zend_Oauth_Consumer($oauthOptions);

// When using HMAC-SHA1, you need to persist the request token in some way.
// This is because you'll need the request token's token secret when upgrading
// to an access token later on. The example below saves the token object as a session variable.
if (!isset($_SESSION['ACCESS_TOKEN'])) {
  $_SESSION['REQUEST_TOKEN'] = serialize($consumer->getRequestToken(array('scope' => implode(' ', $SCOPES))));
}

$approvalUrl = $consumer->getRedirectUrl(array('hd' => 'default'));
echo "<a href=\"$approvalUrl\">Grant access</a>";
// $consumer->redirect(array('hd' => 'default'));


if (!isset($_SESSION['ACCESS_TOKEN'])) {
  if (!empty($_GET) && isset($_SESSION['REQUEST_TOKEN'])) {
    $_SESSION['ACCESS_TOKEN'] = serialize($consumer->getAccessToken($_GET, unserialize($_SESSION['REQUEST_TOKEN'])));
  }
}

print_r($_SESSION);

I get following error

Fatal error: Uncaught exception 'Zend_Oauth_Exception' with message 'Authorized token from Service Provider does not match supplied Request Token details' in /var/www/gmail/Zend/Oauth/Consumer.php:215 Stack trace: #0 /var/www/gmail/index.php(58): Zend_Oauth_Consumer->getAccessToken(Array, Object(Zend_Oauth_Token_Request)) #1 {main} thrown in /var/www/gmail/Zend/Oauth/Consumer.php on line 215

Thanks In Advance.