google-app-engine – 在Google App Engine上使用OAuth令牌的过程

我有一个应用程序,客户端在
Android上运行,服务器部署在Google App Engine上.该应用程序将使用用户Google日历数据,并需要对日历服务进行身份验证才能访问这些供稿.我可以处理从客户端上的每个用户获取OAuth令牌和令牌机密,并将它们保存到服务器上的数据库中,以便在访问源时使用.当我尝试使用它们对Calendar服务进行身份验证时,我遇到了麻烦.

我最初使用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

即使这些库已正确链接.我在线做了一些研究并发现一篇帖子说它是因为app引擎不再喜欢gdata库而且是prefesrs Google的新版google-api-java-client,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进行身份验证并访问他们的日历源?

最佳答案 好吧,基本上我的第一次尝试是正确的,这仍然可以使用gdata库完成.问题是,在将项目上传到托管站点时,Eclipse不会在我的构建路径中包含一些库.我也没有包含位于deps dependencies文件夹中的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();
    }
点赞