谷歌+保存令牌不工作



我的目标是提供一次性登录与google +。我基本上想保存访问令牌,然后在用户返回时重用它。我已经尝试了许多论坛,并在过去2天尝试了各种代码。不管用。有谁能帮帮忙吗?

首次登录码:

$client = new apiClient();
$client->setApplicationName("xxxx");
$client->setAccessType("offline");
$client->setClientId(GCLIENT_ID);
$client->setClientSecret(GCLIENT_SECRET);
$client->setRedirectUri('xxxx');
$client->setDeveloperKey('xxxx');
$client->setScopes(array('https://www.googleapis.com/auth/plus.me'));
$url = $client->createAuthUrl();
//redirected to this url

返回url的代码

$client = new apiClient();
$client->setApplicationName("xxxx");
$client->setClientId(GCLIENT_ID);
$client->setClientSecret(GCLIENT_SECRET);
$client->setRedirectUri('xxxx');
$client->setDeveloperKey('xxxx');
$client->setScopes(array('https://www.googleapis.com/auth/plus.me'));
$client->setAccessType("offline");
$plus = new apiPlusService($client);
if (isset($_GET['code'])) {
    $client->authenticate();
    $_SESSION['access_token'] = $client->getAccessToken();
    gp_reg($_SESSION['access_token']); 
   **//this function saves the access token to the database**
}

用户返回

$client = new apiClient();
$client->setApplicationName("xxxx");
$client->setClientId(GCLIENT_ID);
$client->setClientSecret(GCLIENT_SECRET);
$client->setRedirectUri('xxxxx');
$client->setDeveloperKey('xxxx');
$client->setScopes(array('https://www.googleapis.com/auth/plus.me'));
$client->setAccessType('online');
$client-> setApprovalPrompt("auto");
$plus = new apiPlusService($client);
print_r($row['token']);
$client->setAccessToken($row['token']);
// **$row['token'] contains the token from database**

错误信息:

致命错误:未捕获异常'apiAuthException'与消息'Could not json decode the access token'在/opt/bitnami/apache2/htdocs/src/auth/apiOAuth2.php:144$client->setAccessToken($row['token'])"

感谢您的帮助

这听起来像是一个类似的问题,可以帮助您找到处理已返回的令牌的最佳方法。听起来这可能是它如何作为JSON发送回来和如何存储(可能作为字符串而不是JSON对象)之间的问题。

https://groups.google.com/forum/!味精/google-api-php-client/fc7RUw1Pf44/TKpEihEp35wJ

如果是这种情况,您可能需要在将其从数据库中取出时将其重新构建为对象。

或者您可能只想确保您使用的是refresh_token值,因为这才是您真正需要使用的。

最新更新