Twitter OAuth and accessToken to GET statuses/user_timeline in Objective-C

前端 未结 3 1015
情书的邮戳
情书的邮戳 2020-12-08 08:53

I\'m trying to acquire the accessToken value from Twitter for using it in my app (I need to use API v1.1\'s Authentication Model for GET statuses/user_timeline

相关标签:
3条回答
  • 2020-12-08 09:34

    STTwitter is simple for use . Also I added example that you can get public twitter timeline

    0 讨论(0)
  • 2020-12-08 09:36

    It's been a few days since Twitter added an application only mode. In this mode, you can access specific API endpoints without user's context, ie. your users don't have to authenticate. Only the application does authenticate once in a while to get a "bearer token" which is then sent in every request.

    The following code gets a bearer token and then retrieves the public timeline for @barackobama.

    STTwitterAPI *twitter = [STTwitterAPI twitterAPIApplicationOnlyWithConsumerKey:@""
                                                                    consumerSecret:@""];
    
    [twitter verifyCredentialsWithSuccessBlock:^(NSString *bearerToken) {
    
        NSLog(@"Access granted with %@", bearerToken);
    
        [twitter getUserTimelineWithScreenName:@"barackobama" successBlock:^(NSArray *statuses) {
            NSLog(@"-- statuses: %@", statuses);
        } errorBlock:^(NSError *error) {
            NSLog(@"-- error: %@", error);
        }];
    
    } errorBlock:^(NSError *error) {
        NSLog(@"-- error %@", error);
    }];
    

    See STTwitter iOS demo project for a working example (use you own consumer key / secret).

    0 讨论(0)
  • 2020-12-08 09:41

    If you have your own consumer tokens, you can use the STTwitter library I wrote.

    STTwitterAPI *twitter =
      [STTwitterAPI twitterAPIWithOAuthConsumerName:@""
                                        consumerKey:@"your_key"
                                     consumerSecret:@"your_secret"
                                           username:@"username"
                                           password:@"password"];
    
    [twitter verifyCredentialsWithSuccessBlock:^(NSString *username) {
    
        NSLog(@"Access granted for %@", username);
    
        [twitter getUserTimelineWithScreenName:@"barackobama"
                                  successBlock:^(NSArray *statuses) {
            NSLog(@"-- statuses: %@", statuses);
        } errorBlock:^(NSError *error) {
            NSLog(@"-- error: %@", error);
        }];
    
    } errorBlock:^(NSError *error) {
        NSLog(@"-- error %@", error);
    }];
    

    If your tokens are not 'xAuth' enabled, you'll have to use a PIN. STTwitter provides simple asynchronous block-based methods to ask the user. You can also choose to automate the PIN retrieval, see STTwitterDemo -[AppDelegate guessPIN:] for an automated process.

    0 讨论(0)
提交回复
热议问题