我的目标是通过 google plus 提供一次登录。我基本上想保存访问令牌,然后在用户返回时重用它。我尝试了很多论坛,并在过去 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
返回网址上的代码
$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**
错误信息:
致命错误:/opt/bitnami/apache2/htdocs/src/auth/apiOAuth2.php: 144 堆栈跟踪中的消息“无法 json 解码访问令牌”中未捕获的异常“apiAuthException”导致“$client- >setAccessToken($row['token'])"
任何帮助表示赞赏