[英]Get access google spreadsheet (saved into google drive account) from a java google app engine (servlet)
我真的被困在這里。 我想從google app引擎(使用java,使用servlet)訪問google電子表格,因為這個想法是從該電子表格中讀取信息,並保存在我的google驅動器帳戶中,並通過“ jsp”顯示給有限的用戶數,在這里在我的公司)。
當然,我已經在“谷歌雲開發人員控制台”中創建了我的項目,我得到了“項目ID”第四種情況-XXXX,等等。 在執行此步驟之前,我了解了所有內容,並且一切順利。
我一直在這里,谷歌等上查找成千上萬個示例。對於我讀到的所有內容,我都知道:我必須創建一個“ OAuth2憑據”(為什么我要在開發人員控制台的“ Api和身份驗證”中創建OAuth2憑據)當然(我得到了一個帶有auth_uri,client_secret,client_id等的json)。
根據我所讀的內容,只需要client_secret和client_id。 但是按照本教程( 鏈接 )我得到了這個錯誤“ oauth_token不存在”。 在其他教程中,我讀過,沒有必要在應用程序引擎中使用“ OAuth2”。 我很頭暈。
我唯一想要的是一個簡單的Java servlet(沒什么復雜的,遵循良好實踐,我不在乎),它從google電子表格中讀取數據(保存到我的google驅動器帳戶中)並通過“ jsp”顯示,至少我很滿意接受在我的servlet上接收電子表格數據,稍后我將想象如何顯示
我正在使用Eclipse Luna,我安裝了“ gdata java api”,“作為本地主機的Google應用程序引擎”等(均已正確安裝,正在運行且沒有錯誤)。 創建環境我遵循了本教程
一些問題:1-電子表格需要發布嗎? (菜單:文件->發布到網絡)。 從應用程序引擎獲取訪問權限? 2-要在強制上傳到Google App Engine(http://.appspot.com/guestbook)中測試我的代碼(並查看我是否可以訪問電子表格),還是可以嘗試使用“ localhost”?
我將上傳我的代碼,附加的圖片等。對於令人討厭的代碼,我深表歉意,但現在我需要解決此問題
謝謝大家
public void callingSpreadsheetTest2() throws IOException {
System.out.println("callingSpreadsheetTest2");
HttpTransport transport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
GoogleOAuthParameters oAuthParameters = new GoogleOAuthParameters();
oAuthParameters.setOAuthConsumerKey(CLIENT_ID);
oAuthParameters.setOAuthConsumerSecret(CLIENT_SECRET);
OAuthHmacSha1Signer signer = new OAuthHmacSha1Signer();
GoogleOAuthHelper oAuthHelper = new GoogleOAuthHelper(signer);
oAuthParameters.setScope(SCOPES);
try{
oAuthHelper.getUnauthorizedRequestToken(oAuthParameters);
}catch (OAuthException e){
e.printStackTrace();
}
String requestUrl = oAuthHelper.createUserAuthorizationUrl(oAuthParameters);
System.out.println(requestUrl);
System.out.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(IOException e){
e.printStackTrace();
}
oAuthParameters.setOAuthType(OAuthType.TWO_LEGGED_OAUTH);
String token = null;
try{
token = oAuthHelper.getAccessToken(oAuthParameters);
}catch(OAuthException e){
e.printStackTrace(); //---->Attention: HERE I GOT toke=null (oauth_token does not exist.)
}
System.out.println("OAuth Access Token: " + token);
System.out.println();
URL feedUrl = null;
try{
feedUrl = new URL(SPREADSHEET_URL);
}catch(MalformedURLException e){
e.printStackTrace();
}
SpreadsheetService spreadsheetService = new SpreadsheetService("GAppEngineProj");
try{
spreadsheetService.setOAuthCredentials(oAuthParameters, signer);
}catch(OAuthException e){
e.printStackTrace();
}
try {
SpreadsheetFeed feed = spreadsheetService.getFeed(feedUrl, SpreadsheetFeed.class);
List<SpreadsheetEntry> spreadsheets = feed.getEntries();
if(spreadsheets != null) {
for (SpreadsheetEntry spreadsheet : spreadsheets) {
System.out.println(spreadsheet.getTitle().getPlainText());
}
}
} catch (ServiceException e) {
e.printStackTrace();
}
另外,我的類“ com.google.gdata.client.spreadsheet.SpreadsheetService”不只有方法setOAuth2Credentials [service.setOAuthCredentials(parameters,signer);]
再次感謝!!
您正在使用OAuth1協議的代碼(已棄用)。 您應該改用OAuth2。
App Engine應用程序附帶了一個非常方便的AppIdentityService
( 文檔 ),使您的應用程序可以生成OAuth2令牌,就好像該應用程序具有格式為your-project-id@appspot.gserviceaccount.com
的電子郵件your-project-id@appspot.gserviceaccount.com
。
您所要做的就是使用Google雲端硬盤/ Google Spreadsheets UI通過此電子郵件共享電子表格。 對於您而言,該電子郵件為fourth-case-XXXX@appspot.gserviceaccount.com
情況fourth-case-XXXX@appspot.gserviceaccount.com
然后,在代碼中,執行以下操作:
AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService();
String accessToken = appIdentityService.getAccessToken(Collections.singleton(SPREADSHEET_SCOPE)).getAccessToken();
GoogleCredential googleCredential = new GoogleCredential();
googleCredential.setAccessToken(accessToken);
spreadsheetService = new SpreadsheetService("MyApp");
spreadsheetService.setOAuth2Credentials(buildCredential());
然后您就可以使用spreadsheetService
讀取數據了。
注意:此代碼是使用GData庫版本1.47.1編寫的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.