php - 无法在 PHP 中刷新 OAuth2 token ,授予无效

标签 php google-api oauth-2.0 google-calendar-api google-oauth

我需要制作一个 PHP 脚本来在 Google 日历上创建一个单一事件。 我在设置客户端 ID、客户端密码、开发 key 和创建新事件时没有遇到任何问题。

我唯一的问题是 OAuth2,特别是我需要建立永久连接,我不想每次运行脚本时都进行身份验证。

实际上,使用此脚本我可以获得一个 token 和一个刷新 token ,但我的 token 每小时都会过期,我不知道如何刷新它。我怎样才能编辑这段代码来做到这一点? 我可以将 token 和刷新 token 都保存在某处并始终使用相同的数据吗?

我获得了一个未捕获的异常“Google_AuthException”,消息为“刷新 OAuth2 token 时出错,消息:”{“error”:“invalid_grant”

我已经在 stackoverflow 中阅读了一些关于此主题的其他帖子,但我仍然没有找到解决方案...:\

<?php

 require_once 'src/Google_Client.php';
 require_once 'src/contrib/Google_CalendarService.php';
 session_start();

 $client = new Google_Client();
 $client->setApplicationName("Calendar App");
 $client->setClientId('xxxx');
 $client->setClientSecret('yyy');
 $client->setRedirectUri('http://www.zzzzzz');  
 $client->setDeveloperKey('kkk');
 $client->setAccessType('offline');
 $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'])) {
    //echo $_SESSION['token'];
  //$client->setAccessToken($_SESSION['token']);
    $authObj = json_decode($_SESSION['token']);
    $accessToken = $authObj->access_token;
    $refreshToken = $authObj->refresh_token;
    $tokenType = $authObj->token_type;
    $expiresIn = $authObj->expires_in;
    echo 'access_token = ' . $accessToken;
    echo '<br />';
    echo 'refresh_token = ' . $refreshToken;
    echo '<br />';
    echo 'token_type = ' . $tokenType;
    echo '<br />';
    echo 'expires_in = ' . $expiresIn;
}

if(!empty($cookie)){
    $client->refreshToken($this->Cookie->read('token'));
}

if ($client->getAccessToken()) {
    $calList = $cal->calendarList->listCalendarList();
    $_SESSION['token'] = $client->getAccessToken();
} else {
  $authUrl = $client->createAuthUrl();
  print "<a class='login' href='$authUrl'>Connect Me!</a>";
}


// Creation of a single event
$event = new Google_Event();
$event->setSummary($event_name);            
$event->setLocation('');                    
....

?>

非常感谢您的支持!

最佳答案

本页https://developers.google.com/accounts/docs/OAuth2WebServer#offline解释了刷新 token 的工作原理,以及如何使用它通过原始 http 获取新的访问 token 。

从这个问题How to refresh token with Google API client?这是等效的 php

here is the snippet to set token, before that make sure the access type should be set to offline

if (isset($_GET['code'])) {
  $client->authenticate();
  $_SESSION['access_token'] = $client->getAccessToken();
}
To refresh token

$google_token= json_decode($_SESSION['access_token']);
$client->refreshToken($google_token->refresh_token);
this will refresh your token, you have to update it in session for that you can do

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

调试 按照以下三个步骤调试任何 oauth 应用程序

  1. 确保您已阅读 https://developers.google.com/accounts/docs/OAuth2以及相应的子页面(webapp、javascript 等),具体取决于您使用的是哪种类型的 oauth。如果您不了解您的应用在做什么,您将无法调试您的应用。

  2. 使用 Oauth Playground https://developers.google.com/oauthplayground/完成 Oauth 步骤并最终调用 Google API。这将确认您的理解并向您展示 http 调用和响应的样子

  3. 使用跟踪/日志记录/代理等来跟踪来自您应用的实际 http 流量。将此与您在第 2 步中观察到的情况进行比较。这应该会告诉您问题出在哪里。

关于php - 无法在 PHP 中刷新 OAuth2 token ,授予无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19848002/

相关文章:

php - 重复行输出 PHP - 连接的 fgets

node.js - 如何禁用 googleapi 上的检查重定向 url?

c# - 如何使用服务帐户通过 .NET C# 访问 Google Analytics API V3?

php - Laravel 自定义数据库驱动程序

php - 获取包含其他数组的关联数组中的多值

Python:尝试使用刷新的凭据创建辅助日历时,googleapiclient 返回 "insufficient authentication scopes"

javascript - 如何在 Google Chrome 扩展程序中使用 JavaScript 获取 Facebook 的新 OAuth 2.0 访问 token ?

c# - 获取(OAuthException - #2500)必须使用事件访问 token 来查询有关当前用户的信息

php - 如何上传图像代码并将路径保存到mysql db

python - Nosetest 和 Google API : "no module named apiclient.discovery"