我正嘗試使用Google GAA的Google Api OAuth服務帳戶創建Google日曆活動。使用Google Oauth2 Credentials'服務帳戶'模擬用戶EMAIL ID
我需要正確的會話ID或「通過創建」日曆事件現場的用戶電子郵件ID。 這可能很好,Google Apps腳本(GAS),它使用正確的會話ID創建事件。
但是,我想要在PHP GAE(Google App Engine)中做同樣的事情。所以,我試圖冒充用戶ID如下:Perform Google Apps Domain-Wide Delegation of Authority
我的目標就是像在GAS(谷歌Apps腳本)自動插入任何最終用戶的正確用戶ID。 我不希望任何默認憑據,以「創建人」現場設立..
使用PHP客戶端庫我試圖冒充日曆API.I用戶沒有得到任何完整因此,我嘗試了Drive API文檔sample,此示例似乎工作正常,使用其他域用戶也
使用日曆API我寫了下面的示例。
<?php
require_once realpath(dirname(__FILE__).'/google-api-php-client-master/src/Google/autoload.php');
require_once realpath(dirname(__FILE__).'/google-api-php-client-master/src/Google/Service/Calendar.php');
require_once realpath(dirname(__FILE__).'/google-api-php-client-master/src/Google/Service/Oauth2.php');
use google\appengine\api\users\User;
use google\appengine\api\users\UserService;
$user = UserService::getCurrentUser();
$userEmail=$user->getEmail();
$client_email = '34234242424-qssjf8kg1kk42dnjdv[email protected]';
$private_key = file_get_contents('key.p12');
$scopes = array('https://www.googleapis.com/auth/calendar');
$credentials = new Google_Auth_AssertionCredentials(
$client_email,
$scopes,
$private_key
);
$credentials->sub = $userEmail;
$client = new Google_Client();
$client->setAssertionCredentials($credentials);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion();
}
$service =new Google_Service_Calendar($client);
$event = new Google_Service_Calendar_Event();
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime('2015-09-26T10:00:00.000-07:00');
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime('2015-09-26T10:00:00.000-07:00');
$event->setEnd($end);
$createdEvent = $service->events->insert('[email protected]', $event);
它的工作以及當我檢查了該郵件,我得到了正確的服務帳戶憑據。 但是,當我使用不同的電子郵件ID,我得到這個錯誤以下
PHP Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Error refreshing the OAuth2 token, message: '{ "error" : "unauthorized_client", "error_description" : "Unauthorized client or scope in request." }'' in /base/data/home/apps/s~production/1.3874/google-api-php-client-master/src/Google/Auth/OAuth2.php:363 Stack trace: #0 /base/data/home/apps/s~production/1.387/google-api-php-client-master/src/Google/Auth/OAuth2.php(314): Google_Auth_OAuth2->refreshTokenRequest(Array) #1 /base/data/home/apps/s~ei-html-production/1.3874/final.php(34): Google_Auth_OAuth2->refreshTokenWithAssertion() #2 {main} thrown in /base/data/home/apps/s~production/1.38742/google-api-php-client-master/src/Google/Auth/OAuth2.php on line 363
PLZ幫我,我所做的wrong.Why問題發生了,當我用DIFF電子郵件ID,我的最終用戶,誰就會創建CAL事件,可以是任何GMAIL或Google的域名ID。
所以,PLZ與至少一個樣品部分幫助...
參考鏈接:
How do I Insert Google Calendar Event using Google Api 3 for any user in the domain?
感謝Konqi,我試過它在同一個域用戶中工作。但我想模仿不同的域用戶。可能嗎? – Sattanathan
當域名授權您的應用程序用於域名範圍委派時,您只能模擬域的用戶。如果您想模擬您授權進行全域授權的域中的其他用戶,則必須爲每個用戶創建一個新的Google客戶端。通常情況下,如果您需要授權以用戶身份執行某些操作,則不必使用域範圍委派進行操作,但使用普通OAuth 2.0請求該資源的脫機訪問權限。 – konqi
感謝Konqi的回覆,我的API CONSOLE位於DOMAIN1.COM,最終用戶位於DOMAIN2.COM。使用[email protected]工作正常,但使用[email protected]不工作得到錯誤。 Plz幫助我如何解決這個問題。 – Sattanathan