OAuth2 authorization from Java/Scala using google gdata client API

前端 未结 1 824
悲哀的现实
悲哀的现实 2021-01-23 08:40

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

相关标签:
1条回答
  • 2021-01-23 09:08

    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...

    0 讨论(0)
提交回复
热议问题