views:

99

answers:

0

I'm trying to authenticate with oauth for the formspring.me website using the oauthconsumer framework. I'm successful getting the request token (see 'getRequestToken'). I show them the authorization URL and have them authenticate. I don't see the callback url ever being requested in the webview (even though I believe I specify it with the request token). Once they hit 'Allow' (the callback url is never reached regardless of authorization) I try to get an access token but I don't get these access tokens.

- (NSString *) getRequestToken{


    OAConsumer *consumer = [[[OAConsumer alloc] initWithKey:kConsumerKey secret:kConsumerSecret] autorelease];
    OAMutableURLRequest *request = [[[OAMutableURLRequest alloc] initWithURL:[NSURL URLWithString:kRequestToken] consumer:consumer token:nil realm:nil signatureProvider:nil] autorelease]; 
    [request setHTTPMethod:@"POST"];
    OARequestParameter *callback = [[[OARequestParameter alloc] initWithName:@"oauth_callback" value:@"http://callback"] autorelease];  
    [request setParameters:[NSArray arrayWithObjects: callback, nil]];
    [request prepare];
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *string = [[[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding] autorelease];
    OAToken *requestToken = [[[OAToken alloc] initWithHTTPResponseBody:string] autorelease];

    NSLog(@"%@valid",[requestToken isValid]? @"" : @"Not ");

    if([requestToken.key length] > 0 && [requestToken.secret length] > 0){

        self.tokenKey = requestToken.key;
        self.tokenSecret = requestToken.secret;

        return [NSString stringWithFormat:@"%@?oauth_token=%@",kAuthorizeURL,self.tokenKey];

        // authenticate then call 'getAccessToken'
        //[self getAccessToken];

    }
    return nil;

}


- (void) getAccessToken{

    OAConsumer *consumer = [[[OAConsumer alloc] initWithKey:kConsumerKey secret:kConsumerSecret] autorelease];
    OAToken *token = [[OAToken alloc] initWithKey:self.tokenKey secret:self.tokenSecret]; 



    OAMutableURLRequest *request = [[[OAMutableURLRequest alloc] initWithURL:[NSURL URLWithString:kAccessTokenURL] 
                                                                     consumer:consumer 
                                                                        token:token 
                                                                        realm:nil signatureProvider:nil] autorelease]; 


    [request setHTTPMethod:@"POST"];
    [request prepare];

    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *string = [[[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding] autorelease];
    OAToken *accessToken = [[[OAToken alloc] initWithHTTPResponseBody:string] autorelease];


    if([accessToken.key length] > 0 && [accessToken.secret length]>0){
        // never reached!!!
        self.tokenKey = accessToken.key;
        self.tokenSecret = accessToken.secret;
    }


}