Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
614 views
in Technique[技术] by (71.8m points)

oauth - gdata-java-client + oauth2 + access_token secret

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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("" + entry.getTitle().getPlainText());
}

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...