iOS Twitter 反向 OAuth

标签 ios twitter slrequest

几天来,我一直在网上浏览,试图弄清楚如何实现这一点。

我需要从 Twitter 请求访问 token 和密码,以便将其传递给服务器,该服务器将为我的应用程序处理用户推文。

我一直关注这个链接 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);
          }];
     } 
 }];

我一直在努力弄清楚如何处理 SLRequest,以便将其传递到 Twitter 文档中的第 2 步。

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

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

最佳答案

步骤 1 没有任何代码的原因是他们假设您将在您的服务器上或之前或类似的东西上执行此操作。基本上,您需要生成一个 key ,您的应用程序将使用该 key 将 iOS token 转换为普通 token 。

这里有一个脚本可以为您发出请求:http://www.ananseproductions.com/twitter-reverse-auth-headaches/它是用 ruby​​ 编写的,因此如果您有 ruby​​ 服务器,您可以使用类似的东西。

就我个人而言,我会让我的应用程序从我的服务器请求此 token ,然后向 Twitter 发出请求,然后将新 token 发布回我的服务器。

关于iOS Twitter 反向 OAuth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16862023/

相关文章:

ios - 更改主屏幕快速操作图标位置

ios - UIActionSheet 在第二次单击按钮时关闭

ios - 如何使用 SLRequest 通过 iOS 更改为通过 <AppName>

ios - iOS 发出网络请求时的权限警报

ios - Swift 3.0 无法获取所需的对象指针(替换为 NULL): Couldn't load 'self' because its value couldn't be evaluated

iphone - 在 iOS 6.0 中,视频从应用程序后台播放到前台需要 2 秒从暂停到播放滞后

laravel - Twitter OAuth 访问 token 错误 : Request token missing

python - 使用计算成本低廉的 Python 哈希算法检测转推

android - Twitter OAuth 在 Android 中是如何工作的?

ios - 为什么当我在应用程序加载后将项目添加到 Core Data 时,它们直到我点击单元格才会显示? (然后只有那个显示。)