【问题标题】:Google refresh token getting invalid grant error谷歌刷新令牌获得无效授权错误
【发布时间】:2012-06-11 07:12:31
【问题描述】:

我正在尝试为我的帐户使用谷歌分析 API。所以起初我允许应用程序使用离线刷新令牌进行访问。当我尝试刷新令牌时购买,我收到“无效授权”错误。尝试使用 php 客户端库和使用 curl 手动刷新。

这是手动使用 curl:

$oauth2token_url = "https://accounts.google.com/o/oauth2/token";
    $clienttoken_post = array(
    "client_id" => 'xxxxxxxx-d28qutrse5bvaf7n4a37qgs5ieijvp9n.apps.googleusercontent.com',
    "client_secret" => 'xxxxxxxxxxxxx');

        $clienttoken_post["refresh_token"] = $refresh_token;
        $clienttoken_post["grant_type"] = "refresh_token";

    $curl = curl_init($oauth2token_url);

    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $clienttoken_post);
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    $json_response = curl_exec($curl);
    curl_close($curl);

    $authObj = json_decode($json_response);

    print_r($authObj);


    exit();

我使用 simple.php 示例通过先前调用获得的刷新令牌:

require_once '../../src/apiClient.php';
require_once '../../src/contrib/apiAnalyticsService.php';
session_start();

$client = new apiClient();
$client->setApplicationName("Google Analytics PHP Starter Application");

// Visit https://code.google.com/apis/console?api=analytics to generate your
// client id, client secret, and to register your redirect uri.
 $client->setClientId('xxxxxxxxx-d28qutrse5bvaf7n4a37qgs5ieijvp9n.apps.googleusercontent.com');
 $client->setClientSecret('xxxxxx');
 $client->setRedirectUri('https://www.xxxxxxxxxx.org/oauth2callback/google-api-php-client/examples/analytics/simple.php');
 $client->setDeveloperKey('xxxxxxx');



if (isset($_GET['logout'])) {
  unset($_SESSION['token']);
}

if (isset($_GET['code'])) {
  $client->authenticate();
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

if (isset($_SESSION['token'])) {
//    echo $_SESSION['token'];
  $client->setAccessToken($_SESSION['token']);
}
echo $client->getAccessToken()

【问题讨论】:

    标签: php google-analytics-api google-api-client


    【解决方案1】:

    如果有人感兴趣,我在几个小时后终于解决了:

    我去了oauth游乐场:https://code.google.com/oauthplayground 在右侧,有一个设置按钮。选择“使用您自己的 OAuth 凭据”。从那里获取刷新令牌,它没有问题。

    【讨论】:

      【解决方案2】:

      如果其他人遇到这个问题...

      我们遇到了一个问题,即相同的发布请求最初无法正常工作,但之后会正常工作。

      对于我们来说,谷歌似乎对 iat 时间非常挑剔——试着从中减去 5 分钟。

      【讨论】:

        猜你喜欢
        • 2020-07-26
        • 2019-10-17
        • 2017-12-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-01
        • 2016-01-07
        • 1970-01-01
        相关资源
        最近更新 更多