douqian1975 2014-03-17 15:34
浏览 57
已采纳

无法获得Google OAuth令牌

I'm trying to add calendar events to my Google calendar from a php script. Note that I can do this successfully directly from a Google test page so I think that my calendar is set up correctly. However, when I attempt do this from a php script, I'm not able to get the OAuth2 token.

The following script, mostly taken from a Google example, runs without error, but it always winds up with a "Connect Me!" link. When I click on this link, it takes me to a validation screen that warns me that this app would like to manage my calendars. I click 'Accept' and a then see my calendar. A little inconvenient, but if this gets the OAuth token, fine.

I then go back and run the script again, hoping that the access token will now be available, but I get exactly the same result. No matter how many times I run this script, I never get the token, which I need in order to expand the script to add events to the calendar.

I've been at this for days, and I'm just not getting anywhere. Can anyone help? Thanks.

<?php
require_once '../../src/Google_Client.php';
require_once '../../src/contrib/Google_CalendarService.php';
require_once '../../src/auth/Google_OAuth2.php';

session_start();

$client = new Google_Client();
$client->setApplicationName("Add Google Calendar Entries");

// Visit https://code.google.com/apis/console?api=calendar to generate your
// client id, client secret, and to register your redirect uri.
$client->setClientId('843319906820-1s1d737e77o71a3vaskf434k3ape1fk5.apps.googleusercontent.com');
$client->setClientSecret('AP5imn3e0TEWNyLTCNm8YJj6');
$client->setRedirectUri('https://www.google.com/calendar/');
$client->setDeveloperKey('AIzaSyDIw3ks7AmrIrRxjO9y2gWBhQDYHFWd-uc');
$client->setUseObjects(true);

$cal = new Google_CalendarService($client);

if (isset($_GET['logout'])) {
  unset($_SESSION['token']);
}

if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}

if ($client->getAccessToken()) {
  $calList = $cal->calendarList->listCalendarList();
  print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";

  $_SESSION['token'] = $client->getAccessToken();

} else {
  $authUrl = $client->createAuthUrl();
  print "<a class='login' href='$authUrl'>Connect Me!</a>";
}

?>

EDIT: Attempting to implement the suggestion from Vinicius Pinto. I've changed my redirect to point directly back to this script. Now I get this error:

Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error calling GET https://www.googleapis.com/calendar/v3/users/me/calendarList?key=AIzaSyDIw3ks7AmrIrRxjO9y2gWBhQDYHFWd-uc: (403) Access Not Configured. Please use Google Developers Console to activate the API for your project.' in D:\Hosting\11347607\html\scripts\google-api-php-client\src\io\Google_REST.php:66 Stack trace: #0 D:\Hosting\11347607\html\scripts\google-api-php-client\src\io\Google_REST.php(36): Google_REST::decodeHttpResponse(Object(Google_HttpRequest)) #1 D:\Hosting\11347607\html\scripts\google-api-php-client\src\service\Google_ServiceResource.php(186): Google_REST::execute(Object(Google_HttpRequest)) #2 D:\Hosting\11347607\html\scripts\google-api-php-client\src\contrib\Google_CalendarService.php(205): Google_ServiceResource->__call('list', Array) #3 D:\Hosting\11347607\html\scripts\google-api-php-client\examples\calendar\simple.php(43): Google_CalendarListServiceResource->listCalen in D:\Hosting\11347607\html\scripts\google-api-php-client\src\io\Google_REST.php on line 66

It would appear that I don't have the correct APIs activated in the developer console. I have the Calendar API and the Google Cloud Storage JSON API activated and nothing else. I don't see any other APIs that would be relevant, and I don't think that this message is telling me which APIs it wants.

EDIT 2: Looks like I was using the wrong developer key. I've changed it to the key for server applications. Now my error is this:

Fatal error: Uncaught exception 'Google_AuthException' with message 'Error refreshing the OAuth2 token, message: '{ "error" : "invalid_client" }'' in D:\Hosting\11347607\html\scripts\google-api-php-client\src\auth\Google_OAuth2.php:288 Stack trace: #0 D:\Hosting\11347607\html\scripts\google-api-php-client\src\auth\Google_OAuth2.php(248): Google_OAuth2->refreshTokenRequest(Array) #1 D:\Hosting\11347607\html\scripts\google-api-php-client\src\auth\Google_OAuth2.php(225): Google_OAuth2->refreshToken('1/6ZdJyZALqcbwW...') #2 D:\Hosting\11347607\html\scripts\google-api-php-client\src\service\Google_ServiceResource.php(167): Google_OAuth2->sign(Object(Google_HttpRequest)) #3 D:\Hosting\11347607\html\scripts\google-api-php-client\src\contrib\Google_CalendarService.php(205): Google_ServiceResource->__call('list', Array) #4 D:\Hosting\11347607\html\scripts\google-api-php-client\examples\calendar\simple.php(47): Google_CalendarListServiceResource->listCalendarList() in D:\Hosting\11347607\html\scripts\google-api-php-client\src\auth\Google_OAuth2.php on line 288

  • 写回答

1条回答 默认 最新

  • duanpingzu7194 2014-03-18 00:56
    关注

    The $client->setRedirectUri() should receive a URL pointing to your application, because this is the URL that you'll be redirected to after you authorize the application in the Google page. That's why you are being redirect to the calendar, because you are using the calendar URL.

    It seems like your script is already expecting a $_GET['code'], so try setting the redirect URL to the url of this script. You'll also need to set the same URL in the Developers Console, under API -> Credentials.

    You only get a token when you call $client->authenticate($_GET['code']); with a value code.

    Take a look at this lib I just commited to Github, it's an early version, but it's working fine (I'm using to access YouTube). It's using the new official library.

    Update: as said in the comments, the developer key isn't actually required for this to work, even though all Google sample code sets it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历