我在这里:
https://developers.google.com/+/web/signin/server-side-flow
在步骤 7 和 8 中,引用了变量$request但该变量尚未初始化,因此从他们提供的示例复制和粘贴不起作用,我只收到 500 服务器错误仅第 7 或步骤 8 的第一行,步骤 8 行使用 $request,从未从他们的示例中初始化。
$code = $request->getContent();
您正在查看的示例代码使用 Twig,其中包含 $request
和$response
值来简化 RESTful 端点。
以下代码在没有 Twig 依赖项的情况下执行等效代码:
<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_PlusService.php';
$client = new Google_Client();
// CLIENT ID / Secret from https://code.google.com/apis/console
$CLIENT_ID = 'YOUR_CLIENT_ID';
$client->setClientId($CLIENT_ID);
$client->setClientSecret('YOUR_CLIENT_SECRET');
// CUSTOM redirect URI assuming code from JavaScript callback
$client->setRedirectUri('postmessage');
$plus = new Google_PlusService($client);
// Code from the client (returned in signinCallback, or in token on Android)
$code = file_get_contents('php://input');
// Exchange the OAuth 2.0 authorization code for user credentials.
$client->authenticate($code);
$token = json_decode($client->getAccessToken());
// Verify the token
$reqUrl = 'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=' .
$token->access_token;
$req = new Google_HttpRequest($reqUrl);
$tokenInfo = json_decode(
$client::getIo()->authenticatedRequest($req)->getResponseBody());
// If there was an error in the token info, abort.
if ($tokenInfo->error) {
print $tokenInfo->error;
}
// Make sure the token we got is for our app.
if ($tokenInfo->audience != CLIENT_ID) {
print "Token's client ID does not match app's.";
}
print 'Token from result: ' . print_r($token, true);