Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save moflo/972353 to your computer and use it in GitHub Desktop.
Save moflo/972353 to your computer and use it in GitHub Desktop.
Using the Google iOS OAuth2 library for both Twitter and LinkedIn
- (void)signIn:(int)socialNetworkType {
//! Method to launch authorization modal dialog for Twitter or LinkedIn
NSURL *requestURL;
NSURL *authorizeURL;
NSURL *accessURL;
NSString *scope;
if (socialNetworkType == kSignupSocialLinkedIn) {
// LinkedIn OAuth token request URLs
requestURL = [NSURL URLWithString:@"https://api.linkedin.com/uas/oauth/requestToken"];
authorizeURL = [NSURL URLWithString:@"https://api.linkedin.com/uas/oauth/authorize"];
accessURL = [NSURL URLWithString:@"https://api.linkedin.com/uas/oauth/accessToken"];
scope = @"http://api.linkedin.com/";
}
if (socialNetworkType == kSignupSocialTwitter) {
// Twitter OAuth token request URLs
requestURL = [NSURL URLWithString:@"https://api.twitter.com/oauth/request_token"];
authorizeURL = [NSURL URLWithString:@"https://api.twitter.com/oauth/authorize"];
accessURL = [NSURL URLWithString:@"https://api.twitter.com/oauth/access_token"];
scope = @"http://api.twitter.com/";
}
// Need to request the authorization object, load your app's API tokens
GDataOAuthAuthentication *auth = [self authFor:socialNetworkType];
// Try to get authorization from keychain, if user has previously enabled access...
BOOL didAuthFromKeychain = NO;
if (socialNetworkType == kSignupSocialLinkedIn) {
didAuthFromKeychain = [GDataOAuthViewControllerTouch authorizeFromKeychainForName:kLinkedInAppServiceName authentication:auth];
}
if (socialNetworkType == kSignupSocialTwitter) {
didAuthFromKeychain = [GDataOAuthViewControllerTouch authorizeFromKeychainForName:kTwitterAppServiceName authentication:auth];
}
if (!didAuthFromKeychain) {
// Error: could not get authorization from keyChain, so set up a login dialog
// Always save value in keyChain
NSString *keychainAppServiceName;
if (socialNetworkType == kSignupSocialLinkedIn) {
[auth setCallback:@"http://www.linkedin.com/success"]; // set the callback URL
keychainAppServiceName = kLinkedInAppServiceName;
}
if (socialNetworkType == kSignupSocialTwitter) {
[auth setCallback:@"http://www.twitter.com/success"]; // set the callback URL
keychainAppServiceName = kTwitterAppServiceName;
}
// Display the autentication view.
GDataOAuthViewControllerTouch *viewController;
viewController = [[[GDataOAuthViewControllerTouch alloc] initWithScope:scope
language:nil
requestTokenURL:requestURL
authorizeTokenURL:authorizeURL
accessTokenURL:accessURL
authentication:auth
appServiceName:keychainAppServiceName
delegate:self
finishedSelector:@selector(viewController:finishedWithAuth:error:)] autorelease];
[[self navigationController] pushViewController:viewController animated:YES];
}
else {
// Authentication succeeded
// save the authentication object, which holds the auth tokens
[self setAuthentication:auth];
if (signupSocialNetworkType == kSignupSocialLinkedIn) {
[self doAnAuthenticatedAPIFetchLinkedIn];
}
if (signupSocialNetworkType == kSignupSocialTwitter) {
[self doAnAuthenticatedAPIFetchTwitter];
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment