我尝试在 java 中使用 Oauth 2.0 Google Credential 和服务帐户来编辑用户签名,但收到该错误:
com.google.gdata.util.ServiceForbiddenException: OK
<HTML><HEAD><TITLE>You are not authorized to access this API.</TITLE></HEAD><BODY BGCOLOR="#FFFFFF" TEXT="#000000"><H1>You are not authorized to access this API.</H1><H2>Error 403</H2></BODY></HTML>
我创建了一段代码,以便在此模式下将 GoogleCredential 对象与服务帐户电子邮件一起使用:
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
Collection<String> clist= new ArrayList<String>(
Arrays.asList("https://apps-apis.google.com/a/feeds/emailsettings/2.0/", "https://www.googleapis.com/auth/admin.directory.user"
));
GoogleCredential gc=null;
try {
gc = new GoogleCredential.Builder().setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(c.getServiceAccountEmail())
.setServiceAccountScopes(clist)
.setServiceAccountPrivateKeyFromP12File(new java.io.File(c.getPkcs12FileName()))
.build();
} catch (GeneralSecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.setOAuth2Credentials(gc);
我已在域 Google Apps for Business 面板中插入服务帐户的范围,并在控制台项目中启用了 Admin sdk API。
为什么我收到消息说我没有获得授权? 我可以使用 Oauth 2.0 而不是像此代码中那样使用管理员的用户名和密码?
https://developers.google.com/admin-sdk/email-settings/#manage_signature_settings
为什么没有一个官方的 Gmail 设置服务不是用于示例的 appsforyoudomainservice?Oauth 2.0 及其受支持的服务帐户?
谢谢
最佳答案
您已授予服务帐户代表您的用户行事的权利。现在,服务帐户需要充当您域中的用户,有权为所有用户执行签名更新,换句话说,它需要充当 super 管理员。尝试添加:
.setServiceAccountUser(userEmail)
其中 userEmail 是您的 Google Apps 实例中 super 管理员的电子邮件地址。一个good walkthrough for this is in the Admin SDK documentation .
关于java - Google Apps 电子邮件设置 API 和 oauth2.0 凭据以及服务帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22590255/