java - Google Apps 电子邮件设置 API 和 oauth2.0 凭据以及服务帐户

标签 java google-app-engine oauth google-api google-apps

我尝试在 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/

相关文章:

python - 谷歌应用引擎、云 sql 和 django : no rdbms backend module

ruby-on-rails - 使用 OAuth 密码凭证授予优于 token 访问身份验证的优势

java - Jetty应用服务器热部署

java - 应用程序从用户那里获取二进制输入,并在 Android Studio 中按下按钮时闪烁手电筒传输输入

google-app-engine - 缓慢加载托管在 GAE 上的静态文件

google-app-engine - 在 app.yaml 中混合脚本和静态

java - 服务器端访问/刷新 token 中的 Oauth2

ruby-on-rails - Rails、OAuth 和 CSRF 保护

java - 如何计算多列(colspan)HTML 表格的宽度?

java - 如何向 XML 元素添加属性