使用Google gdata客户端API从Java / Scala获得OAuth2授权 [英] OAuth2 authorization from Java/Scala using google gdata client API

查看:94
本文介绍了使用Google gdata客户端API从Java / Scala获得OAuth2授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何执行与 Google .Net示例相同的流程,在执行OAuth 2.0 ,使用等效的Java API?

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

.Net示例我试图模仿使用Java api似乎适合发送api请求来创建授权url,然后假设我将在浏览器中使用该URL来获取访问代码...因此允许服务器端代码在此后使用Google电子表格API,用于该Google帐户。

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.

我发现的最近的Google Java API类是 OAuthHelper ,但它似乎需要在实例化时使用 userAuthorizationUrl 时间,这实际上是我希望通过它自己的 createUserAuthorizationUrl方法 在我设法实例化之后对我来说,这是一个循环的难题。这似乎表明我错过了我的假设,可能这不是用来模仿.Net代码示例的正确类。

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.

您的帮助非常感谢。

推荐答案

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

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

Scala解决方案代码,由 http:// javatoscala提供。 com /

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

然而,对于scala,您目前还需要应用这个 ...

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

这篇关于使用Google gdata客户端API从Java / Scala获得OAuth2授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆