iOS Twitter反向OAuth

Kyl*_*man 7 twitter ios slrequest

我已经在互联网上倾倒了几天,试图弄清楚如何实现这一点.

我需要从twitter请求访问令牌和秘密,以便将其传递给将为我的应用程序处理用户推文的服务器.

我一直关注此链接https://dev.twitter.com/docs/ios/using-reverse-auth

问题是第1步.他们没有给你一个步骤1的例子.

这是我的代码:

NSURL *url = [NSURL URLWithString:TW_OAUTH_URL_REQUEST_TOKEN];
NSDictionary *parameters = @{TW_X_AUTH_MODE_KEY:TW_X_AUTH_MODE_REVERSE_AUTH};

SLRequest *getTwitterAuth = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:url parameters:parameters];


//  Assume that we stored the result of Step 1 into a var 'resultOfStep1'

NSString *S = resultOfStep1;
NSDictionary *step2Params = [[NSMutableDictionary alloc] init];
[step2Params setValue:@"kfLxMJsk7fqIuy8URhleFg" forKey:@"x_reverse_auth_target"];
[step2Params setValue:S forKey:@"x_reverse_auth_parameters"];

NSURL *url2 = [NSURL URLWithString:@"https://api.twitter.com/oauth/access_token"];
SLRequest *stepTwoRequest =
[SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:url2 parameters:step2Params];

//  You *MUST* keep the ACAccountStore alive for as long as you need an ACAccount instance
//  See WWDC 2011 Session 124 for more info.
self.accountStore = [[ACAccountStore alloc] init];

//  We only want to receive Twitter accounts
ACAccountType *twitterType =
[self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

//  Obtain the user's permission to access the store
[self.accountStore requestAccessToAccountsWithType:twitterType
                             withCompletionHandler:^(BOOL granted, NSError *error) {
     if (!granted) {
         // handle this scenario gracefully
     } else {
         // obtain all the local account instances
         NSArray *accounts =
         [self.accountStore accountsWithAccountType:twitterType];

         // for simplicity, we will choose the first account returned - in your app,
         // you should ensure that the user chooses the correct Twitter account
         // to use with your application.  DO NOT FORGET THIS STEP.
         [stepTwoRequest setAccount:[accounts objectAtIndex:0]];

         // execute the request
         [stepTwoRequest performRequestWithHandler:
          ^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
              NSString *responseStr = 
              [[NSString alloc] initWithData:responseData 
                                    encoding:NSUTF8StringEncoding];

              // see below for an example response
              NSLog(@"The user's info for your server:\n%@", responseStr);
          }];
     } 
 }];
Run Code Online (Sandbox Code Playgroud)

我一直试图弄清楚如何处理oder中的SLRequest以将其从twitter文档传递到第2步.

我也在这里使用过:https://github.com/seancook/TWReverseAuthExample

这段代码很棒但非常复杂.任何帮助将不胜感激!谢谢!

Kyl*_*man 2

下面是一个类,可以通过单个方法调用来帮助完成此任务,该方法调用返回带有令牌和令牌秘密的字典。

https://github.com/kbegeman/Twitter-Reverse-Auth

希望这可以帮助其他人!

  • 它不起作用,虽然进入 if 块但凭证值 = "<?xml version" = "\"1.0\" 编码=\"UTF-8\"?>\n<errors>\n <error code=\ "101\">反向身份验证凭据无效</error>\n</errors>\n"; (2认同)