2013-11-26 5 views
0

서비스 계정을 사용하여 관리 사용자로 가장하여 관리 디렉토리 및 Google+ 도메인 API를 사용하고 있지만 사이트에서 서비스 계정을 사용할 수 있는지 파악할 수 없습니다. 심지어 가능할까요? 나는 콘텐츠를 관리하는 웹 마스터 도구 API가 아닌 사이트를 만들고 삭제할 수있는 API를 언급하고 있습니다.사이트 API에 Google 서비스 계정을 사용할 수 있음

답변

0

예는 수 있습니다 : 여기

는 코드입니다. 다음 코드 예제 :

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("[email protected]").build(); 
sitesService.setOAuth2Credentials(credential); 

당신이주의해야 할 유일한 것은, 일부 SitesService 방법이 제대로 토큰을 새로 고침하지 못할 수있는 경우에 당신이 SessionExpiredException을 잡을해야하고 새로 고침 것입니다 Credential.

0

Google 사이트 도구 API 페이지에는 [새 사이트 만들기 또는 기존 사이트 복사] 기능이 있습니다.

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

+0

예, 이미 자바 버전을 사용하여 해당 API가 포함 된 사이트를 만들고 있지만 로그인 할 때 사용자/패스 조합을 사용하며 디렉토리를 사용하여 사용자를 가장하기 위해 서비스 계정을 사용하고 싶습니다. , Google+ 또는 캘린더 API – momo

1

당신은 사이트 API와 서비스 계정을 사용할 수 있습니다. SignedJwtAssertionCredentials를 사용하여 백그라운드에서 사용자를 가장하고 GData API에 액세스하기 만하면됩니다.

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)