I am using a service account to impersonate an admin user successfully with the Admin Directory and Google+ Domain APIs, but I am unable to figure out if Sites can make use of service accounts. Is it even possible? I am referring to the API that allows you to create and delete sites, not the Webmaster Tools API for managing the content and so on.

有帮助吗?

解决方案 2

Yes it is possible. Here's a code example :

SitesService sitesService = new SitesService("MyApplication");
final GoogleCredential credential = new GoogleCredential.Builder()
                .setTransport(new NetHttpTransport()).setJsonFactory(new JacksonFactory())
                .setServiceAccountId("XXXX.apps.googleusercontent.com")
                .setServiceAccountScopes(Collections.singleton(SERVICE_SCOPES))
                .setServiceAccountPrivateKeyFromP12File(new File("key.p12"))
                .setServiceAccountUser("mysiteowner@domain.com").build();
sitesService.setOAuth2Credentials(credential);

The only thing you have to be cautious with, is that some SitesService methods might not be able to refresh the token properly, in which case you will have to catch the SessionExpiredException and refresh the Credential yourself.

其他提示

You can use Service Accounts with Sites API. All you need to do is use SignedJwtAssertionCredentials, impersonate the user from background and access the GData APIs.

Here is the code snippet:

credentials = SignedJwtAssertionCredentials(
      SERVICE_ACCOUNT_EMAIL,
      file(SERVICE_ACCOUNT_PEM_FILE_PATH, "rb").read(),
      scope=["https://sites.google.com/feeds/"],
      prn=userEmailYouWantToImpersonate
    )

http = httplib2.Http()
http = credentials.authorize(http)

sitesClient = gdata.sites.client.SitesClient(source='mycompany', site='mySite', domain='mydomain')
sitesClient.auth_token = TokenFromOAuth2Creds(credentials)
feed = sitesClient.GetContentFeed()

class TokenFromOAuth2Creds:
    def __init__(self, creds):
        self.creds = creds
    def modify_request(self, req):
        if self.creds.access_token_expired or not self.creds.access_token:
            self.creds.refresh(httplib2.Http())
        self.creds.apply(req.headers)

Google Sites API pages says [1] that you can "Create new Sites or copy existing Sites" with it.

[1] https://developers.google.com/google-apps/sites

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top