如何使用Java客户端访问Google服务? [英] How do you access Google service using a java client?
问题描述
我正在开发一个名为searcegadget2的Google应用程序,该应用程序在Google Apps Engine(GAE)上已注册。我已经使用OAuthHmacSha1Signer()实施了三方OAuth 。我正在获取访问令牌 &将它存储在 Session (会话启用)。
下面,我使用链接onclick调用servlet事件。该servlet将使用accesstoken访问Spreadsheet服务。我的代码是: GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
oauthParameters.setOAuthConsumerKey(CONSUMER_KEY);
oauthParameters.setOAuthConsumerSecret(CONSUMER_SECRET);
oauthParameters.setOAuthType(OAuthParameters.OAuthType.THREE_LEGGED_OAUTH);
oauthParameters.setScope(https://spreadsheets.google.com/feeds/);
oauthParameters.setOAuthToken(request.getSession()。getAttribute(oauth_token)。toString());
oauthParameters.setOAuthVerifier(request.getSession()。getAttribute(oauth_verifier)。toString());
oauthParameters.setOAuthTokenSecret(request.getSession()。getAttribute(oauth_token_secret)。toString());
out.println(访问服务);
GoogleService googleService = new GoogleService(wise,searceapps-searcegadget2-1);
out.println(< br />设置参数);
googleService.setOAuthCredentials(oauthParameters,new OAuthHmacSha1Signer());
out.println(< br />设置URl);
网址feedUrl =新网址(https://spreadsheets.google.com/feeds/spreadsheets/private/full);
out.println(< br /> Accessing ResultFeed);
SpreadsheetFeed resultFeed = googleService.getFeed(feedUrl,SpreadsheetFeed.class);
out.println(< div id ='feed'>);
out.println(Response Data:);
out.println(========================================= ============);
out.println(| TITLE:+ resultFeed.getTitle()。getPlainText());
if(resultFeed.getEntries()。isEmpty()){
out.println(| \ tNo entries found。);
} else {
列表< SpreadsheetEntry> spreadsheets = resultFeed.getEntries();
for(int i = 0; i< spreadsheets.size(); i ++){
SpreadsheetEntry entry = spreadsheets.get(i);
System.out.println(\ t+ entry.getTitle()。getPlainText());
}
}
out.println(=============================== ======================);
out.println(< / div>);
我已经打印所有oauthParameter进行测试&他们正确设置。但是,在调用这个servlet时,我得到的唯一结果是:
$ b
访问服务
blockquote>
我添加了gdata-spreadsheet-3.0.jar,但它甚至不允许我创建服务对象!问题是什么?
此外,按照给出 here ,应用程序名称最好具有[company-id] - [app-name] - [app-version]的格式。名称将被Google服务器用来监控认证来源。我不知道我在哪里找到我的公司ID,但我的应用名称是searcegadget2&版本为1。
供参考: Google Spreadsheets API开发人员指南:Java ,使用Java客户端库, Google数据协议客户端中的OAuth图书馆
解决方案最后发现,
I am developing a java application which is registered on Google Apps Engine (GAE) with the name "searcegadget2". I have implemented 3-legged OAuth with OAuthHmacSha1Signer(). I am getting the Access Token properly & storing it on Session (session is enabled).
Next, I am calling the servlet using a link onclick event. This servlet is to access the Spreadsheet service using the accesstoken. My code is :
GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters(); oauthParameters.setOAuthConsumerKey(CONSUMER_KEY); oauthParameters.setOAuthConsumerSecret(CONSUMER_SECRET); oauthParameters.setOAuthType(OAuthParameters.OAuthType.THREE_LEGGED_OAUTH); oauthParameters.setScope("https://spreadsheets.google.com/feeds/"); oauthParameters.setOAuthToken(request.getSession().getAttribute("oauth_token").toString()); oauthParameters.setOAuthVerifier(request.getSession().getAttribute("oauth_verifier").toString()); oauthParameters.setOAuthTokenSecret(request.getSession().getAttribute("oauth_token_secret").toString()); out.println("Accessing Service"); GoogleService googleService = new GoogleService("wise", "searceapps-searcegadget2-1"); out.println("<br/>Setting Parameter"); googleService.setOAuthCredentials(oauthParameters, new OAuthHmacSha1Signer()); out.println("<br/>Setting URl"); URL feedUrl = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full"); out.println("<br/>Accessing ResultFeed"); SpreadsheetFeed resultFeed = googleService.getFeed(feedUrl, SpreadsheetFeed.class); out.println("<div id='feed'>"); out.println("Response Data:"); out.println("====================================================="); out.println("| TITLE: " + resultFeed.getTitle().getPlainText()); if (resultFeed.getEntries().isEmpty()) { out.println("|\tNo entries found."); } else { List<SpreadsheetEntry> spreadsheets = resultFeed.getEntries(); for (int i = 0; i < spreadsheets.size(); i++) { SpreadsheetEntry entry = spreadsheets.get(i); System.out.println("\t" + entry.getTitle().getPlainText()); } } out.println("====================================================="); out.println("</div>");
I have printed all the oauthParameter for testing & they are properly set. But, while calling this servlet, the only output that I am getting is :
Accessing Service
I have added, gdata-spreadsheet-3.0.jar but still it is not even allowing me to create the service object !!! What is the problem ?
Also, as per it given here, "Application names should preferably have the format [company-id]-[app-name]-[app-version]. The name will be used by the Google servers to monitor the source of authentication." I don't know where I find my company id, but my app name is "searcegadget2" & version is "1".
For reference : Google Spreadsheets API Developer's Guide: Java, Using the Java Client Library, OAuth in the Google Data Protocol Client Libraries
解决方案Finally found,
Google Service has dependency on : guava as it is mentioned here.
这篇关于如何使用Java客户端访问Google服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!