2

解决方案

我想出了如何解决此问题。使用服务帐户使用customerLicense应用程序市场时出错OAuth2

的一切都在这里首先是我的执行与服务帐户:

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

本许可证的Builder对象是自己基于全新谷歌的API客户端1.17及以上的实施。如果有人能够指导我,我如何与社区其他人分享,我会很乐意去做。

最佳,


我已经发布了另一个线程,Google Apps Marketplace API customerLicense with OAuth2,解释我的意图与消费的OAuth2服务帐户策略,这个API。

我都想尽方法和官员图书馆目前,我总是得到无效的OAuth头消息或非授权

我要详细说一下的是风景,什么事情我都试过:

  1. 我有和Google App Marketplace使用服务帐户OAuth2,因为所有任务都在后台执行。
  2. 此API项目也有服务帐户密钥和客户端Web帐户密钥。
  3. 我发布的应用程序仅限于我的域名,因为我尚未开发。所以我为我的域安装了应用程序。
  4. 在这一点上,假设我是通过API项目ID和客户ID(它是域名)来查询客户许可证的,我必须查看我的域的APP许可证。
  5. 我已使用此罐子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更改了前缀clientId的appId,我得到了200但是body UNLICENSED。
  9. 我已将范围添加到我的应用程序https://www.googleapis.com/auth/appsmarketplace.license,而且我仍得到相同的结果。
  10. 我曾尝试也从服务帐户握手管理员用户获得访问令牌,然后用它的OAuth2访问令牌访问API许可和相同的结果无效的OAuth令牌

我的问题是:

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

这将是巨大的,如果有人能帮助我,因为我们正试图给我们7个谷歌应用程序旧市场应用从您好!OAuth1迁移到OAuth2按谷歌要求,但我们已经在执行一些黑洞,如果我们也不会能够查询哪些域有我们的应用程序安装。

最好,

+0

我终于设法只使用服务帐号密钥来使用授权API。 Java API中存在一些棘手的问题,我只能使用Python库进行研究。对不起,Java的人,但在其他语言的一些实现更好,更完整。我使用新的google-api-client-1.17.0制作了许可证API的实施,我想与其他人分享。我可以在哪上传? – jproyo

回答

0

除了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 = "[email protected]"; 
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下。

+1

我不是初级程序员。事实上,如果我们要严格,不需要URL提取,它可以用任何HTTP库实现。当我指图书馆时,我正在谈论的包装响应对象表示,如谷歌API服务管理目录或任何其他谷歌官方包装。 – jproyo

相关问题