【问题标题】:Error consuming customerLicense App Marketplace with Service Account OAuth2使用服务帐户 OAuth2 使用 customerLicense App Marketplace 时出错
【发布时间】:2014-02-20 01:02:32
【问题描述】:

解决方案

我想出了如何解决这个问题。

首先是我使用服务帐户的实现:

// Build service account credential.
    GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
        .setJsonFactory(JSON_FACTORY)
        .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
        .setServiceAccountScopes(Collections.singleton("https://www.googleapis.com/auth/appsmarketplace.license"))
        .setServiceAccountPrivateKeyFromP12File(new File("/path/to/mykey/key.p12"))
//            .setServiceAccountUser("NOT SET THIS LINE")
        .build();

        License build = new License.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName("My Application").build();
    Licenses execute = build.customerLicense().get("9999999999", "domain.test.com").execute();

这个 License Builder 对象是我自己基于新的 google-api-client 1.17 及更高版本实现的。如果有人可以建议我如何与社区的其他人分享,我会很乐意这样做。

最好的,


我发布了另一个线程 Google Apps Marketplace API customerLicense with OAuth2,解释了我打算使用 OAuth2 服务帐户策略使用此 API。

我已经尝试了所有现有的方法和官方图书馆,但我总是收到 Invalid OAuth header 消息或 UNLICENSED

我将详细介绍什么是风景以及我尝试过的事情:

  1. 我有和 Google App Marketplace 使用服务帐户 OAuth2,因为所有任务都在后台执行。
  2. 此 API 项目也有服务帐户密钥和客户端 Web 帐户密钥。
  3. 我发布应用受限于我的域只是因为我还在开发中。所以我为我的域安装了应用程序。
  4. 此时假设如果我使用 API 项目 ID 和客户 ID(即域名)查询客户许可证,我必须查看我的域的 APP LICENSE。
  5. 我已经使用这个 jars https://developers.google.com/google-apps/marketplace/v2/developers_guide 来访问许可证 API。
  6. 这是我的代码:

    String appName = "MY APP"; AppsMarketService service = new AppsMarketService(); service.appId = "NUMBER_APP_ID"; service.appName = appName; service.endpoint = "https://www.googleapis.com/appsmarket/v2/"; service.consumerKey = service.appId + ".apps.googleusercontent.com"; service.consumerSecret = "CLIENT_SECRET_FROM_WEB_OAUTH2_API_PROJECT"; service.authorize();

  7. 如果我使用此代码,我会得到 403 禁止。

  8. 如果我从我的 API 项目 web OAuth2 将 appId 更改为前缀 clientId,我会得到 200,但正文是 UNLICENSED。
  9. 我已将范围添加到我的应用程序 https://www.googleapis.com/auth/appsmarketplace.license,但我仍然得到相同的结果。
  10. 我还尝试通过服务帐户握手从管理员用户获取访问令牌,然后使用该 Oauth2 访问令牌访问 API 许可证和相同的结果 无效的 OAuth 令牌

我的问题是:

  1. 考虑到服务帐户密钥中没有消费者秘密,只有客户端 ID 和私钥文件,是否有任何方法可以使用服务帐户密钥访问此 API?
  2. 是否有任何更新的库可以将其与 OAuth2 一起使用,因为我看到所有这些库都使用带有两条腿身份验证的 OAuth1?

如果有人可以帮助我,那就太好了,因为我们正在尝试根据 Google 请求将我们的 7 个 Google App Old Marketplace Apps 从 OAuth1 迁移到 OAuth2,但是如果我们无法查询,我们的实施中会出现一些黑洞我们的应用安装了哪些域。

最好的,

【问题讨论】:

  • 我终于设法仅使用服务帐户密钥来使用许可证 API。 Java API 中有一些棘手的事情,我只能使用 Python 库来解决。对不起 Java 人,但在其他语言中,一些实现更好、更完整。我使用新的 google-api-client-1.17.0 实现了许可 API,我想与其他人分享。我可以在哪里上传?

标签: google-apps google-oauth google-api-java-client google-api-client google-apps-marketplace


【解决方案1】:

除了 OAuth2 库之外,不需要任何其他库。您可以使用 urlfetch 来实现这一点。

...
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.appengine.api.urlfetch.FetchOptions.Builder;
import com.google.appengine.api.urlfetch.HTTPHeader;
import com.google.appengine.api.urlfetch.HTTPMethod;
import com.google.appengine.api.urlfetch.HTTPRequest;
import com.google.appengine.api.urlfetch.HTTPResponse;
import com.google.appengine.api.urlfetch.URLFetchServiceFactory;

...

String SERVICE_ACCOUNT_EMAIL = "...@developer.gserviceaccount.com";
String P12 = "...-privatekey.p12";
// appid is the same id that you have in the google cloud project that has the Google Apps Marketplace API and SDK enabled
String appid = "";

GoogleCredential credential = new GoogleCredential.Builder()
  .setTransport(new NetHttpTransport())
  .setJsonFactory(new JacksonFactory())
  .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
  .setServiceAccountScopes(Collections.singleton("https://www.googleapis.com/auth/appsmarketplace.license"))
  .setServiceAccountPrivateKeyFromP12File(new File(P12))
  .build();

credential.refreshToken();
String token = credential.getAccessToken();

URL url = new URL("https://www.googleapis.com/appsmarket/v2/licenseNotification/"+appid);

HTTPRequest request = new HTTPRequest(url, HTTPMethod.GET, Builder.allowTruncate());
request.setHeader(new HTTPHeader("Authorization", "Bearer "+token));

HTTPResponse response = URLFetchServiceFactory.getURLFetchService().fetch(request);

您需要安装 OAuth2 软件包才能使其正常工作。在 eclipse 下的 Google > Add Google APIs 下。

【讨论】:

  • 我不是初级程序员。事实上,如果我们要严格一点,也不需要 URL Fetch,它可以用周围的任何 HTTP 库来实现。当我指的是库时,我指的是包装器响应对象表示,例如 google-api-services-admin-directory 或任何其他 Google 官方包装器。
猜你喜欢
  • 2015-09-19
  • 2014-01-07
  • 1970-01-01
  • 1970-01-01
  • 2012-10-31
  • 1970-01-01
  • 2019-01-23
  • 2017-01-23
  • 2018-09-19
相关资源
最近更新 更多