在Google App Engine上使用OAuth令牌的步骤

4
我有一个应用程序,客户端运行在Android上,服务器部署在Google App Engine上。该应用程序将使用用户的Google日历数据,并需要对日历服务进行身份验证以访问其源。我可以处理从客户端获得每个用户的OAuth令牌和令牌密钥,并将它们持久化到服务器数据库中以在访问源时使用。但当我尝试使用它们对日历服务进行身份验证时,遇到了问题。
起初,我是通过使用gdata库来做到这一点 按照此示例操作
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

尽管库已经正确链接,但我在网上进行了一些研究,并发现一篇帖子称它是因为应用引擎不再喜欢gdata库,而更喜欢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()时遇到了错误。我查看了GoogleTransport的API并阅读了警告:警告:计划在版本1.1中不再扩展HttpTransport,因此这似乎是我的错误源头。
所以现在我向大家寻求帮助解决这个问题。基于我上面讨论的内容,我应该如何使用我为每个用户拥有的访问令牌和令牌密钥进行身份验证并访问他们的日历订阅?
1个回答

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();
    }

我们如何获取 token_secret - Rahul Khurana

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接