1

I was trying to create an automatic post to Google plus from my own CMS. I've tried Google Plus Domain API. But I always get Forbidden log although I've put the right scope, and I've set the permission on my Credential.

Here is my code

@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";
}

And here is the log

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)

I've read this, this, and this. But their case is on google Plus page, whereas mine is on a normal User. Do you have any idea where I've done wrong?

Community
  • 1
  • 1
Kelimutu
  • 11
  • 2

1 Answers1

0

i also had same issue while Auto Posting to G+ user's wall.

a few points to note in this solution are:

  1. remove the permissions granted earlier, visit accounts.google.com
  2. scope request - Google_Service_Plus::PLUS_ME, \Google_Service_PlusDomains::PLUS_STREAM_WRITE
  3. i used Guzzle to request to GooglePlusDomain's REST API to auto post

for reference i have created a public gist. in the example of gist, i am using php, google-api-php-client, yii2. if any issue in solving it, comment. will try to help

P.S. succesfully implemented and tested

Gunnrryy
  • 350
  • 1
  • 5
  • 17