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.

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

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog