Google Cloud Storage - how to authorize?

I am desperately trying to access Google Cloud Storage from Java. My challenge is that I have to create a shared library and browser based authentication is not possible.

Whatever I do, it always tells me "401 Unauthorized". I am definitely using correct credentials, ... My Java version is 1.7

Here is the code:

        GoogleCredential credential = new GoogleCredential.Builder()
            .setTransport(httpTransport)
            .setJsonFactory(new JacksonFactory())
            .setClientSecrets("clientid", "clientsecret").build();

            storage = new Storage.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName(
                "test-name")
                .build();

      

The corresponding client ID and passcode are set as shown on the console; However, I always say "Login". The library must be used from a workflow, so it is not possible for someone to authorize the application in the browser window ...

Thank,

Mario

+3


source to share


1 answer


I believe it was just recently that we personally discussed that this issue was actually solved using Google Service Accounts, as described here: https://developers.google.com/accounts/docs/OAuth2ServiceAccount

The credentials are structured as follows:



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();

      

+1


source







All Articles