以“服务帐户”身份连接到Google协作平台API

时间:2012-10-22 10:07:37

标签: php google-api oauth-2.0 google-sites google-oauth

我正在尝试从Google协作平台帐户(Google应用)中读取Feed。 我不需要我的应用程序要求每个用户登录,因此我在“Google API控制台”中将我的ClientID创建为“服务帐户”。 我已将此客户ID和范围(https://sites.google.com/feeds/)添加到我的Google Apps控制面板中的“Mange API客户端访问权限”页面。

我使用下面的代码连接,所有常量都在我的代码中用正确的值定义。

// api dependencies
require_once(GOOGLE_API_PATH);

// create client object and set app name
$client = new Google_Client();
$client->setApplicationName(GOOGLE_API_NAME);

// set assertion credentials
$client->setAssertionCredentials(

new Google_AssertionCredentials(

GOOGLE_API_EMAIL,
array(GOOGLE_API_SCOPE),
file_get_contents(GOOGLE_API_PK) 
));

$client->setClientId(GOOGLE_API_CLIENTID);

// create service

$req = new Google_HttpRequest("https://sites.google.com/feeds/content/<herismydomainname.com>/intranet");
$val = $client->getIo()->authenticatedRequest($req);

// The contacts api only returns XML responses.
$response = json_encode($val->getResponseBody());
print "<pre>" . print_r(json_decode($response, true), true) . "</pre>";

我得到的回复是“未授权访问此Feed” 当我尝试在使用我的谷歌应用程序帐户登录的OAuth2.0游戏中获取此订阅源时,我得到预期的响应。 我在这里俯瞰什么?

1 个答案:

答案 0 :(得分:5)

目前无法一起使用服务帐户和Google协作平台。 Google Apps提供了一个消费者密钥,可用于访问您域中的数据,如OAuth 1.0a中的双腿OAuth。

查看http://support.google.com/a/bin/answer.py?hl=en&answer=162105了解如何配置您的Apps帐户,以及https://developers.google.com/gdata/docs/auth/oauth#2LeggedOAuth上的示例代码。