开发者

Procedure for using OAuth tokens on Google App Engine

开发者 https://www.devze.com 2023-03-03 11:26 出处:网络
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

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.setOAu开发者_运维知识库thConsumerSecret(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?


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:

    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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号