簡體   English   中英

Google刷新令牌獲得無效授權錯誤

[英]Google refresh token getting invalid grant error

我正在嘗試為我的帳戶使用Google Analytics(分析)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()

如果有人有興趣,幾個小時后我終於解決了:

我去了oauth游樂場:右側為https://code.google.com/oauthplayground ,那里有一個設置按鈕。 選擇“使用您自己的OAuth憑據”。 從那里獲取了刷新令牌,並且沒有問題。

如果有人遇到這個問題...

我們遇到一個問題,即相同的發帖請求最初不會起作用,但之后會起作用。

對我們來說,谷歌對空閑時間似乎非常挑剔-嘗試從中減去5分鍾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM