检测用户是否通过谷歌应用程序市场登录的最简单方法是什么?

时间:2014-11-20 19:53:40

标签: google-oauth google-apps-marketplace

我有一个用户使用google oauth2登录的网络应用程序。

我有一个针对此应用程序的谷歌应用程序市场列表,并且部分谷歌oauth2用户不需要授予我的应用程序权限,因为他们的谷歌应用程序域管理员在安装应用程序市场列表时这样做了。

我希望能够检测到第二组用户,以分析使用应用程序市场列表登录我的应用程序的频率。目前所有谷歌oauth2登录看起来与我的应用程序相同。

我是否可以通过简单的API调用来确定当前用户是否在此组中?

1 个答案:

答案 0 :(得分:1)

我使用此代码查找给定appId和目标域的市场列表信息:

InputStream p12File = Config.class.getResourceAsStream(Config.SERVICE_ACCOUNT_PRIVATE_KEY_RESOURCE_PATH);
PrivateKey serviceAccountPrivateKey = SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(), p12File, "notasecret", "privatekey", "notasecret");

JsonFactory jsonFactory = new JacksonFactory();
HttpTransport t = GoogleNetHttpTransport.newTrustedTransport();

GoogleCredential.Builder bgc = new GoogleCredential.Builder()
            .setTransport(t)
            .setJsonFactory(jsonFactory)
            .setServiceAccountScopes(Collections.singleton("https://www.googleapis.com/auth/appsmarketplace.license"))
            .setServiceAccountPrivateKey(serviceAccountPrivateKey)
            .setServiceAccountId(Config.SERVICE_ACCOUNT_ID);

GoogleCredential gc = bgc.build();
String token = gc.getAccessToken();
if(token == null) {
  gc.refreshToken();
  token = gc.getAccessToken();
}

HttpGet request = new HttpGet("https://www.googleapis.com/appsmarket/v2/customerLicense/" + applicationId + "/" + customerDomain);
request.setHeader("Authorization", "Bearer " + token);

DefaultHttpClient client = new DefaultHttpClient(httpParams);
HttpResponse resp = client.execute(request);
// ... read API JSON response
相关问题