【问题标题】:Google Apps Email Settings API and oauth2.0 Credential with service account带有服务帐户的 Google Apps 电子邮件设置 API 和 oauth2.0 凭据
【发布时间】:2014-04-30 16:18:28
【问题描述】:

我正在尝试在 java 中使用 Oauth 2.0 Google 凭据和服务帐户来编辑用户签名,但我收到了该错误:

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 而不是像这段代码中那样使用管理员的 unsername 和 password 吗?

https://developers.google.com/admin-sdk/email-settings/#manage_signature_settings

为什么没有一个官方的 gmail 设置服务,它不是用于示例的 appsforyoudomainservice?Oauth 2.0 与它支持的服务帐户?

谢谢

【问题讨论】:

  • 一个服务帐号将只允许您访问该服务帐号的帐号。我不明白您为什么要让每个人都更改您的电子邮件签名。您确定不应该使用正常的 Oauth2 流程吗?

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


【解决方案1】:

您已授予服务帐号代表您的用户行事的权利。现在服务帐户需要充当您域中的用户,有权为所有用户执行签名更新,换句话说,它需要充当超级管理员。尝试添加:

.setServiceAccountUser(userEmail)

其中 userEmail 是您的 Google Apps 实例中超级管理员的电子邮件地址。一个good walkthrough for this is in the Admin SDK documentation

【讨论】:

  • 我的印象是 setServiceAccountUser(过去也是)用于插入您想在您的域中模拟的用户,而不是管理员电子邮件。看那里。 code.google.com/p/google-api-java-client/wiki/…。如果我需要设置一个管理员帐户,我仍然需要至少在可以更改的谷歌应用域中找到用户超级管理员。我不能直接放管理员帐户的字符串。
  • 奇怪。我很确定,它适用于不是超级管理员的用户。现在,如果我想列出域中任何用户的代表,我必须提供超级管理员帐户作为 ServiceAccountUser。
猜你喜欢
  • 2017-03-13
  • 2014-03-10
  • 2021-11-17
  • 1970-01-01
  • 2015-04-03
  • 2015-07-20
  • 2016-06-08
  • 2017-07-27
  • 1970-01-01
相关资源
最近更新 更多