23

I am using the Java SDK for Android Publisher v2 and Oauth2 v2. Once I created the service account I was provided with a JSON of Google Play Android Developer service account with client id, email, private key etc. I have tried to look around and figure out how to create a Credential so that I use the AndoirdPublisher service to fetch info on my Android App's users subscriptions, entitlements etc and store this info on our backend servers.

I am getting hard time trying to figure out how to go about this. None of the documentations I have seen so far help in creating GoogleCredential using the downloaded JSON.

For example there is this documentation but it mentions only about P12 file and not the JSON. I want to avoid P12 if possible since I have multiple clients & I would like to save this JSON in some sort of database & then use it to create credential for each client.

Just to clarify I am trying to create the GoogleCredential object as described here,

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.sqladmin.SQLAdminScopes;

// ...

String emailAddress = "[email protected]";
JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential credential = new GoogleCredential.Builder()
    .setTransport(httpTransport)
    .setJsonFactory(JSON_FACTORY)
    .setServiceAccountId(emailAddress)
    .setServiceAccountPrivateKeyFromP12File(new File("MyProject.p12"))
    .setServiceAccountScopes(Collections.singleton(SQLAdminScopes.SQLSERVICE_ADMIN))
    .build();

But instead of setting the Service Account using P12 File like so setServiceAccountPrivateKeyFromP12File(), I want to use the JSON that carries the same info & generated when I created the service account.

3
  • Not entirely sure what you're asking, but you should first follow the steps listed here. It'll tell you how to create a service account if you'd rather use that. To make sure you can actually pull information some things to check are, have Google Play Android Developer API enabled, link your project with your Google Play publishing account, and grant the necessary permissions to your service account in the Google Play Store console.
    – Andy
    Commented Aug 17, 2015 at 16:15
  • @Andy Thanks. Yeah I already have created a service account & want to use the JSON that was generated for it (instead of the P12 file) for creating the GoogleCredential object.
    – Chantz
    Commented Aug 17, 2015 at 17:33
  • @Andy I have updated the question to give more info.
    – Chantz
    Commented Aug 17, 2015 at 17:35

1 Answer 1

32

I was able to solve this by digging in to the source code for GoogleCredential itself on github (which as of now resides here).

Turns out the version of google-api-java-client that was existing in my code was slightly dated. I upgraded it to the latest one 1.20.0 & I am able to now directly specify the JSON object created to create the GoogleCredential. I wish Google would have updated their documentation.

Here is the code snippet,

InputStream resourceAsStream = AuthTest.class.getClassLoader().getResourceAsStream("Google-Play-Android-Developer.json");

GoogleCredential credential = GoogleCredential.fromStream(resourceAsStream);

That simple.

9
  • 9
    Some Google Api needs some scopes you can add the scopes using: credential = credential.createScoped(PredictionScopes.all()); Commented Dec 14, 2015 at 4:37
  • What are the security risks of putting your JSON key into your apk? Seems like a bad idea....but I don't have a better solution.
    – Innova
    Commented Apr 22, 2016 at 19:41
  • @Chantz Where did you put your Google-Play-Android-Developer.json file? I put it into inside app folder but it giving me null.
    – Amit Pal
    Commented Oct 27, 2016 at 22:25
  • @Innova I did this for a web application. So my use case is different form apps.
    – Chantz
    Commented Nov 3, 2016 at 21:06
  • 1
    I tried the credential = credential.createScoped(PredictionScopes.all()). But it does work when I am trying to impersonate someone in my GSuite domain.
    – TerNovi
    Commented Jul 15, 2017 at 2:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.