I need to access the Direct Messages using SLrequest. I got the oAuthToken and oAuthToken Secret using the Reverse oAuth of twitter. Now I need to know how to fetch the direct messages from the https://api.twitter.com/1.1/direct_messages.json url. I have tried adding oAuthToken and oAuthTokenSecret part of SLRequest but I get the same error, "This application is not allowed to access or delete your direct messages". What is the use of oAuthToken and oAuthTokenSecret? How to make direct messages work for the app? I have changed the access level of application to "Read, write, and direct messages". Please help me in solving the problem.
Asked
Active
Viewed 2,335 times
2 Answers
6
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) {
// ...
}];

nst
- 3,862
- 1
- 31
- 40
-
It doesn't use SLRequest. I was actually trying it with SLRequest. – Raj iOS Sep 13 '13 at 11:09
-
SLRequest uses iOS consumer tokens, which cannot access direct messages. Using reverse auth, iOS grants another application access to the some user account with the permissions of this second application. For a clearer explanation of reverse auth. see http://seriot.ch/abusing_twitter_api.php#24 Also, the example I posted uses SLRequest for phase 2. – nst Sep 13 '13 at 12:21
-
So basically you cannot use SLRequest for direct messages. That I too just got to know. Thanks for your answer. Please update in your answer that SLRequest cannot be used directly so that others will find it easily. – Raj iOS Sep 13 '13 at 12:53
-2
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.