What is the easiest way to detect whether a user i

2019-07-19 06:54发布

问题:

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