21

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 = "123456789000-abc123def456@developer.gserviceaccount.com";
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.

Chantz
  • 5,883
  • 10
  • 56
  • 79
  • Not entirely sure what you're asking, but you should first follow the steps listed [here](https://developers.google.com/android-publisher/getting_started#using_a_service_account). 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 Aug 17 '15 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 Aug 17 '15 at 17:33
  • @Andy I have updated the question to give more info. – Chantz Aug 17 '15 at 17:35

1 Answers1

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.

Chantz
  • 5,883
  • 10
  • 56
  • 79
  • 9
    Some Google Api needs some scopes you can add the scopes using: credential = credential.createScoped(PredictionScopes.all()); – Benn Sandoval Dec 14 '15 at 04: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 Apr 22 '16 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 Oct 27 '16 at 22:25
  • @Innova I did this for a web application. So my use case is different form apps. – Chantz Nov 03 '16 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 Jul 15 '17 at 02:21
  • Just for the record, on Android you can put the json file to the assets folder, eg: `app/src/main/assets/credentials.json` And then use it with the AssetManager (context can be for example the current activity): ```AssetManager assetManager = context.getAssets(); InputStream jsonStream = assetManager.open("credentials.json");? GoogleCredential credential = GoogleCredential.fromStream(jsonStream); ``` – akos May 01 '18 at 21:35
  • GoogleCredential.fromStream() will work for p12 file or only for json? – Pavel Poley Sep 10 '18 at 08:43
  • Hi @Chantz. I followed your directions, even with loading the JSON file, which appears to work fine, I went through my Admin account and set all of the settings but I am getting a 400 Bad Request when I run my API. Would you mind reviewing my question on SO? https://stackoverflow.com/questions/29327846/gmail-rest-api-400-bad-request-failed-precondition – Peter S Jan 07 '19 at 17:00
  • GoogleCredential.fromStream is deprecated without a named replacement... https://googleapis.dev/java/google-api-client/latest/com/google/api/client/googleapis/auth/oauth2/GoogleCredential.html#fromStream-java.io.InputStream- – Stefan Reich Jan 14 '21 at 01:58