Trouble with OAuth in Temboo

132 views Asked by At

I am trying to authorize Twitter For a user in iOS and Temboo. Here is how I am attempting to do it.

In my SetttingsViewController class I have a button where the user can tap to begin the TwitterAuthentication. I have the InitializeOauth methods in a class called TwitterClient, and the FinalizeOauth methods in a class called TwitterFClient. Here are my classes.

TwitterClient.m

-(void)runInitializeOAuthChoreo {
    // Instantiate the Choreo, using a previously instantiated TembooSession object, eg:
    TMBTembooSession *session = [[TMBTembooSession alloc] initWithAccount:@"prnk28" appKeyName:@"Floadt" andAppKeyValue:@"9b9031d182d7441da05f8214ba2c7170"];

    // Create the choreo object using your Temboo session
    TMBTwitter_OAuth_InitializeOAuth *initializeOAuthChoreo = [[TMBTwitter_OAuth_InitializeOAuth alloc] initWithSession:session];

    // Get Inputs object for the choreo
    TMBTwitter_OAuth_InitializeOAuth_Inputs *initializeOAuthInputs = [initializeOAuthChoreo newInputSet];

    // Set credential to use for execution
    [initializeOAuthInputs setCredential:@"Twitter"];

    // Set inputs
    [initializeOAuthInputs setForwardingURL:@"floadt://success"];

    // Execute choreo specifying this class as the choreo delegate
    [initializeOAuthChoreo executeWithInputs:initializeOAuthInputs delegate:self];
}

// TMBChoreographyDelegate method implementation - handle choreo errors
-(void)choreographyDidFailWithError:(NSError*)error {
    // Log error to the console
    NSLog(@"Error - %@", error);
}

// TMBChoreographyDelegate method implementation - choreo executed successfully
-(void)choreographyDidFinishExecuting:(TMBTwitter_OAuth_InitializeOAuth_ResultSet*)result {
    // Log results to the console
   // NSLog(@"%@", [result getAuthorizationURL]);
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[result getAuthorizationURL]]];

    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];

    NSString *callbackid = [result getCallbackID];
    [user setObject:callbackid forKey:@"TwitterCallbackID"];
    [user synchronize];

    NSString *oauthtokensecret = [result getOAuthTokenSecret];
    [user setObject:oauthtokensecret forKey:@"TwitterTemporaryOAuth"];
    [user synchronize];

}

TwitterFClient.m

-(void)runFinalizeOAuthChoreo {
    // Instantiate the Choreo, using a previously instantiated TembooSession object, eg:
    TMBTembooSession *session = [[TMBTembooSession alloc] initWithAccount:@"prnk28" appKeyName:@"Floadt" andAppKeyValue:@"9b9031d182d7441da05f8214ba2c7170"];

    // Create the choreo object using your Temboo session
    TMBTwitter_OAuth_FinalizeOAuth *finalizeOAuthChoreo = [[TMBTwitter_OAuth_FinalizeOAuth alloc] initWithSession:session];

    // Get Inputs object for the choreo
    TMBTwitter_OAuth_FinalizeOAuth_Inputs *finalizeOAuthInputs = [finalizeOAuthChoreo newInputSet];

    // Set credential to use for execution
    [finalizeOAuthInputs setCredential:@"Twitter"];

    // Set inputs
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *tCall = [user stringForKey:@"TwitterCallbackID"];
    NSString *tTAuth = [user stringForKey:@"TwitterTemporaryOAuth"];
    [finalizeOAuthInputs setOAuthTokenSecret:tCall];
    [finalizeOAuthInputs setCallbackID:tTAuth];
    [finalizeOAuthInputs setConsumerKey:TWITTER_CONSUMER_KEY];
    [finalizeOAuthInputs setConsumerSecret:TWITTER_CONSUMER_SECRET];

    // Execute choreo specifying this class as the choreo delegate
    [finalizeOAuthChoreo executeWithInputs:finalizeOAuthInputs delegate:self];
}

// TMBChoreographyDelegate method implementation - handle choreo errors
-(void)choreographyDidFailWithError:(NSError*)error {
    // Log error to the console
    NSLog(@"Error - %@", error);
}

// TMBChoreographyDelegate method implementation - choreo executed successfully
-(void)choreographyDidFinishExecuting:(TMBTwitter_OAuth_FinalizeOAuth_ResultSet*)result {
    // Log results to the console
    NSLog(@"%@", [result getAccessTokenSecret]);
    NSLog(@"%@", [result getAccessToken]);
    NSLog(@"%@", [result getScreenName]);
    NSLog(@"%@", [result getUserID]);
}

Those are the two client classes. I believe that in the Facebook example they are both combined would that be the suggested practice?

Anyway this is how the methods are called from within the SettingsViewController Class:

SettingsViewController.m

TwitterClient *twitter = [[TwitterClient alloc] init];
[twitter runInitializeOAuthChoreo];

When the user returns from the Web Browser is when I call the FinalizeOAuth Method. Here is how it is stated in the AppDelegate.

AppDelegate.m

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{

    NSString *daURL = [url absoluteString];
 //   NSString *instagram;
    NSString *twitter;

    twitter = [daURL substringWithRange: NSMakeRange (0, 16)];
   //  instagram = [daURL substringWithRange:NSMakeRange(0, 27)];

    if ([daURL isEqual: @"floadt://success"]) {
        TwitterFClient *fo = [[TwitterFClient alloc] init];
        [fo runFinalizeOAuthChoreo];
    }else{
        [[InstagramClient sharedClient] handleOAuthCallbackWithURL:url];
    }

    return YES;
}

Im under the assumption that there is a much simpler way to do this and I would be open to drastic code change!

1

There are 1 answers

0
Madbreaks On BEST ANSWER

It looks like you have these values reversed:

[finalizeOAuthInputs setOAuthTokenSecret:tCall];
[finalizeOAuthInputs setCallbackID:tTAuth];

Also, in the finalize step you're specifying your Twitter credential, but then you're also doing this:

[finalizeOAuthInputs setConsumerKey:TWITTER_CONSUMER_KEY];
[finalizeOAuthInputs setConsumerSecret:TWITTER_CONSUMER_SECRET];

That's unnecessary, since those values are stored in your credential (as evidenced by your use of it in the initialize-oauth flow). You can remove those two lines altogether. If they for some reason didn't match, your choreo execution would fail.