Google客户端API刷新令牌致命错误:未捕获的异常'Google_Auth_Exception''刷新OAuth2令牌时出错

时间:2015-01-24 15:59:28

标签: php oauth google-api google-api-php-client google-client


当我尝试刷新我的令牌时,我收到了

  

致命错误:未捕获的异常'Google_Auth_Exception',显示消息'刷新OAuth2令牌时出错,消息:'{“error”:“invalid_client”,“error_description”:“找不到OAuth客户端。” }''

// FUNCTION THAT MAKES THE OAUTH 
$oauth_scope = "contacts"
$client = new Google_Client();
$client->setClientId(CLIENT_ID);
$client->setClientSecret(CLIENT_SECRET);
$client->setRedirectUri(REDIRECT_URI);

switch ($oauth_scope) {
    case 'contacts':

        $client->addScope("https://www.google.com/m8/feeds/");
        $client->addScope("https://www.googleapis.com/auth/apps.groups.settings");
        // to get the refresh token
        $client->setAccessType('offline');
        $client->setApprovalPrompt('force');

        // $client->addScope("https://picasaweb.google.com/data/");
        $template_name = "contact_make_outh";
        $_SESSION["oauth2"] = "contacts";
        break;

    default:
        # code...
        break;
}
$data['auth_url'] = $client->createAuthUrl();



// OAUTH CALLBACK

$client = new Google_Client();
$client->setClientId(CLIENT_ID);
$client->setClientSecret(CLIENT_SECRET);
$client->setRedirectUri(REDIRECT_URI);

switch ($_SESSION["oauth2"]) {
    case 'contacts':
        $client->addScope("https://www.google.com/m8/feeds/");
        $client->addScope("https://www.googleapis.com/auth/apps.groups.settings");
        break;

    default:
        # code...
        break;
}


if (isset($_GET['code']) && !isset($_SESSION['access_token'][$_SESSION["oauth2"]])) {

  $client->authenticate($_GET['code']);
  $tokens = json_decode($client->getAccessToken() , true);
  $_SESSION['access_token'][$_SESSION["oauth2"]] = $tokens;

}
//  store the main  token for refresh in the logged person
$logged_person = CurrentLoggedPerson();
$logged_person->setGoogleToken($client->getAccessToken());
$logged_person->store();

unset($_SESSION["oauth2"]);


// REFRESH TOKEN FUNCTION

$for = "contacts";
$logged_person = CurrentLoggedPerson();
$token = logged_person->getGoogleToken();
/*
LOOKS LIKE :
array
(
    'access_token' => '*********',
    'token_type' => 'Bearer',
    'expires_in' => '3600',
    'refresh_token' => '*********',
    'created' => '1422104143'
);
*/


// TOKEN IS STORED IN THE DB AFTER THE FIRST OAUTH REQUEST AND FETCHED FROM THERE
// THE TOKEN WORKS THIS WAY UNTIL IT EXPIRES

require_once 'app/lib/google-api-php-client/autoload.php';

$client = new Google_Client();
$client->setClientId(CLIENT_ID);
$client->setClientSecret(CLIENT_SECRET);
$client->setRedirectUri(REDIRECT_URI);

switch ($for) {
    case 'contacts':
        $client->addScope("https://www.google.com/m8/feeds/");
        $client->addScope("https://www.googleapis.com/auth/apps.groups.settings");
        // $client->setAccessType('offline');
        // $client->setApprovalPrompt('force');
        $redirect = "?action=google_api/contacts_sync&with_controller_action=1";
        break;

    default:
        # code...
        break;
}

$client->setAccessToken(json_encode($token));

    if($client->isAccessTokenExpired()) {
        $refreshToken = $token['refresh_token'];
        $client->refreshToken($refreshToken);
        $_SESSION['access_token'][$for] = json_decode($client->getAccessToken(), true);
}

0 个答案:

没有答案