I am trying to integrate an iOS 开发者_Go百科application with Google App Engine using OAuth. I found that GTM has an OAuth Controller -- http://code.google.com/p/gtm-oauth/
Can it be used to connect to Google App Engine? If so, what do I put as the "scope" parameter in
- (id)initWithScope:(NSString *)scope
language:(NSString *)language
appServiceName:(NSString *)keychainAppServiceName
delegate:(id)delegate
finishedSelector:(SEL)finishedSelector;
I've tried to use my App Engine application's address (http://my-app-name.appspot.com) but it didn't work.
Thanks in advance!
btw this is sort of a follow up question to Authenticating into Google App Engine from an iOS device.
A late answer I know, but hopefully this will help someone:
MYSITE can be something like thunderofthor.com
Step 1:
Set up 2-legged OAuth for the App Engine domain of interest. To do this, log in as admin to https://www.google.com/a/MYSITE. Under advanced tools, click Manage OAuth domain key. Here, click 'Enable this consumer key' and 'Allow access to all APIs' to enable those options. On Google's side, you can now handle requests that use the MYSITE consumer key and the OAUTHCONSUMERSECRET.
Step 2:
In your servlet code, you can confirm the client has the correct credentials by requesting the OAuthConsumerKey which will be MYSITE
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String user = null;
try {
OAuthService oauth = OAuthServiceFactory.getOAuthService();
user = oauth.getOAuthConsumerKey();
LOG.info("Authenticated: " + user);
} catch (OAuthRequestException e) {
LOG.info("Not authenticated: " + e.getMessage());
}
Step 3:
Download the GTMOAuth package from Google. It will allow iOS to talk to your server effortlessly. You won't need the whole package for 2-legged auth. In fact, all you need is the GTMOAuthAuthentication files. To use in your code, do something like the following:
NSURL *url = [NSURL URLWithString:@"https://MYSITE/dosomething"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
GTMOAuthAuthentication *auth = [[GTMOAuthAuthentication alloc] initWithSignatureMethod:kGTMOAuthSignatureMethodHMAC_SHA1 consumerKey:@"MYSITE" privateKey:@"OAUTHCONSUMERSECRET"] ;
[auth setVersion:@"1.0"];
[auth addRequestTokenHeaderToRequest:request];
// Perform request and get JSON back as a NSData object
NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
There! Effortless secure communication without requiring usernames and passwords!
精彩评论