Skip to content

Commit

Permalink
URL request is now mutable.
Browse files Browse the repository at this point in the history
Support for extra OAuth parameters (e.g. linkedin's oauth_verification)
  • Loading branch information
chriseidhof committed Jun 27, 2012
1 parent a777311 commit 6632dae
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
9 changes: 5 additions & 4 deletions GCOAuth.h
Expand Up @@ -63,7 +63,7 @@
encode them yourself. The contents of the parameters dictionary must be string
key/value pairs. You are contracted to consume the NSURLRequest *immediately*.
*/
+ (NSURLRequest *)URLRequestForPath:(NSString *)path
+ (NSMutableURLRequest *)URLRequestForPath:(NSString *)path
GETParameters:(NSDictionary *)parameters
host:(NSString *)host
consumerKey:(NSString *)consumerKey
Expand All @@ -75,7 +75,7 @@
Performs the same operation as the above method but allows a customizable URL
scheme, e.g. HTTPS.
*/
+ (NSURLRequest *)URLRequestForPath:(NSString *)path
+ (NSMutableURLRequest *)URLRequestForPath:(NSString *)path
GETParameters:(NSDictionary *)parameters
scheme:(NSString *)scheme
host:(NSString *)host
Expand All @@ -89,13 +89,14 @@
data will be sent as form URL encoded. Restrictions on the arguments to this
method are the same as the GET request methods.
*/
+ (NSURLRequest *)URLRequestForPath:(NSString *)path
+ (NSMutableURLRequest *)URLRequestForPath:(NSString *)path
POSTParameters:(NSDictionary *)parameters
host:(NSString *)host
consumerKey:(NSString *)consumerKey
consumerSecret:(NSString *)consumerSecret
accessToken:(NSString *)accessToken
tokenSecret:(NSString *)tokenSecret;
tokenSecret:(NSString *)tokenSecret
oauthParameters:(NSDictionary*)oauthParameters;
@end

/*
Expand Down
29 changes: 19 additions & 10 deletions GCOAuth.m
Expand Up @@ -62,7 +62,8 @@ + (NSString *)queryStringFromParameters:(NSDictionary *)parameters;
- (id)initWithConsumerKey:(NSString *)consumerKey
consumerSecret:(NSString *)consumerSecret
accessToken:(NSString *)accessToken
tokenSecret:(NSString *)tokenSecret;
tokenSecret:(NSString *)tokenSecret
oauthParameters:(NSDictionary*)oauthParameters;

// generate a request
- (NSMutableURLRequest *)request;
Expand Down Expand Up @@ -94,17 +95,22 @@ @implementation GCOAuth
- (id)initWithConsumerKey:(NSString *)consumerKey
consumerSecret:(NSString *)consumerSecret
accessToken:(NSString *)accessToken
tokenSecret:(NSString *)tokenSecret {
tokenSecret:(NSString *)tokenSecret
oauthParameters:(NSDictionary*)oauthParameters {
self = [super init];
if (self) {
OAuthParameters = [[NSDictionary alloc] initWithObjectsAndKeys:

NSDictionary* libraryParameters = [NSDictionary dictionaryWithObjectsAndKeys:
[[consumerKey copy] autorelease], @"oauth_consumer_key",
[GCOAuth nonce], @"oauth_nonce",
[GCOAuth timeStamp], @"oauth_timestamp",
@"1.0", @"oauth_version",
@"HMAC-SHA1", @"oauth_signature_method",
[[accessToken copy] autorelease], @"oauth_token", // leave accessToken last or you'll break XAuth attempts
nil];
NSMutableDictionary* parameters = oauthParameters ? [oauthParameters mutableCopy] : [[NSMutableDictionary alloc] init];
[parameters addEntriesFromDictionary:libraryParameters];
OAuthParameters = parameters;
signatureSecret = [[NSString stringWithFormat:@"%@&%@", [consumerSecret pcen], [tokenSecret ?: @"" pcen]] retain];
}
return self;
Expand Down Expand Up @@ -227,7 +233,7 @@ + (NSString *)queryStringFromParameters:(NSDictionary *)parameters {
}];
return [entries componentsJoinedByString:@"&"];
}
+ (NSURLRequest *)URLRequestForPath:(NSString *)path
+ (NSMutableURLRequest *)URLRequestForPath:(NSString *)path
GETParameters:(NSDictionary *)parameters
host:(NSString *)host
consumerKey:(NSString *)consumerKey
Expand All @@ -243,7 +249,7 @@ + (NSURLRequest *)URLRequestForPath:(NSString *)path
accessToken:accessToken
tokenSecret:tokenSecret];
}
+ (NSURLRequest *)URLRequestForPath:(NSString *)path
+ (NSMutableURLRequest *)URLRequestForPath:(NSString *)path
GETParameters:(NSDictionary *)parameters
scheme:(NSString *)scheme
host:(NSString *)host
Expand All @@ -259,7 +265,8 @@ + (NSURLRequest *)URLRequestForPath:(NSString *)path
GCOAuth *oauth = [[GCOAuth alloc] initWithConsumerKey:consumerKey
consumerSecret:consumerSecret
accessToken:accessToken
tokenSecret:tokenSecret];
tokenSecret:tokenSecret
oauthParameters:nil];
oauth.HTTPMethod = @"GET";
oauth.requestParameters = parameters;

Expand All @@ -273,18 +280,19 @@ + (NSURLRequest *)URLRequestForPath:(NSString *)path
oauth.URL = [NSURL URLWithString:URLString];

// return
NSURLRequest *request = [oauth request];
NSMutableURLRequest *request = [oauth request];
[oauth release];
return request;

}
+ (NSURLRequest *)URLRequestForPath:(NSString *)path
+ (NSMutableURLRequest *)URLRequestForPath:(NSString *)path
POSTParameters:(NSDictionary *)parameters
host:(NSString *)host
consumerKey:(NSString *)consumerKey
consumerSecret:(NSString *)consumerSecret
accessToken:(NSString *)accessToken
tokenSecret:(NSString *)tokenSecret {
tokenSecret:(NSString *)tokenSecret
oauthParameters:(NSDictionary*)oauthParameters {

// check parameters
if (host == nil || path == nil) { return nil; }
Expand All @@ -293,7 +301,8 @@ + (NSURLRequest *)URLRequestForPath:(NSString *)path
GCOAuth *oauth = [[GCOAuth alloc] initWithConsumerKey:consumerKey
consumerSecret:consumerSecret
accessToken:accessToken
tokenSecret:tokenSecret];
tokenSecret:tokenSecret
oauthParameters:oauthParameters];
oauth.HTTPMethod = @"POST";
oauth.requestParameters = parameters;
NSURL *URL = [[NSURL alloc] initWithScheme:@"https" host:host path:path];
Expand Down

0 comments on commit 6632dae

Please sign in to comment.