Java / Scala OAuth2 authorization using google gdata API

How would you execute the same thread as the Google.Net sample in the "Running OAuth 2.0" subsection using the equivalent Java api?

This .Net sample that I am trying to simulate using a Java api seems appropriate for sending an api request to create an authorization URL, then assumes that I will use this URL in the browser to get the access code ... this way to subsequently use google spreadsheets for this google account.

The Java api class cell, which I noticed, is OAuthHelper , but it seems that it takes userAuthorizationUrl time to create, which is actually what I want to get from it through my createUserAuthorizationUrl method after I manage to instantiate it - a little cyclic puzzle for me. Which seems to indicate that I have something missing from my assumptions, probably not the class that needs to be used to simulate a sample .Net code.

Your help is greatly appreciated.

+1
source share
1 answer

It looks like http://soatutorials.blogspot.co.at/2013/08/google-spreadsheet-api-connecting-with.html has it for Java.

Scala solution code courtesy of http://javatoscala.com/ :

 package com.articlio.googleApi import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import com.google.gdata.client.GoogleService; import com.google.gdata.client.authn.oauth.GoogleOAuthHelper; import com.google.gdata.client.authn.oauth.GoogleOAuthParameters; import com.google.gdata.client.authn.oauth.OAuthException; import com.google.gdata.client.authn.oauth.OAuthHmacSha1Signer; import com.google.gdata.client.authn.oauth.OAuthRsaSha1Signer; import com.google.gdata.client.authn.oauth.OAuthSigner; import com.google.gdata.client.spreadsheet.SpreadsheetService; import com.google.gdata.data.spreadsheet.SpreadsheetFeed import com.google.gdata.data.BaseEntry; import com.google.gdata.data.BaseFeed; import com.google.gdata.data.Feed; import com.google.gdata.util.AuthenticationException; import com.google.gdata.util.ServiceException; //remove if not needed import scala.collection.JavaConversions._ object OAuth2Sample { def loginOAuth2(clientID: String, clientSecret: String) { val SCOPES = "https://docs.google.com/feeds https://spreadsheets.google.com/feeds" val oauthParameters = new GoogleOAuthParameters oauthParameters.setOAuthConsumerKey(clientID) // var signer: OAuthSigner = null oauthParameters.setOAuthConsumerSecret(clientSecret) // signer = new OAuthHmacSha1Signer() val oauthHelper = new GoogleOAuthHelper(signer) oauthParameters.setScope(SCOPES) try { oauthHelper.getUnauthorizedRequestToken(oauthParameters) } catch { case e: OAuthException => e.printStackTrace() } val requestUrl = oauthHelper.createUserAuthorizationUrl(oauthParameters) println(requestUrl) println("Please visit the URL above to authorize your OAuth " + "request token. Once that is complete, press any key to " + "continue...") try { System.in.read() } catch { case e: IOException => e.printStackTrace() } var token: String = null try { token = oauthHelper.getAccessToken(oauthParameters) } catch { case e: OAuthException => e.printStackTrace() } println("OAuth Access Token: " + token) println() var feedUrl: URL = null try { feedUrl = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full") } catch { case e: MalformedURLException => e.printStackTrace() } println("Sending request to " + feedUrl.toString) println() val googleService = new SpreadsheetService("oauth-sample-app") try { googleService.setOAuthCredentials(oauthParameters, signer) } catch { case e: OAuthException => e.printStackTrace() } val feed = googleService.getFeed(feedUrl, classOf[SpreadsheetFeed]) val spreadsheets = feed.getEntries println("Response Data:") println("=====================================================") if (spreadsheets != null) { for (spreadsheet <- spreadsheets) { println(spreadsheet.getTitle.getPlainText) } } println("=====================================================") println() println("Revoking OAuth Token...") try { oauthHelper.revokeToken(oauthParameters) } catch { case e: OAuthException => e.printStackTrace() } println("OAuth Token revoked...") } } 

However, for scala you also need to apply this ...

+1
source

All Articles