Accessing Twitter Direct Messages using SLRequest iOS

余生颓废 提交于 2019-11-30 15:54:23

Here is how to access direct messages for the iOS default Twitter account.

This example uses the STTwitter library, which internally uses SLRequest for phase 2 and a custom crafted request for phase 1.

NSString *CONSUMER_KEY = @"";
NSString *CONSUMER_SECRET = @"";

STTwitterAPI *twitter = [STTwitterAPI twitterAPIWithOAuthConsumerName:nil
                                                          consumerKey:CONSUMER_KEY
                                                       consumerSecret:CONSUMER_SECRET];

[twitter postReverseOAuthTokenRequest:^(NSString *authenticationHeader) {

    STTwitterAPI *twitterAPIOS = [STTwitterAPI twitterAPIOSWithFirstAccount];

    [twitterAPIOS verifyCredentialsWithSuccessBlock:^(NSString *username) {

        [twitterAPIOS postReverseAuthAccessTokenWithAuthenticationHeader:authenticationHeader
                                                            successBlock:^(NSString *oAuthToken,
                                                                           NSString *oAuthTokenSecret,
                                                                           NSString *userID,
                                                                           NSString *screenName) {

                                                                STTwitterAPI *x = [STTwitterAPI twitterAPIWithOAuthConsumerName:nil
                                                                                                                    consumerKey:CONSUMER_KEY
                                                                                                                 consumerSecret:CONSUMER_SECRET
                                                                                                                     oauthToken:oAuthToken
                                                                                                               oauthTokenSecret:oAuthTokenSecret];

                                                                [x verifyCredentialsWithSuccessBlock:^(NSString *username) {

                                                                    [x getDirectMessagesSinceID:nil count:10 successBlock:^(NSArray *messages) {
                                                                        // ...
                                                                    } errorBlock:^(NSError *error) {
                                                                        // ...
                                                                    }];

                                                                } errorBlock:^(NSError *error) {
                                                                    // ...
                                                                }];


                                                            } errorBlock:^(NSError *error) {
                                                                // ...
                                                            }];

    } errorBlock:^(NSError *error) {
        // ...
    }];

} errorBlock:^(NSError *error) {
    // ...
}];

You can't do it using reverse authentication. Reverse authentication basically gives you access to the OAuth tokens at the same access level as the root iOS app, so you can do twitter processing on a remote server. It doesn't use the expanded permissions from your twitter app from the dev portal. As documented on twitter's website, you have to use the full OAuth authentication flow, including the web popup, to gain the user's explicit permission to access direct messages.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!