matanox
matanox

Reputation: 13706

OAuth2 authorization from Java/Scala using google gdata client API

How would you perform the same flow as the Google .Net sample at subsection "Performing OAuth 2.0", using an equivalent Java api?

That .Net sample I am trying to mimic using Java api seems fit for sending an api request to create an authorization url, then assumes I would use that url in a browser to obtain an access code... thus allowing server-side code to use google spreadsheets api thereafter, for that one google account.

The closet Google Java api class I spotted is OAuthHelper, but it seems to require the userAuthorizationUrl at instantiation time, which is actually what I wish to obtain from it via its own createUserAuthorizationUrl method after I will have managed to instantiate it - a bit of a cyclic conundrum to me. Which seems to indicate I am missing something in my assumptions, probably this is not the right class to use for mimicking the .Net code sample.

Your help much appreciated.

Upvotes: 1

Views: 3347

Answers (1)

matanox
matanox

Reputation: 13706

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...")
  }
}

For scala however, you currently also need to apply this...

Upvotes: 2

Related Questions