使用Google Oauth2凭据的“服务帐户”

时间:2015-09-28 05:28:25

标签: php google-app-engine google-calendar-api google-api-php-client service-accounts

我正在尝试使用PHP GAE中的Google Api OAuth服务帐户创建Google日历活动。

我需要在日历事件字段'由'创建的会话ID 用户电子邮件ID 。 这在 Google Apps脚本(GAS)中很有用,它会使用正确的会话ID创建事件。

但是,我希望在 PHP GAE(Google App Engine)中完成同样的工作。因此,我尝试按如下方式模拟用户ID:Perform Google Apps Domain-Wide Delegation of Authority

我的目标是自动插入任何最终用户的正确用户ID,就像在 GAS(Google Apps脚本)中一样。 我不希望在'由'创建'字段中设置任何默认凭据。

使用 PHP客户端库我尝试在Calendar API中模拟用户。我没有获得Calendar API的任何完整示例。所以,我尝试了Drive API文档{{3此示例似乎工作正常,使用其他域用户

使用Calendar 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-qssjf8kg1kk42dnjdvd2ndrk7rou44@developer.gserviceaccount.com';
    $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('abc@group.calendar.google.com', $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帮助我做错了。为什么问题发生时,当我使用差异电子邮件ID时,我的最终用户将创建cal事件,可以是任何GMAIL或基于Google的域ID。

所以,plz至少帮助了一个样本部分...

参考链接:

sample

How do I Insert Google Calendar Event using Google Api 3 for any user in the domain?

1 个答案:

答案 0 :(得分:1)

您只能在拥有必要权限的Google域应用域中模拟用户(搜索“域范围授权”)。你已经有了这个。

一旦您获得模拟授权,您必须为您冒充的每个用户重新创建一个全新的Google客户端。您只会收到一个access_token,您可以为此用户使用。如果您只是在获得授权后更改了电子邮件地址,则无法使用。

相关问题