【发布时间】:2014-03-17 15:34:20
【问题描述】:
我正在尝试通过 php 脚本将日历事件添加到我的 Google 日历中。请注意,我可以直接通过 Google 测试 page 成功完成此操作,因此我认为我的日历设置正确。但是,当我尝试从 php 脚本执行此操作时,我无法获取 OAuth2 令牌。
以下脚本大部分取自 Google 示例,运行时没有错误,但最后总是显示“连接我!”关联。当我单击此链接时,它会将我带到一个验证屏幕,警告我此应用程序想要管理我的日历。我单击“接受”,然后查看我的日历。有点不方便,但如果这获得了 OAuth 令牌,就可以了。
然后我返回并再次运行脚本,希望访问令牌现在可用,但我得到完全相同的结果。无论我运行此脚本多少次,我都没有获得令牌,我需要它来扩展脚本以将事件添加到日历中。
我已经在这工作了好几天,但我就是无处可去。任何人都可以帮忙吗?谢谢。
<?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>";
}
?>
编辑:尝试实施 Vinicius Pinto 的建议。我已将重定向更改为直接指向此脚本。现在我得到这个错误:
致命错误:带有消息的未捕获异常“Google_ServiceException” '调用GET出错 https://www.googleapis.com/calendar/v3/users/me/calendarList?key=AIzaSyDIw3ks7AmrIrRxjO9y2gWBhQDYHFWd-uc: (403) 未配置访问。请使用 Google Developers Console 来 为您的项目激活 API。在 D:\Hosting\11347607\html\scripts\google-api-php-client\src\io\Google_REST.php:66 堆栈跟踪:#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 在第 66 行
我似乎没有在开发者控制台中激活正确的 API。我激活了Calendar API 和Google Cloud Storage JSON API,仅此而已。我没有看到任何其他相关的 API,而且我不认为这条消息告诉我它需要哪些 API。
编辑 2:看起来我使用了错误的开发人员密钥。我已将其更改为服务器应用程序的密钥。现在我的错误是这样的:
致命错误:带有消息的未捕获异常“Google_AuthException” '刷新 OAuth2 令牌时出错,消息:'{ "error" : "invalid_client" }'' 在 D:\Hosting\11347607\html\scripts\google-api-php-client\src\auth\Google_OAuth2.php:288 堆栈跟踪:#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() 在 D:\Hosting\11347607\html\scripts\google-api-php-client\src\auth\Google_OAuth2.php 在第 288 行
【问题讨论】:
标签: php oauth google-api-php-client