使用服务帐户OAuth2消耗customerLicense App Marketplace时出错 [英] Error consuming customerLicense App Marketplace with Service Account OAuth2

本文介绍了使用服务帐户OAuth2消耗customerLicense App Marketplace时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想出了解决这个问题的方法.

I figured out how to solve this problem.

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

First of all here is my implementation with Service Account:

// 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();

此许可证生成器对象是我自己基于新google-api-client 1.17及更高版本的实现.如果有人可以建议我如何与社区的其他人分享我会很高兴的.

This License Builder object is myself implementation based on the new google-api-client 1.17 and above. If someone could advice me how can i share with the rest of the community i will be glad to do it.

最好

我发布了另一个线程,带有OAuth2的Google Apps Marketplace API customerLicense a>,说明我打算通过OAuth2服务帐户策略使用此API.

I have posted another thread, Google Apps Marketplace API customerLicense with OAuth2, explaining about my intentions to consume this API with OAuth2 Service Account strategy.

我尝试了所有存在的方法和官员库,并且总是收到无效的OAuth标头消息或未经许可的

I have tried every method and officials library present and I always get Invalid OAuth header message or UNLICENSED

我将详细说明风景和尝试过的事情:

I am going to detail what is the scenery and what things i have tried:

  1. 我和Google App Marketplace都使用服务帐户OAuth2,因为所有任务都在后台执行.
  2. 此API项目也具有服务帐户密钥和客户端Web帐户密钥.
  3. 我发布的应用仅在我的域内受限制,原因是我仍在开发中.因此,我为我的域安装了App.
  4. 在这一点上,假设我要查询带有API项目ID和客户ID(即域名)的客户许可证,那么我必须查看我的域的APP许可证.
  5. 我已经使用了这个罐子 https://developers.google.com/google-apps/marketplace/v2/developers_guide 来访问许可证API.
  6. 这是我的代码:

  1. I have and Google App Marketplace which use Service Account OAuth2 because all task are being perform on background.
  2. This API Project has Service Account keys and Client Web Account keys too.
  3. I published app restricted for my domain only because i am yet developing. So I installed App for my domain.
  4. At this point it is suppose if I queried Customer License with API Project ID and Customer Id, which is domain name, I have to see the APP LICENSE for my domain.
  5. I have used this jars https://developers.google.com/google-apps/marketplace/v2/developers_guide to access License API.
  6. This is my code:

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();

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();

如果我使用此代码,则会被禁止403.

I get 403 forbidden if i use this code.

我的问题是:

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

如果有人可以帮助我,那就太好了,因为我们正在尝试根据Google的要求将7个Google App旧市场应用从OAuth1迁移到OAuth2,但是如果我们无法查询,我们的实现就会遇到一些漏洞我们的应用程序安装在哪些域中.

It would be great if someone can help me because we are trying to migrate our 7 Google App Old Marketplace Apps from OAuth1 to OAuth2 as per Google request but we have some black holes in our implementation if we would not be able to query what domains have our App Installed.

最好

推荐答案

除OAuth2库外,不需要任何其他库.您可以使用urlfetch来实现此目的.

There is no need for any other libraries than OAuth2 lib. You can impmement this using 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>添加Google Apis.

You need to install the OAuth2 package for this to work. In eclipse its under Google > Add Google Apis.

这篇关于使用服务帐户OAuth2消耗customerLicense App Marketplace时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆