2016-08-30 25 views
1

私は自分のCMSからGoogleプラスの自動投稿を作成しようとしていました。私はGoogle Plus Domain APIを試しました。しかし、私は正しいスコープを入れていますが、私は常にForbiddenログを取得し、私は自分のCredentialの権限を設定しました。googleプラスドメインAPIを使用して新しい投稿を作成

は、ここに私のコード

@RequestMapping(value = { "/callback" }, method = RequestMethod.GET) 
public String gplusCallback (
     @RequestParam(value = "code", required = false) String code, 
     WebRequest webRequest, 
     HttpServletRequest request, 
     HttpServletResponse response, 
     HttpSession session) throws GeneralSecurityException, IOException { 

    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
      new NetHttpTransport(), 
      new JacksonFactory(), 
      CLIENT_ID, 
      CLIENT_SECRET, 
      SCOPE) 
      .setApprovalPrompt("force") 
      .setAccessType("offline").build(); 

    System.out.println("Codenya " + code); 
    GoogleTokenResponse tokenResponse = flow.newTokenRequest(code) 
      .setRedirectUri(REDIRECT_URI).execute(); 
    GoogleCredential credential = new GoogleCredential.Builder() 
     .setTransport(new NetHttpTransport()) 
     .setJsonFactory(new JacksonFactory()) 
      .setServiceAccountId(SERVICE_ACCOUNT_EMAIL) 
      .setServiceAccountScopes(SCOPE) 
      .setServiceAccountUser(USER_EMAIL) 
      .setServiceAccountPrivateKeyFromP12File(
       new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH)) 
      .build(); 


    Plus service = new Plus.Builder(new NetHttpTransport(), new JacksonFactory(), credential) 
      .setApplicationName("GNEWS.ID") 
      .build(); 
    com.google.api.services.plus.model.Person mePerson = service.people().get("me").execute(); 

    System.out.println("ID:\t" + mePerson.getId()); 
    System.out.println("Display Name:\t" + mePerson.getDisplayName()); 
    System.out.println("Image URL:\t" + mePerson.getImage().getUrl()); 
    System.out.println("Profile URL:\t" + mePerson.getUrl()); 

    try { 
     PlusDomains plusDomains = new PlusDomains.Builder(new NetHttpTransport(), new JacksonFactory(), credential) 
       .setApplicationName("GNEWS.ID") 
       .build(); 

     com.google.api.services.plusDomains.model.Person me = plusDomains.people().get(mePerson.getId()).execute(); 
     System.out.println("ID:\t" + me.getId()); 
     System.out.println("Profile URL:\t" + me.getUrl()); 
    }catch (IOException e) { 
     e.printStackTrace(); 
    } 

    System.out.println("G+ has been connected!"); 
    return "redirect:/cms/connection"; 
} 

されており、ここで私はthisthis、およびthisを読んだ

com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden { 


"code" : 403, 
    "errors" : [ { 
    "domain" : "global", 
    "message" : "Forbidden", 
    "reason" : "forbidden" 
    } ], 
    "message" : "Forbidden" 
} 
    at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:146) 
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113) 
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40) 
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321) 
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1065) 
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419) 
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352) 
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469) 
    at io.gnews.cms.controller.GplusController.gplusCallback(GplusController.java:140) 

ログです。しかし、彼らのケースはgoogleプラスページにありますが、私の場合は通常のユーザーです。私が間違って行ったことはありますか?

+0

Googleアカウントの電子メールアドレスが 'gmail.com 'で終わる場合、動作しません。 Domains APIは、カスタムドメインを使用するhttps://apps.google.com/アカウントでのみ機能します。 – abraham

+0

Googleプラスページに使用するとうまくいくと思いますか? @アブラハム? – Kelimutu

+1

ああ、心配しないでください。今私はグーグルプラスページの別のAPIを使用して知っている。 – Kelimutu

答えて

0

G +ユーザーの壁に自動投稿する際にも同じ問題が発生しました。

この溶液中で注目すべきいくつかのポイントは以下のとおりです。

  1. 、先に付与された権限を削除
  2. スコープ要求accounts.google.com訪問 - Google_Service_PlusDomains Google_Service_Plus :: PLUS_MEを、\ :: PLUS_STREAM_WRITE
  3. 私はGuzzleを使ってGooglePlusDomainのREST APIに自動投稿をリクエストしました

参考のために私は公の要点を作成しました。 gistの例では、私はphp、google-api-php-client、yii2を使用しています。それを解決する上で問題があれば、コメントしてください。助けようとするでしょう

P.S.正常に実装され、テストされました

関連する問題