Connecting to the Google Sites API as a “Service Account”

与世无争的帅哥 提交于 2019-12-07 23:21:54

问题


I am trying to read a feed from a Google Sites account (Google apps). I don't need my app to require every user to login so i created my ClientID as a "Service Account" in the "Google API console". I have added this Client ID and the scope (https://sites.google.com/feeds/) to the "Mange API client access" page in my google apps control panel.

I connect using the code below, all constants are defined in my code with the right values.

// 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>";

The response i get is "Not authorized to access this feed " When i try to get this feed in the OAuth2.0 playground logging in using my google apps account i get the expected response. What am i overlooking here?


回答1:


Service accounts and Google Sites can't be used together currently. Google Apps provides a consumer secret that can be used to access data across your domain as Two-Legged OAuth within OAuth 1.0a.

Check out http://support.google.com/a/bin/answer.py?hl=en&answer=162105 for how to configure your Apps account, and the sample code at https://developers.google.com/gdata/docs/auth/oauth#2LeggedOAuth.



来源:https://stackoverflow.com/questions/13009120/connecting-to-the-google-sites-api-as-a-service-account

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!