【问题标题】:Procedure for using OAuth tokens on Google App Engine在 Google App Engine 上使用 OAuth 令牌的过程
【发布时间】:2011-08-18 15:16:36
【问题描述】:

我有一个应用程序,其客户端在 Android 上运行,服务器部署在 Google App Engine 上。该应用程序将使用用户的 Google 日历数据,并且需要对日历服务进行身份验证才能访问提要。我可以处理从客户端上的每个用户那里获取 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

即使库已正确链接。我在网上做了一些研究,发现一个帖子说这是因为应用引擎不再喜欢 gdata 库,并且更喜欢谷歌的新 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 进行身份验证并访问他们的日历提要?

【问题讨论】:

    标签: google-app-engine oauth google-calendar-api gdata-api


    【解决方案1】:

    好的,基本上我的第一次尝试是正确的,这仍然可以使用 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
    猜你喜欢
    • 2014-07-30
    • 2012-01-10
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    • 2012-04-05
    • 2013-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多