使用google gdata客户端API从Java / Scala获得OAuth2授权

时间:2014-12-19 19:23:15

标签: oauth-2.0 google-api google-oauth google-spreadsheet-api

如何使用等效的Java api执行与Google .Net sample at subsection "Performing OAuth 2.0"相同的流程?

我尝试使用Java api模拟的.Net示例似乎适合发送api请求来创建授权URL,然后假设我将在浏览器中使用该URL来获取访问代码...从而允许服务器 - 对于那个谷歌帐户,此后使用谷歌电子表格api的旁边代码。

我发现的Google Java api类是OAuthHelper,但它似乎需要userAuthorizationUrl实例化时间,这实际上是我希望通过它自己{{3}获得的} 之后我将设法实例化它 - 这对我来说是一个循环难题。这似乎表明我在我的假设中遗漏了一些东西,可能这不是用于模仿.Net代码示例的正确类。

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

http://soatutorials.blogspot.co.at/2013/08/google-spreadsheet-api-connecting-with.html看起来像是Java。

Scala解决方案代码,由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...")
  }
}

但是,对于scala,您目前还需要应用this ...