From fce8c191e75726a90130c1571ecae78a0fbc2e8a Mon Sep 17 00:00:00 2001 From: "Lukhnos D. Liu" Date: Sun, 9 Oct 2011 12:40:20 -0700 Subject: [PATCH] Access token can now be obtained. --- Source/ObjectiveFlickr.h | 10 ++++++- Source/ObjectiveFlickr.m | 64 +++++++++++++++++++++++++++++++++++----- 2 files changed, 65 insertions(+), 9 deletions(-) diff --git a/Source/ObjectiveFlickr.h b/Source/ObjectiveFlickr.h index a98eec5..deeb7fb 100644 --- a/Source/ObjectiveFlickr.h +++ b/Source/ObjectiveFlickr.h @@ -77,6 +77,9 @@ extern NSString *const OFFlickrDeletePermission; @property (nonatomic, retain) NSString *photoWebPageSource; @property (nonatomic, retain) NSString *authEndpoint; @property (nonatomic, retain) NSString *uploadEndpoint; + +@property (nonatomic, retain) NSString *OAuthToken; +@property (nonatomic, retain) NSString *OAuthTokenSecret; #else - (void)setAuthToken:(NSString *)inAuthToken; @@ -95,6 +98,11 @@ extern NSString *const OFFlickrDeletePermission; - (void)setUploadEndpoint:(NSString *)inEndpoint; - (NSString *)uploadEndpoint; +- (void)setOAuthToken:(NSString *)inToken; +- (NSString *)OAuthToken; + +- (void)setOAuthTokenSecret:(NSString *)inTokenSecret; +- (NSString *)OAuthTokenSecret; #endif @@ -136,7 +144,7 @@ extern NSString *const OFFetchOAuthAccessTokenSession; - (void)flickrAPIRequest:(OFFlickrAPIRequest *)inRequest imageUploadSentBytes:(unsigned int)inSentBytes totalBytes:(unsigned int)inTotalBytes; #endif -- (void)flickrAPIRequest:(OFFlickrAPIRequest *)inRequest didObtainOAuthRequestToken:(NSString *)inRequestToken; +- (void)flickrAPIRequest:(OFFlickrAPIRequest *)inRequest didObtainOAuthRequestToken:(NSString *)inRequestToken secret:(NSString *)inSecret; - (void)flickrAPIRequest:(OFFlickrAPIRequest *)inRequest didObtainOAuthAccessToken:(NSString *)inAccessToken secret:(NSString *)inSecret userFullName:(NSString *)inFullName userName:(NSString *)inUserName userNSID:(NSString *)inNSID; @end diff --git a/Source/ObjectiveFlickr.m b/Source/ObjectiveFlickr.m index 9d8204e..98639c9 100644 --- a/Source/ObjectiveFlickr.m +++ b/Source/ObjectiveFlickr.m @@ -77,6 +77,9 @@ - (void)dealloc [authEndpoint release]; [uploadEndpoint release]; + [oauthToken release]; + [oauthTokenSecret release]; + [super dealloc]; } @@ -226,6 +229,30 @@ - (NSString *)uploadEndpoint return uploadEndpoint; } +- (void)setOAuthToken:(NSString *)inToken +{ + NSString *tmp = oauthToken; + oauthToken = [inToken copy]; + [tmp release]; +} + +- (NSString *)OAuthToken +{ + return oauthToken; +} + +- (void)setOAuthTokenSecret:(NSString *)inSecret; +{ + NSString *tmp = oauthTokenSecret; + oauthTokenSecret = [inSecret copy]; + [tmp release]; +} + +- (NSString *)OAuthTokenSecret +{ + return oauthTokenSecret; +} + #if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4 @synthesize key; @synthesize sharedSecret; @@ -573,26 +600,47 @@ - (BOOL)uploadImageStream:(NSInputStream *)inImageStream suggestedFilename:(NSSt - (void)httpRequestDidComplete:(LFHTTPRequest *)request { if ([request sessionInfo] == OFFetchOAuthRequestTokenSession) { + [request setSessionInfo:nil]; + NSString *response = [[[NSString alloc] initWithData:[request receivedData] encoding:NSUTF8StringEncoding] autorelease]; - NSLog(@"%@ response: %@", sessionInfo, response); - if (![response hasPrefix:@"oauth_callback_confirmed=true"]) { + NSDictionary *params = OFExtractURLQueryParameter(response); + NSString *oat = [params objectForKey:@"oauth_token"]; + NSString *oats = [params objectForKey:@"oauth_token_secret"]; + if (!oat || !oats) { NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:response, OFFlickrAPIRequestOAuthErrorUserInfoKey, nil]; NSError *error = [NSError errorWithDomain:OFFlickrAPIRequestErrorDomain code:OFFlickrAPIRequestOAuthError userInfo:userInfo]; - [delegate flickrAPIRequest:self didFailWithError:error]; + [delegate flickrAPIRequest:self didFailWithError:error]; } else { - NSAssert([delegate respondsToSelector:@selector(flickrAPIRequest:didObtainOAuthRequestToken:)], @"Delegate must implement the method -flickrAPIRequest:didObtainOAuthRequestToken: to handle OAuth request token callback"); + NSAssert([delegate respondsToSelector:@selector(flickrAPIRequest:didObtainOAuthRequestToken:secret:)], @"Delegate must implement the method -flickrAPIRequest:didObtainOAuthRequestToken:secret: to handle OAuth request token callback"); - NSDictionary *params = OFExtractURLQueryParameter(response); - [delegate flickrAPIRequest:self didObtainOAuthRequestToken:[params objectForKey:@"oauth_token"]]; + [delegate flickrAPIRequest:self didObtainOAuthRequestToken:oat secret:oats]; } } else if ([request sessionInfo] == OFFetchOAuthAccessTokenSession) { + [request setSessionInfo:nil]; NSString *response = [[[NSString alloc] initWithData:[request receivedData] encoding:NSUTF8StringEncoding] autorelease]; - NSLog(@"%@ response: %@", sessionInfo, response); - + NSDictionary *params = OFExtractURLQueryParameter(response); + + + NSString *fn = [params objectForKey:@"fullname"]; + NSString *oat = [params objectForKey:@"oauth_token"]; + NSString *oats = [params objectForKey:@"oauth_token_secret"]; + NSString *nsid = [params objectForKey:@"user_nsid"]; + NSString *un = [params objectForKey:@"username"]; + if (!fn || !oat || !oats || !nsid || !un) { + NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:response, OFFlickrAPIRequestOAuthErrorUserInfoKey, nil]; + NSError *error = [NSError errorWithDomain:OFFlickrAPIRequestErrorDomain code:OFFlickrAPIRequestOAuthError userInfo:userInfo]; + [delegate flickrAPIRequest:self didFailWithError:error]; + } + + else { + NSAssert([delegate respondsToSelector:@selector(flickrAPIRequest:didObtainOAuthAccessToken:secret:userFullName:userName:userNSID:)], @"Delegate must implement -flickrAPIRequest:didObtainOAuthAccessToken:secret:userFullName:userName:userNSID: to handle the obtained access token"); + + [delegate flickrAPIRequest:self didObtainOAuthAccessToken:oat secret:oats userFullName:fn userName:un userNSID:nsid]; + } } else { NSDictionary *responseDictionary = [OFXMLMapper dictionaryMappedFromXMLData:[request receivedData]];