问题
I'm trying to execute the example from:
https://code.google.com/p/google-api-java-client/source/browse?repo=samples#hg%2Ffusiontables-cmdline-sample
Which is basically an example on how to authenticate and use Fusion API. I can't make it work...at this point:
// authorize
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
The browser shows an "Error:redirect_uri_mismatch".
The weird thing here is that the other examples that authenticate using Oauth (google plus API and cloud storage one), don't use the same approach as this one, that basically consists in using the JSON file that you can download once a Service Account has been created trough the API console.
Actually, there is a question that uses the approach from this examples:
Google Fusion Tables 400 invalid_grant
My main question is..which one is correct? I'm not sure about this
Second one is...this example looks for an item called client-secret in the JSON file, which I don't have. I don't think this is the cause that makes the code not workable, but I'm wondering what this client-secret thingy is...
Some insights on how to proceed would be greatly appreciated
Thanks! Alex
回答1:
Well, if anyone is in the same situation as I was...I will post the solution I found to be working. What I did basically is modified the example (special thanks to Christian Junk for his work) so it looks more like the others examples (storage and plus ones) when it comes to authentication.
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
// check for valid setup
if (SERVICE_ACCOUNT_EMAIL.startsWith("Enter ")) {
System.err.println(SERVICE_ACCOUNT_EMAIL);
System.exit(1);
}
String p12Content = Files.readFirstLine(new File("key.p12"), Charset.defaultCharset());
if (p12Content.startsWith("Please")) {
System.err.println(p12Content);
System.exit(1);
}
// service account credential (uncomment setServiceAccountUser for domain-wide delegation)
GoogleCredential credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes(FusiontablesScopes.FUSIONTABLES)
.setServiceAccountPrivateKeyFromP12File(new File("key.p12"))
// .setServiceAccountUser("user@example.com")
.build();
// set up global FusionTables instance
fusiontables = new Fusiontables.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();
This differs from the approach of the original example, but it works for me and I could list the tables, create new ones, populate them and delete them.
I just wish the examples were more easy to find in the Oauth and Fusion tables documentation pages, this would saved me some precious time :)
来源:https://stackoverflow.com/questions/16429828/how-to-authenticate-in-fusion-tables-with-oauth-v2