在 Google App Engine 上使用 OAuth 令牌的过程
我有一个应用程序,其客户端在 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,所以这似乎是我的错误的根源。
所以现在我来向大家寻求帮助来解决这个问题。根据我上面讨论的内容,如何使用每个用户拥有的访问令牌和令牌机密来向 Google 进行身份验证并访问他们的日历源?
I have an application with a client side running on Android and the server deployed on the Google App Engine. The application will make use of the users Google Calendar data and needs to authenticate to the Calendar service in order to access the feeds. I can handle obtaining the OAuth tokens and token secrets from each user on the client and persist them into the database on the server to use when accessing the feeds. It when I try to use them to authenticate with the Calendar service I am running into trouble.
I was at first using the gdata libraries to do this 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();
However, this caused exceptions of:
java.lang.NoClassDefFoundError: com/google/gdata/client/authn/oauth/OAuthParameters
even though the libraries were correctly linked. I did some reaearch online and found a post saying it as because app engine doesn't like the gdata libraries anymore and prefesrs Google's newer google-api-java-client, Google APIs Client Library for Java
So I re-wrote the code to use this library instead hoping it would solve my problem. Using their sample code and replacing with the proper credentials:
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);
However now I was getting an error with the call to GoogleTransport.create(). I looked into the api for GoogleTransport and read: Warning: scheduled in version 1.1 to no longer extend HttpTransport, so it seems this is the source of my error.
So now I come to you all for help with this problem. Based on what I have discussed above, and how can I use the access tokens and token secrets I have for each user to authenticate to Google and access their Calendar feeds?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,基本上我的第一次尝试是正确的,这仍然可以使用 gdata 库来完成。问题是,当将项目上传到其托管站点时,Eclipse 没有在我的构建路径中包含一些库。我也没有包含位于 deps 依赖项文件夹中的
google-collect-1.0-rc1.jar
,这是必须的。如果这个问题困扰其他人,请确保涵盖了我上面讨论的内容,并且以下代码应该适合您根据需要进行修改:Ok, basically my first attempt was correct and this can still be done using the gdata libraries. The problem was that Eclipse wasn't including a few of the libraries in my build path along for the ride when uploading the project to its hosted site. I also did not include
google-collect-1.0-rc1.jar
located in the deps dependencies folder which is a must. In case this problem troubles anyone else, make sure what I discussed above is covered and the following code should work for you to modify as you need: