Created
July 3, 2012 18:37
-
-
Save danthorpe/3041742 to your computer and use it in GitHub Desktop.
Using Twitter reverse_auth and Parse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)signInWithTwitterAccount:(ACAccount *)account completion:(void (^)(Person *person, BOOL justSignedUp, NSError *error))completion { | |
// Use Twitter's reverse auth to get the user credentials | |
[TwitterHelper getCredentialsForAccount:account completion:^(NSDictionary *credentials, NSError *error) { | |
// Now sign in using Parse | |
[PFTwitterUtils logInWithTwitterId:(credentials)[TwitterCredentials.userId] screenName:(credentials)[TwitterCredentials.screenName] authToken:(credentials)[TwitterCredentials.oauthToken] authTokenSecret:(credentials)[TwitterCredentials.oauthTokenSecret] block:^(PFUser *user, NSError *error) { | |
// Check if we've got a user | |
if (user && !error) { | |
// Do something interesting with a newly signed in user. | |
Person *person = [Person personWithUser:user]; | |
completion(person, user.isNew, nil); | |
} else { | |
// Perform error handling | |
completion(nil, NO, error); | |
} | |
}]; | |
}]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a query | |
self.query = [PFQuery queryWithClassName:NSStringFromClass([GeoThings class])]; | |
// Add the geo constraint | |
[self.query whereKey:@"location" withinGeoBoxFromSouthwest:southwest toNortheast:northeast]; | |
// Order by timestamp | |
[self.query orderByDescending:@"timestamp"]; | |
// Perform the query | |
[self.query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { | |
// Do something cool with the objects | |
}]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (void)logInWithTwitterId:(NSString *)twitterId | |
screenName:(NSString *)screenName | |
authToken:(NSString *)authToken | |
authTokenSecret:(NSString *)authTokenSecret | |
block:(PFUserResultBlock)block; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// TwitterHelper.h | |
// | |
// Created by Daniel Thorpe on 23/05/2012. | |
// Copyright (c) 2012 Blinding Skies Limited. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
#import <ACAccounts/ACAccounts.h> | |
extern const struct TwitterCredentials { | |
__unsafe_unretained NSString *oauthToken; | |
__unsafe_unretained NSString *oauthTokenSecret; | |
__unsafe_unretained NSString *userId; | |
__unsafe_unretained NSString *screenName; | |
} TwitterCredentials; | |
@interface TwitterHelper : NSObject | |
+ (void)getCredentialsForAccount:(ACAccount *)account | |
completion:(void(^)(NSDictionary *credentials, NSError *error))completion; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// TwitterHelper.m | |
// | |
// Created by Daniel Thorpe on 23/05/2012. | |
// Copyright (c) 2012 Blinding Skies Limited. All rights reserved. | |
// | |
#import "STLOAuthClient.h" | |
#import "SecretKeys.h" | |
#import <Social/Social.h> | |
#import "DeviceHelper.h" | |
#import "TwitterHelper.h" | |
#define kTwitterApiRootURL [NSURL URLWithString:@"https://api.twitter.com/1/"] | |
const struct TwitterCredentials TwitterCredentials = { | |
.oauthToken = @"oauth_token", | |
.oauthTokenSecret = @"oauth_token_secret", | |
.userId = @"user_id", | |
.screenName = @"screen_name" | |
}; | |
@implementation TwitterHelper | |
+ (void)getCredentialsForAccount:(ACAccount *)account completion:(void(^)(NSDictionary *credentials, NSError *error))completion { | |
NSParameterAssert(completion); | |
// Configure the URL | |
NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/"]; | |
// Create a client | |
STLOAuthClient *client = [[STLOAuthClient alloc] initWithBaseURL:url consumerKey:kTwitterConsumerKey secret:kTwitterConsumerSecret]; | |
// Create other parameters | |
NSDictionary *params = @{ @"x_auth_mode" : @"reverse_auth" }; | |
[client getPath:@"oauth/request_token" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { | |
// Get the oauth response | |
NSString *oauth = operation.responseString; | |
// Configure the parameters | |
NSDictionary *params = @{ @"x_reverse_auth_target" : kTwitterConsumerKey, @"x_reverse_auth_parameters" : oauth }; | |
// Get the URL | |
NSURL *accessURL = [NSURL URLWithString:@"oauth/access_token" relativeToURL:url]; | |
// Create a request | |
TWRequest *request = [[TWRequest alloc] initWithURL:accessURL parameters:params requestMethod:TWRequestMethodPOST]; | |
// Set the account | |
request.account = account; | |
// Perform the request | |
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { | |
// Check for errors | |
if (responseData && !error) { | |
// Decode the response data into a string | |
NSString *response = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; | |
completion([TwitterHelper credentialsFromTwitterOAuthResponse:response], nil); | |
} else { | |
completion(nil, error); | |
} | |
}]; | |
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { | |
completion(nil, error); | |
}]; | |
} | |
+ (NSDictionary *)credentialsFromTwitterOAuthResponse:(NSString *)response { | |
// Divide the string by ampersands | |
NSArray *components = [response componentsSeparatedByString:@"&"]; | |
NSMutableDictionary *credentials = [NSMutableDictionary dictionaryWithCapacity:4]; | |
// Iterate through the components | |
for (NSString *component in components) { | |
// Split by = sign | |
NSRange divider = [component rangeOfString:@"="]; | |
NSString *key = [component substringToIndex:divider.location]; | |
NSString *val = [component substringFromIndex:divider.location+1]; | |
[credentials setObject:val forKey:key]; | |
} | |
return credentials; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment