您将如何执行与“执行 OAuth 2.0”小节中的 Google .Net 示例 https://developers.google.com/google-apps/spreadsheets/,使用等效的 Java api?
我尝试使用 Java api 模仿的那个 .Net 示例似乎适合发送 api 请求来创建授权 url,然后假设我将在浏览器中使用该 url 来获取访问代码...从而允许服务器端代码此后,对于该谷歌帐户,使用谷歌电子表格 API。
我发现的最隐蔽的 Google Java api 类是OAuth助手 http://grepcode.com/file/repo1.maven.org/maven2/com.google.gdata/core/1.47.1/com/google/gdata/client/authn/oauth/OAuthHelper.java#OAuthHelper,但似乎需要userAuthorizationUrl
在实例化时,这实际上是我希望通过它自己的方式从中获得的创建用户授权Url方法 https://developers.google.com/gdata/javadoc/com/google/gdata/client/authn/oauth/OAuthHelper#createUserAuthorizationUrl(com.google.gdata.client.authn.oauth.OAuthParameters) 在我成功实例化它之后- 对我来说有点循环难题。这似乎表明我在假设中遗漏了一些东西,可能这不是用于模仿 .Net 代码示例的正确类。
非常感谢您的帮助。
好像http://soatutorials.blogspot.co.at/2013/08/google-spreadsheet-api-connecting-with.html http://soatutorials.blogspot.co.at/2013/08/google-spreadsheet-api-connecting-with.html有Java版的。
Scala 解决方案代码,由http://javatoscala.com/ 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 https://stackoverflow.com/questions/27574210/scala-class-file-broken-error-when-using-java-jar/27574714#27574714...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)