5

I'm currently trying to use the new java client(s) and due to legacy reasons for current state of google libraries, I need to use both the gdata and the new google java client api.

Obviously I'd like to use OAuth2 -- however with OAuth2 I am not getting the access token secret -- which causes an issue b/c gdata requires the access token secret.

Could anyone pls advise on a workaround eg. - is there a way to use gdata java libraries with only access token(OAuth2) and not access token secret? Code examples(and attempt) do not corroborate this but perhaps I'm doing something incorrectly

  • or do I have to use OAuth1 for both gdata and new google java client api?
  • or is there another way?

Thanks

Java Guy
  • 870
  • 12
  • 16
  • I would be interested as well. I am using Oauth2 example from here http://code.google.com/p/google-api-java-client/wiki/OAuth2Draft10 but I can't find a way to use the access token when using a e.g. com.google.gdata.client.spreadsheet.SpreadsheetService.SpreadsheetService(String) – Christoph Dec 28 '11 at 12:50

1 Answers1

13

I found a solution. You can set a special HTTP-Header (Authorization: Bearer ACCESS_TOKEN) as documented in http://code.google.com/apis/accounts/docs/OAuth2WebServer.html#callinganapi

Once you have received your accessToken (e.g. like this http://code.google.com/p/google-api-java-client/wiki/OAuth2Draft10 ) you can call your "old" gdata service like this:

SpreadsheetService service = new SpreadsheetService("yourAppName");
service.setHeader("Authorization", "Bearer " + accessToken);
URL metafeedUrl = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");
SpreadsheetFeed feed = service.getFeed(metafeedUrl, SpreadsheetFeed.class);

List<SpreadsheetEntry> spreadsheets = feed.getEntries();
for (int i = 0; i < spreadsheets.size(); i++) {
  SpreadsheetEntry entry = spreadsheets.get(i);
  System.out.println("\t" + entry.getTitle().getPlainText());
}

It's a bit odd that I couldn't find it really documented clearly. I just found it by coincidence.

Christoph
  • 3,640
  • 1
  • 35
  • 41
  • 1
    Thanks Christoph -- that's great. I actually got lucky, and Google happened to update the library I needed to the new google java client api. Yeah, their docs are in need of some work... – Java Guy Jan 03 '12 at 19:26
  • 1
    SO if we use new google java client api then we can use OAUTH 2.0 directly from the api right? Please correct me if I am wrong. I am working on the spreadsheet api since last week and not able to achieve my goal. Also I would like to add that if I am having published spreadsheet then does Authorization is needed or not? Please kindly help me – Scorpion Sep 19 '12 at 04:25
  • THANK YOU!!! Every other site that I visited first had non-compiling, non-functional examples. I can't believe that the idiots at Google didn't put this in their documentation. – Jesse Barnum Oct 09 '15 at 22:56