谷歌 API 客户端库


Google API client library

我最近下载了 PHP 的 Google API 客户端库,将其上传到我的网站,并使用 Google 提供的代码与我的日历连接。第一部分效果很好。我被发送到:https://accounts.google.com/o/oauth2/auth?response_type=code&redirect_uri=http我按下接受按钮的地方。我被重定向回带有身份验证码的文件。但是这条线失败了:
$accessToken = $client->身份验证($authCode);

require_once ('google-api-php-client/autoload.php'); // path to autoload.php
if ($_GET[code] == FALSE) {
$client = new Google_Client();
$client->setClientId('My client ID');
$client->setClientSecret('My secret');
$client->setRedirectUri('http://'); // path to this file.
$client->addScope('https://www.googleapis.com/auth/calendar');
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$service = new Google_Service_Calendar($client);
$authUrl = $client->createAuthUrl();
header("location: $authUrl"); // sends me to account.google.com  I accept and get send back to    this file :-)
}
Else {
//$authCode = trim(fgets(STDIN));
$authCode = $_GET[code]; // get authcode from google
//Exchange authorization code for access token
$accessToken = $client->authenticate($authCode); //Fatal error: Call to a member function on a              strong text**non-object in authenticate() on line 23
$client->setAccessToken($accessToken); 

$client变量需要在if分支之外的范围内,如下所示:

require_once ('google-api-php-client/autoload.php'); // path to autoload.php
$client = new Google_Client();
$client->setClientId('My client ID');
$client->setClientSecret('My secret');
$client->setRedirectUri('http://'); // path to this file.
$client->addScope('https://www.googleapis.com/auth/calendar');
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$service = new Google_Service_Calendar($client);
if ($_GET['code'] == FALSE) {
  $authUrl = $client->createAuthUrl();
  header("location: $authUrl"); // sends me to account.google.com  I accept and get send back to    this file :-)
} else {
  //$authCode = trim(fgets(STDIN));
  $authCode = $_GET['code']; // get authcode from google
  //Exchange authorization code for access token
  $accessToken = $client->authenticate($authCode); //Fatal error: Call to a member function on a              strong text**non-object in authenticate() on line 23
  $client->setAccessToken($accessToken);
}