What is the easiest way to detect whether a user is logged in via google apps marketplace?

霸气de小男生 提交于 2020-01-04 19:19:47

问题


I have a web application which users log into using google oauth2.

I have a google apps marketplace listing for this application, and some portion of the google oauth2 users didn't need to grant permission to my application because their google apps domain administrator did it while installing the apps marketplace listing.

I'd like to be able to detect this second group of users, to analyze how frequently the apps marketplace listing is being used to log into my application. At the moment all google oauth2 logins look the same to my application.

Is there a simple API call I can make to find out whether the current user is in this group?


回答1:


I use this code for finding out marketplace listing info for given appId and target domain:

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


来源:https://stackoverflow.com/questions/27048316/what-is-the-easiest-way-to-detect-whether-a-user-is-logged-in-via-google-apps-ma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!