首页 文章

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

提问于
浏览
3

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

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

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

我收到了一条未捕获的异常“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 '
'; echo 'refresh_token = ' . $refreshToken; echo '
'; echo 'token_type = ' . $tokenType; echo '
'; 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(''); .... ?>

非常感谢你的支持!

3 回答

  • 5

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

    从这个问题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()
    

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

    • 确保您已阅读https://developers.google.com/accounts/docs/OAuth2以及相应的子页面(webapp,javascript等),具体取决于您使用的oauth风格 . 你赢了't get very far debugging your app if you don't了解它在做什么 .

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

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

  • 0

    之前我收到此错误,因为我试图进行两次身份验证 .

    既然你有这条线:

    if (isset($_GET['code']))

    无论您是否已经过身份验证,它都会尝试在代码位于查询字符串中时进行身份验证 . 在尝试(重新)进行身份验证之前,请尝试检查SESSION令牌是否存在 .

  • 2

    尝试删除setDeveloperKey(),这给我带来了一些问题,似乎不需要 .

相关问题