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

Oauth 2.0 使用google gdata客户端API从Java/Scala获得OAuth2授权,oauth-2.0,google-api,google-oauth,google-spreadsheet-api,Oauth 2.0,Google Api,Google Oauth,Google Spreadsheet Api,如何使用等效的JavaAPI执行与相同的流 我尝试使用Java api模拟的.Net示例似乎适合发送api请求以创建授权url,然后假设我将在浏览器中使用该url来获取访问代码。。。因此,允许服务器端代码使用谷歌电子表格api之后,为一个谷歌帐户 我发现的隐藏的Google Java api类是,但在实例化时它似乎需要userAuthorizationUrl,这实际上是我希望在成功实例化之后通过它自己的类从中获得的-对我来说有点循环难题。这似乎表明我在假设中遗漏了一些东西,可能这不是用于模拟.N

如何使用等效的JavaAPI执行与相同的流

我尝试使用Java api模拟的.Net示例似乎适合发送api请求以创建授权url,然后假设我将在浏览器中使用该url来获取访问代码。。。因此,允许服务器端代码使用谷歌电子表格api之后,为一个谷歌帐户

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

非常感谢您的帮助。

看起来它适合Java

Scala解决方案代码,由以下人员提供:

package com.articlio.googleApi
导入java.io.IOException;
导入java.net.MalformedURLException;
导入java.net.URL;
导入com.google.gdata.client.GoogleService;
导入com.google.gdata.client.authn.oauth.GoogleOAuthHelper;
导入com.google.gdata.client.authn.oauth.GoogleOAuthParameters;
导入com.google.gdata.client.authn.oauth.OAuthException;
导入com.google.gdata.client.authn.oauth.OAuthHmacSha1Signer;
导入com.google.gdata.client.authn.oauth.oauthrssasha1signer;
导入com.google.gdata.client.authn.oauth.OAuthSigner;
导入com.google.gdata.client.spreadsheet.spreadsheet服务;
导入com.google.gdata.data.spreadsheet.SpreadsheetFeed
导入com.google.gdata.data.BaseEntry;
导入com.google.gdata.data.BaseFeed;
导入com.google.gdata.data.Feed;
导入com.google.gdata.util.AuthenticationException;
导入com.google.gdata.util.ServiceException;
//如果不需要,请移除
导入scala.collection.JavaConversions_
对象OAuth2Sample{
def loginOAuth2(clientID:String,clientSecret:String){
val范围=”https://docs.google.com/feeds https://spreadsheets.google.com/feeds"
val oauthParameters=新的GoogleOAuthParameters
oauthParameters.setOAuthConsumerKey(clientID)//
变量签名者:OAuthSigner=null
oauthParameters.setOAuthConsumerCret(clientSecret)//
签名者=新的OAuthHMACSHA1签名者()
val oauthHelper=新的Google oauthHelper(签名者)
oauthParameters.setScope(范围)
试一试{
oauthHelper.getUnauthorizedRequestToken(oauthParameters)
}抓住{
案例e:OAutheException=>e.printStackTrace()
}
val requestUrl=oauthHelper.createUserAuthorizationUrl(oauthParameters)
println(请求URL)
println(“请访问上面的URL以授权您的OAuth”+
请求令牌。完成后,按任意键“+
“继续……”
试一试{
System.in.read()
}抓住{
案例e:IOException=>e.printStackTrace()
}
变量标记:String=null
试一试{
token=oauthHelper.getAccessToken(oauthParameters)
}抓住{
案例e:OAutheException=>e.printStackTrace()
}
println(“OAuth访问令牌:”+令牌)
println()
var feedUrl:URL=null
试一试{
feedUrl=新URL(“https://spreadsheets.google.com/feeds/spreadsheets/private/full")
}抓住{
案例e:MalformedURLException=>e.printStackTrace()
}
println(“将请求发送到”+feedUrl.toString)
println()
val googleService=新的电子表格服务(“oauth示例应用程序”)
试一试{
setOAuthCredentials(oauthParameters,签名者)
}抓住{
案例e:OAutheException=>e.printStackTrace()
}
val feed=googleService.getFeed(feedUrl,classOf[SpreadsheetFeed])
val电子表格=feed.getEntries
println(“响应数据:”)
println(“===============================================================================”)
如果(电子表格!=null){
对于(电子表格e.printStackTrace()
}
println(“OAuth令牌已撤销…”)
}
}

但是,对于scala,您当前还需要应用…

看起来像是这样,只是抽象级别略低于.Net代码。Matts answer看起来更好,但这是相关的/重复的:刷新令牌?那篇博客文章看起来很方便,但缺少刷新令牌。如果有使用刷新令牌创建n的代码就好了ew访问令牌,因为访问令牌很快过期(1小时后)。无论如何,很好的帖子,+1。
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...")
  }
}