One can't use GoogleCredential.fromStream() to create a GoogleCredential that has scopes.
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.
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.
@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.
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?
So I am using at least one scope AndroidPublisherScopes.ANDROIDPUBLISHER. And the credentials are of type service_account.
@ccarpita Try using GoogleCredential.createScoped(scopes)
This issue may no longer be relevant due to its age. Feel free to re-open.
@mattwhisenhunt @indriesergiu what is scopes here? where is list of it? where is examples? still have same issue
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.
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.