我最近下载了适用于PHP的Google API客户端库,将其上传到我的网站,并使用Google提供的代码连接我的日历。第一部分非常有效。我被发送到:https://accounts.google.com/o/oauth2/auth?response_type=code&redirect_uri=http
我按下接受按钮。我使用authcode重定向回这个文件。但是这条线路失败了:
$ accessToken = $ client-> authenticate($ 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);
答案 0 :(得分:0)
$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);
}