7

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

我需要从 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);
          }];
     } 
 }];

我一直在试图弄清楚我如何处理 SLRequest 以便将其从 twitter 文档传递到第 2 步。

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

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

4

4 回答 4

3

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

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

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

于 2013-07-17T18:55:45.480 回答
2

这是一个类,通过一个方法调用来帮助实现这一点,该方法调用返回一个带有令牌和令牌秘密的字典。

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

希望这可以帮助其他人!

于 2013-12-18T11:35:19.897 回答
2

截至此代码https://github.com/seancook/TWReverseAuthExample,在您自己的应用程序中实现相当简单。我更喜欢创建可重用的类,所以我不必多次实现相同的代码。通常你会创建一些单例并在下面的教程中使用它。但是,本说明的重点不是教您如何创建单例,因此为了简单起见,我们将使用AppDelegate.h/m在整个应用程序中都可以轻松访问的。

您所要做的就是:

  1. 打开你和 Sean Cook 的项目(上面是 URL 的那个)

  2. 将 Source->Vendor->ABOauthCore 组拖放到您的项目中

  3. 选择TWAPIManager.h/mTWSignedRequest.h/m然后将它们复制到您的项目中

  4. 将以下代码添加到您的 AppDelegate.h 文件中

    @property (nonatomic, strong) ACAccountStore* 存储;

    @property (nonatomic, strong) TWAPIManager *apiManager;

    @property (nonatomic, strong) NSArray *accounts;

    -(void)storeAccountWithAccessToken:(NSString *)token secret:(NSString *)secret;

    -(void)performReverseAuth:(id)sender inView:(UIView*)viewToDisplaySheet;

    -(void)_refreshTwitterAccounts;

  5. 现在将以下方法粘贴到您的AppDelegate.m文件中

    -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;

    -(void)_refreshTwitterAccounts;

    -(void)_obtainAccessToAccountsWithBlock:(void (^)(BOOL))块;

    -(void)performReverseAuth:(id)sender inView:(UIView*)viewToDisplaySheet;

  6. 在文件的某些初始化方法中,或在此示例中:`application: didFinishLaunchingWithOptions' 粘贴以下代码:

    _store = [[ACAccountStore alloc] init];

    _apiManager = [[TWAPIManager alloc] init];

    [[NSNotificationCenter defaultCenter] addObserver:self 选择器:@selector(_refreshTwitterAccounts) 名称:ACAccountStoreDidChangeNotification 对象:nil];

  7. 请记住使用以下代码删除观察者。将其粘贴到AppDelegate.m

    -(无效)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self];
    

    }

  8. 打开您的app-Info.plist文件并添加 2 个字符串键。取自:https ://apps.twitter.com/

    TWITTER_CONSUMER_KEY

    TWITTER_CONSUMER_SECRET

  9. 在要用于实现 twitter 功能的 View Controller 中,在viewDidLoad方法中添加以下代码:

    AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;

    [appDelegate _refreshTwitterAccounts];

  10. OK,终于可以开始整机了。在要用于实现 twitter 功能的 View Controller 中,创建UIButton调用_reverseAuthBtn并创建IBAction它。然后在你的IBAction粘贴下面的代码:

    AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;

    [appDelegate performReverseAuth:sender inView:self.view];

哇,我想就是这样!如果我没有忘记任何事情,您已经获得了 Twitter 反向 Oauth 实现,如果您想在多个视图控制器中使用它,您所要做的就是执行步骤 1-8,然后粘贴步骤 9 中的代码和 10 进入您的视图控制器。

此致!

于 2014-02-23T23:51:33.747 回答
1

使用这个库,它工作得很好!

https://github.com/nst/STTwitter

信息如何实施:https ://github.com/nst/STTwitter#reverse-authentication

:)

于 2014-09-03T17:31:11.493 回答