2016-07-19 42 views
0

我已經嘗試過所有事情的組合,試圖在這個問題上取得一些進展,但我無法獲得任何地方。我正嘗試使用服務帳戶訪問域上用戶的日曆。服務帳戶已設置,似乎工作正常。據我所知,根據https://developers.google.com/identity/protocols/OAuth2ServiceAccount?hl=en_US#delegatingauthority已啓用域範圍委派。在我的代碼下面,一切正常,直到我設置模擬帳戶,並在標題中出現錯誤。我見過各種不同的帖子,關於驗證模仿用戶等,但這似乎不符合谷歌所說的。我卡住了。Google API PHP,服務帳戶假冒問題

$client = new Google_Client(); 
$client->setSubject('[email protected]'); 
$client->setAuthConfig($_SERVER['DOCUMENT_ROOT'] . '/private.json'); 
$client->setScopes(['https://www.googleapis.com/auth/calendar', 'https://www.googleapis.com/auth/calendar.readonly']); 

這將導致任何類型的API請求的失敗與錯誤:

Message: { 
"error": "unauthorized_client", 
"error_description": "Unauthorized client or scope in request." 
} 

回答

1

你怎麼樣?

對於其顯示的文字想念你釋放,如果你想使用谷歌日曆應用程序將使用的scopo,你必須使用此:

$客戶 - > addScope(「https://www.googleapis.com/auth/calendar,https://www.googleapis.com/auth/calendar.readonly」);

範圍的詳細信息: OAuth 2.0 Scopes for Google APIs

:)

+0

感謝您的答覆。我確實有這樣的設置,但沒有包含原始問題,但已更新。 –

+0

您在管理控制檯中添加了在Google Developer Console中創建的服務密鑰? 以下教程:https://developers.google.com/api-client-library/php/auth/service-accounts#delegatingauthority –