Skip to content

Commit

Permalink
Access token can now be obtained.
Browse files Browse the repository at this point in the history
  • Loading branch information
lukhnos committed Oct 9, 2011
1 parent 54b2a13 commit fce8c19
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 9 deletions.
10 changes: 9 additions & 1 deletion Source/ObjectiveFlickr.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
64 changes: 56 additions & 8 deletions Source/ObjectiveFlickr.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ - (void)dealloc
[authEndpoint release];
[uploadEndpoint release];

[oauthToken release];
[oauthTokenSecret release];

[super dealloc];
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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]];
Expand Down

0 comments on commit fce8c19

Please sign in to comment.