Reputation: 63
I am desperately trying to access Google Cloud Storage from Java. My challenge is that I should create a generic library and a browser based auth is not possible.
Whatever I do, it always tells me "401 Unauthorised". I am definitely using the 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 secret is set as shown in the console; however, I always tells me "Login required". The library needs to be used from a worker process, so it is impossible that someone authorises the application in a browser window ...
Thanks,
Mario
Upvotes: 3
Views: 344
Reputation: 1776
I believe that just recently we discussed in person that this problem is actually solved by using Google Service accounts as documented over here: https://developers.google.com/accounts/docs/OAuth2ServiceAccount
The credentials are the built like this:
String emailAddress = "[email protected]";
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();
Upvotes: 1