This has been a massively frustrating experience. I am trying to access Google Cloud Storage (list a bucket accessibe to anyone) and do it from Android via a nonservice account.
Here is my code:
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.InputStreamContent;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.storage.Storage;
import com.google.api.services.storage.model.Bucket;
import com.google.api.services.storage.model.StorageObject;
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
Credential credential = new GoogleCredential.Builder()
.setClientSecrets("ANDROID_CLIENT_ID_FROM_DEV_CONSOLE", "some random string, since no clienet secret for android")
.setJsonFactory(jsonFactory)
.setTransport(httpTransport)
.build();
credential.setAccessToken(token);
Storage storage = new Storage.Builder(httpTransport, jsonFactory,
credential)
.setApplicationName("My app name")
.build();
List<StorageObject> objects = storage.objects().list("my-bucket").execute().getItems()
Considerations:
I get a valid oauth token which has the following scopes: "oauth2:https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/devstorage.read_write"
I have Google Cloud Storage JSON API and Google Cloud Storage enabled in the API section of my Cloud Dev Console.
As an alternative, I tried setting it up with a serivce account (i.e. using methods like setServiceAccountId(...) when creating a GoogleCredential) and it works perfectly. But i want this to work on behalf of an authorized user, as per above code snippet!
All of my code is in package com.mytestapp. This is the package i specified when creating the Android client id.
I am launching the app through a debugger in Android Studio, while the app is launched on my physical Android device.
Problem Statement:
During executing the list bucket operation above, i get this Json exception:
com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden { "code" : 403, "errors" : [ { "domain" : "usageLimits", "message" : "Access Not Configured. The API (Cloud Storage API) is not enabled for your project. ............}
What am i doing wrong? Is it because i am not setting up the client secret right? (i thought no client secrets for Android anymore). Or application name has to be smth very specific? I set it to some free-form text string.
I thought this was supposed to be easy and straightforward!
At the very least -- is there a coherent, complete example of how to do this for nonservice accounts on Android? I searched all over the place without any luck.