无法刷新PHP中的OAuth2令牌,无效授权

Rob*_*ani 3 php google-calendar-api google-api oauth-2.0 google-oauth

我需要创建一个PHP脚本,在Google Calendar上创建一个sigle事件.我在设置客户端ID,客户端密钥,开发密钥和创建新事件时没有任何问题.

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

实际上,使用这个脚本我可以获得一个令牌和一个刷新令牌,但是我的令牌到期时我每小时都知道如何刷新它.如何编辑此代码才能执行此操作?我可以在某处保存令牌和刷新令牌并始终使用相同的数据吗?

我收到了一条未捕获的异常"Google_AuthException",其中显示消息"刷新OAuth2令牌时出错,消息:'{"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('');                    
....

?>
Run Code Online (Sandbox Code Playgroud)

非常感谢你的支持!

pin*_*yid 5

此页面https://developers.google.com/accounts/docs/OAuth2WebServer#offline说明了刷新令牌的工作原理,以及如何使用它来使用原始http获取新的访问令牌.

从这个问题如何使用Google API客户端刷新令牌?这是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()
Run Code Online (Sandbox Code Playgroud)

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

  1. 请确保您已阅读https://developers.google.com/accounts/docs/OAuth2以及相应的子页面(webapp,javascript等),具体取决于您使用的oauth风格.如果您不了解它正在做什么,您将无法调试您的应用程序.

  2. 使用https://developers.google.com/oauthplayground/上的Oauth Playground 来完成Oauth步骤并最终进行Google API调用.这将确认您的理解并向您显示http调用和响应的外观

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