google-api-java-client icon indicating copy to clipboard operation
google-api-java-client copied to clipboard

One can't use GoogleCredential.fromStream() to create a GoogleCredential that has scopes.

Open stephenrobertson0 opened this issue 11 years ago • 9 comments

I created a google service account for accessing Google Shopping, generated a JSON key, then used GoogleCredential.fromStream() to create a GoogleCredential from it. But when I use it I get this response:

Exception in thread "main" com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request { "error": "invalid_scope", "error_description": "Empty or missing scope not allowed." }

So I need to now manually build up my GoogleCredential using the Builder, which is not ideal.

stephenrobertson0 avatar Jul 08 '15 13:07 stephenrobertson0

I just ran into the same issue today, and had to extract various bits of code such as privateKeyFromPkcs8 in order to work around the fact that fromStreamServiceAccount hard-codes scopes to an empty list. It would be nice if we could get an API like builderFromStream or perhaps the scopes list is parsed from the json.

While using a .p12 file works in development, our secrets management systems don't handle binary data, so plain-text is necessary for security reasons, and thus I was following the .json route when I got bitten by this issue.

Using: com.google.api-ads:1.38.0 -- did a search of master and looks like scopes is still hard-coded as empty list.

ccarpita avatar Aug 19 '15 01:08 ccarpita

@stephenrobertson0 @ccarpita I did this yesterday, I was not facing the same issue that you guys seem to face. Here is my snippet,

JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();

HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();

InputStream inputStream = IOUtils.toInputStream(serviceAccountJson);

GoogleCredential credential = GoogleCredential.fromStream(inputStream, httpTransport, jsonFactory);

credential = credential.createScoped(Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER));

AndroidPublisher androidPublisher = new AndroidPublisher(httpTransport, jsonFactory, credential);

This works fine. Of course this is for AndroidPublisher, so I am not sure if its an issue with different API.

tankchintan avatar Aug 19 '15 19:08 tankchintan

In our case, the failure actually occurred within the fromStream stack (which issues a refresh token call), so it was unavoidable. Is it possible that your credentials don't require scoping for some reason? Also are your credentials a service account type or user type?

ccarpita avatar Aug 20 '15 15:08 ccarpita

So I am using at least one scope AndroidPublisherScopes.ANDROIDPUBLISHER. And the credentials are of type service_account.

tankchintan avatar Aug 20 '15 15:08 tankchintan

@ccarpita Try using GoogleCredential.createScoped(scopes)

indriesergiu avatar Sep 19 '16 10:09 indriesergiu

This issue may no longer be relevant due to its age. Feel free to re-open.

mattwhisenhunt avatar Oct 11 '17 22:10 mattwhisenhunt

@mattwhisenhunt @indriesergiu what is scopes here? where is list of it? where is examples? still have same issue

qwertukg avatar Nov 16 '17 08:11 qwertukg

If you are building an app that lets your users connect to their own Google services they need to grant your app permission. The set of scopes your app requests will appear to your users as a list of permissions they say yes to the first time they use your app; in your app, the scopes will be a list of URLs (or consts) near the top of your code that you pass into the client constructor.

Here is a list of potential scopes: https://developers.google.com/identity/protocols/googlescopes

In the Gmail Quickstart the only scope used is GmailScopes.GMAIL_LABELS.

mattwhisenhunt avatar Feb 02 '18 16:02 mattwhisenhunt

GoogleCredential.createScoped(...) worked for me to add scopes to a service account JSON credential. In many of our cases, we do not want a manual OAuth flow for wholly owned resources. Also, it doesn't seem like the gcloud interface allows us to generate P12 keys anymore... the option is there but greyed out.

booi avatar Nov 01 '18 18:11 booi