我正在尝试为 iOS 和 Temboo 中的用户授权 Twitter。这就是我试图做到的方式。
在我的 SetttingsViewController 类中,我有一个按钮,用户可以点击该按钮开始 TwitterAuthentication。我在一个名为 TwitterClient 的类中有 InitializeOauth 方法,在一个名为 TwitterFClient 的类中有 FinalizeOauth 方法。这是我的课。
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]);
}
这是两个客户端类。我相信在 Facebook 的例子中,它们都结合了,这会是建议的做法吗?
无论如何,这是从 SettingsViewController 类中调用方法的方式:
设置视图控制器.m
TwitterClient *twitter = [[TwitterClient alloc] init];
[twitter runInitializeOAuthChoreo];
当用户从 Web 浏览器返回时,就是我调用 FinalizeOAuth 方法的时候。这是 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;
}
我假设有一种更简单的方法可以做到这一点,并且我愿意接受剧烈的代码更改!