2012-07-12 61 views
3

我使用Google Analytics報告API在PHP中創建了一個內部應用程序。 該應用程序將始終使用同一個Google帳戶連接到Google Analytics,因此我只想將訪問權限和刷新令牌保存在我的數據庫中。使用GoogleAPI的OAuth給出'invalid_grant'

我使用以下Google tutorial來設置OAuth。我採取了以下步驟:

引導代碼(每一步)

$client = new apiClient(); 
$client->setApplicationName("Stat-robot"); 
$client->setApprovalPrompt("force");    
$client->setAccessType('offline'); 
$client->setClientId("CLIENT_ID"); 
$client->setClientSecret("CLIENT_SECRET"); 
$client->setDeveloperKey("DEV_KEY"); 
$client->setRedirectUri("http://localhost/statistics/api"); 
$client->setScopes("https://www.googleapis.com/auth/analytics.readonly"); 

步驟1

Redirect to the URL retrieved from $client->createAuthUrl()) 

步驟2

//Get a redirect from Google with a GET-variable 'code' 
$client->authenticate(); 
$accessToken = $client->getAccessToken 
//This gives me a JSON-format access-token 

步驟3

//Set the access token retrieved in previous request 
$client->setAccessToken($accessToken); 
//set the refresh token from the JSON response 
$client->refreshToken($refreshToken); 

現在,最後一部分(refreshToken)給我一個錯誤:invalid_grant。 我已閱讀所以和其他互聯網,我應該將我的訪問類型設置爲offline,並且我的批准提示爲force。但我想我有(看上面)。

那麼會出現什麼問題呢?

編輯:我正在閱讀谷歌API本身的來源。從OAuth的類sign()方法規定了以下內容:

if ($expired) { 
    if (! array_key_exists('refresh_token', $this->accessToken)) { 
     throw new apiAuthException("The OAuth 2.0 access token has expired, " 
      . "and a refresh token is not available. Refresh tokens are not " 
      . "returned for responses that were auto-approved."); 
     } 
     $this->refreshToken($this->accessToken['refresh_token']); 
    } 
} 

所以真的沒有必要呼籲$client->refreshToken()

+0

看起來像一個錯誤信息/代碼檢查供應商的文檔中給出該消息時。並與您的代碼進行比較。 – hakre 2012-07-12 12:07:21

+0

你說得很對。但供應商說我應該添加'access_type = offline'。我認爲我做了什麼 – 2012-07-12 12:10:36

+0

好吧,我沒有談到第三方,我談到了供應商。供應商(Google Inc.)對該錯誤代碼的評論是什麼? – hakre 2012-07-12 12:13:21

回答

0

對於接收到的錯誤的解決「invalid_grant」,谷歌的意見,同時更新您的服務器的時鐘,檢查你是否達到了最大的API調用。