【问题标题】:Unable to get Google OAuth Token无法获取 Google OAuth 令牌
【发布时间】: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


    【解决方案1】:

    $client-&gt;setRedirectUri() 应该会收到一个指向您的应用程序的 URL,因为这是您在 Google 页面中授权应用程序后将被重定向到的 URL。这就是您被重定向到日历的原因,因为您使用的是日历 URL。

    您的脚本似乎已经需要 $_GET['code'],因此请尝试将重定向 URL 设置为此脚本的 url。您还需要在 Developers Console 中的 API -> Credentials 下设置相同的 URL。

    只有当您使用价值代码调用 $client-&gt;authenticate($_GET['code']); 时,您才会获得一个令牌。

    看看this lib I just commited to Github,这是一个早期版本,但运行良好(我用来访问 YouTube)。它正在使用new official library。

    更新:正如 cmets 中所说,即使所有 Google 示例代码都设置了开发者密钥,但实际上并不需要开发者密钥。

    【讨论】:

    • 谢谢你,@ViniciusPinto。我已更改该重定向,现在收到有关 API 未激活的 403 错误。如果您对此有耐心(也为我感到遗憾!)请您看一下我发布的编辑。也许这就像打开另一个 API 一样简单,但我不知道它需要哪些,除了已经激活的 Calendar API。再次感谢你。我真的很感激!
    • 我认为它只需要日历 API,脚本对我来说工作正常。仔细检查您的凭据并确保您将凭据用于正确的项目。我不确定还有什么问题。
    • 我一遍又一遍地检查它们。我需要做出一些选择(比如我是使用服务器应用程序的密钥还是浏览器应用程序的密钥),但我可能会弄错。我暂时包含了这个项目的整个开发者控制台的屏幕截图。都是测试数据,后面我会重新生成这些key,去掉截图。请你看看这个,看看我是否遗漏了什么?再次感谢。你是救生员。
    • 开发者密钥应该是“服务器应用程序密钥”下的API密钥
    • 嗯,我不确定,但您尝试过清除 cookie 吗?看起来它正在尝试使用您会话中的令牌,并且该令牌是使用错误的凭据创建的...
    猜你喜欢
    • 1970-01-01
    • 2016-07-10
    • 1970-01-01
    • 1970-01-01
    • 2018-06-17
    • 1970-01-01
    • 2019-06-02
    • 1970-01-01
    • 2015-10-11
    相关资源
    最近更新 更多