我试图从我自己的CMS创建一个自动发布到Google plus的帖子。我试过Google Plus Domain API。但是我总是得到Forbidden log,虽然我已经放置了正确的范围,并且我已经设置了我的Credential的权限。
这是我的代码
@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";
}
这是日志
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)
答案 0 :(得分:0)
在自动发布到G +用户的墙上时,我也遇到了同样的问题。
此解决方案中需要注意的几点是:
作为参考我已经创建了一个公共要点。 在gist的例子中,我使用的是php,google-api-php-client,yii2。如果解决它有任何问题,请评论。将尽力帮助
P.S。成功实施和测试