2017-02-10 76 views
1

帶有服務帳戶憑證的谷歌驅動器API,要上傳特定用戶的文件j********@gmail.com。訪問谷歌驅動器api服務帳戶有時會失敗

我在Google開發人員API控制檯中創建了我的帳戶服務憑據。 我添加權限的用戶j****@gmail.com 本人授權我的應用程序的j****@gmail.com

但有時autetication失敗 本messagge

Google_Service_Exception 

{ 
"error": "unauthorized_client", 
"error_description": "Client is unauthorized to retrieve access tokens using this method." 
} 

這裏的我的身份驗證

if ($credentials_file = checkServiceAccountCredentialsFile()) { 
    // set the location manually 
    $client->setAuthConfig($credentials_file); 
} elseif (getenv('GOOGLE_APPLICATION_CREDENTIALS')) { 
    // use the application default credentials 
    $client->useApplicationDefaultCredentials(); 
} else { 
    echo missingServiceAccountDetailsWarning(); 
    return; 
} 
$client->setSubject('j******@gmail.com'); 
$client->setScopes(['https://www.googleapis.com/auth/drive']); 
$service = new Google_Service_Drive($client); 
$files = $service->files->listFiles(); 
+0

您需要澄清何時「有時」,作爲識別問題的一種方式。 – Wolfie

+0

有時候意思是說如果我刷新頁面,有三次或四次我有錯誤,這有點奇怪,非常感謝Gianmaria –

+0

這裏是鏈接,只是有一個想法 http://advertising.fortale.it/basic/web/ –

回答

0

代碼可以使用Google API Client Library for PHP創建使用OAuth 2.0授權訪問谷歌API的Web服務器應用程序。另外,Web應用程序在進行API調用時也經常使用service accounts,尤其是在調用Cloud API訪問基於項目的數據而不是用戶特定的數據時。這些機制可以與Web服務器應用程序中的用戶授權一起使用。

關於你的錯誤unauthorized_client,它通常發生在請求未能通過驗證的情況下。如here所述,如果您使用Oauth2,則需要爲將要使用您的應用程序的每個用戶提供一個刷新令牌。您可以在OAuth2的工作方式上檢查此tutorial