doupang9080 2015-05-29 14:07
浏览 47

Invlid_grant尝试刷新令牌时 - Google Oauth - PHP

just started using google admin SDK,trying to retrieve specific user's infomation. i keep getting the same error over and over

[29-May-2015 14:41:18 Africa/Tunis] PHP Fatal error:  Uncaught
exception 'Google_Auth_Exception' with message 'Error refreshing the
OAuth2 token, message: '{   "error" : "invalid_grant"

during my research i found that it's mostly a syncing problem(server time) but my web app is hosted on a third party server.checked my service account credentials

Ps: don't know if it matters but the server's time zone is GMT+1

`

<?php
require 'src/Google/autoload.php';

session_start();

$timestamp = $_SERVER['REQUEST_TIME'];
echo gmdate("Y-m-d\TH:i:s\Z", $timestamp);

$service_account_name = "xx";
$key_file_location = "yyy.p12";

$client = new Google_Client();

$client->setApplicationName("Members");

$directory = new Google_Service_Directory($client);

if (isset($_SESSION['service_token']) && $_SESSION['service_token']) {
  $client->setAccessToken($_SESSION['service_token']);
}

$key = file_get_contents($key_file_location);

$cred = new Google_Auth_AssertionCredentials(
  // Replace this with the email address from the client.
  $service_account_name,
  // Replace this with the scopes you are requesting.
  array('https://www.googleapis.com/admin/directory/v1/users'),
  $key,
  'notasecret'

);
$cred->sub = "admin-email";
$client->setAssertionCredentials($cred);

if ($client->getAuth()->isAccessTokenExpired()) {
  $client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();


$email = "personto lookfor-email";
$r = $dir->users->get($email);
if($r) {
     echo "Name: ".$r->name->fullName."<br/>";
     echo "Suspended?: ".(($r->suspended === true) ? 'Yes' : 'No')."<br/>";
     echo "Org/Unit/Path: ".$r->orgUnitPath."<br/>";
} else {
     echo "User does not exist: $email<br/>";
     // if the user doesn't exist, it's safe to create the new user
}

`

  • 写回答

1条回答 默认 最新

  • doutian3010 2015-05-29 16:33
    关注

    The reason of the "Invalid grant" error may be due to the refresh token not working. This happens When the number of refresh tokens exceeds the limit, older tokens become invalid. If the application attempts to use an invalidated refresh token, an invalid_grant error response is returned. Here is the link for more documentation.

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP