2011-05-04 68 views
4

我有一個運行在Android上的客戶端和部署在Google App Engine上的服務器的應用程序。該應用程序將利用用戶Google日曆數據並需要對日曆服務進行身份驗證才能訪問這些Feed。我可以處理從客戶端上的每個用戶獲取OAuth令牌和令牌機密,並將它們保存到服務器上的數據庫中,以便在訪問提要時使用。當我嘗試使用它們來驗證日曆服務時,我遇到了麻煩。在Google App Engine上使用OAuth令牌的過程

我是在第一次使用GDATA庫做到這一點by following this example

import com.google.gdata.client.calendar.*; 
import com.google.gdata.client.authn.oauth.*; 

GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters(); 
oauthParameters.setOAuthConsumerKey(CONSUMER_KEY); 
oauthParameters.setOAuthConsumerSecret(CONSUMER_SECRET); 
oauthParameters.setOAuthToken(ACCESS_TOKEN); 
oauthParameters.setOAuthTokenSecret(TOKEN_SECRET); 

CalendarService client = new CalendarService("yourCompany-YourAppName-v1"); 
client.setOAuthCredentials(oauthParameters, new OAuthHmacSha1Signer()); 

URL feedUrl = new URL("https://www.google.com/calendar/feeds/default/freebusy/busy-times"+username); 
CalendarEventFeed resultFeed = service.getFeed(eventFeedUrl,CalendarEventFeed.class); 

System.out.println("All events on your calendar:"); 
System.out.println(); 
for (int i = 0; i < resultFeed.getEntries().size(); i++) { 
    CalendarEventEntry entry = resultFeed.getEntries().get(i); 
    System.out.println("\t" + entry.getTitle().getPlainText()); 
} 
System.out.println(); 

然而,這引起了異常:

java.lang.NoClassDefFoundError: com/google/gdata/client/authn/oauth/OAuthParameters 

即使文庫正確鏈接。我做了一些reaearch在線,並發現一個職位說這是因爲應用程序引擎不再喜歡gdata庫和prefesrs谷歌的新google-api-java客戶端,Google APIs Client Library for Java

因此,我重新編寫代碼來使用此庫,而不是希望它能解決我的問題。使用他們的示例代碼,並用適當的憑據替換:

import com.google.api.client.auth.oauth.*; 
import com.google.api.client.googleapis.*; 
import com.google.api.client.googleapis.json.*; 
import com.google.api.client.http.*; 
import com.google.api.client.util.*; 
import java.io.*; 

// authorize using OAuth 
HttpTransport transport = GoogleTransport.create(); 
transport.addParser(new JsonCParser()); 
OAuthParameters parameters = new OAuthParameters(); 
parameters.consumerKey = "anonymous"; 
parameters.token = "..."; 
OAuthHmacSigner signer = new OAuthHmacSigner(); 
signer.clientSharedSecret = "anonymous"; 
signer.tokenSharedSecret = "..."; 
parameters.signer = signer; 
parameters.signRequestsUsingAuthorizationHeader(transport); 

但是現在我得到一個錯誤,調用GoogleTransport.create()。我看着api for GoogleTransport並閱讀:警告:計劃在版本1.1中不再擴展HttpTransport,所以看起來這是我的錯誤的來源。

所以現在我來找你們所有人來幫助解決這個問題。基於我上面討論的內容,以及我如何使用訪問令牌和令牌機密讓每個用戶向Google進行身份驗證並訪問其日曆提要?

回答

2

好吧,基本上我的第一次嘗試是正確的,這仍然可以使用gdata庫完成。問題在於,在將項目上傳到託管站點時,Eclipse沒有在我的構建路徑中包含一些庫。我也沒有包括位於deps依賴關係文件夾中的google-collect-1.0-rc1.jar,這是必須的。如果這個問題麻煩別人,確保了我上面的討論覆蓋和下面的代碼應該爲你工作進行修改,因爲你需要:

URL feedUrl; 
    try { 
     /* Setup the request for retrieving events for a given day */ 
     feedUrl = new URL("https://www.google.com/calendar/feeds/default/private/full");  
     CalendarQuery myQuery = new CalendarQuery(feedUrl); 
     myQuery.setMinimumStartTime(DateTime.parseDateTime("2011-05-07T00:00:00")); 
     myQuery.setMaximumStartTime(DateTime.parseDateTime("2011-05-07T23:59:59")); 

     /* Setup OAuth parameters to include in the request */ 
     GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters(); 
     oauthParameters.setOAuthConsumerKey(CONSUMER_KEY); 
     oauthParameters.setOAuthConsumerSecret(CONSUMER_SECRET); 
     oauthParameters.setOAuthToken(token); 
     oauthParameters.setOAuthTokenSecret(token_secret); 

     /* Send the request */ 
     service.setOAuthCredentials(oauthParameters, new OAuthHmacSha1Signer()); 
     CalendarEventFeed myFeed = service.getFeed(myQuery, CalendarEventFeed.class); 

     /* Process the request */ 
     CalendarEntry entry; 
     for (int i = 0; i < myFeed.getEntries().size(); i++) { 
      entry = myFeed.getEntries().get(i); 
      System.out.println(entry.getTitle().getPlainText()); 
     }   
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (OAuthException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (ServiceException e) { 
     e.printStackTrace(); 
    } 
+0

我們怎麼** ** token_secret? – 2017-12-22 08:24:25