Get current user's info from Twitter API

Twitter

Twitter Problem Overview


I am building a Twitter app (with oAuth) and I cannot find any info on how I can get the current user's info.

By current user, I mean the user which granted my app permissions and for whom the app is making the requests to the API.

Any suggestions?

Twitter Solutions


Solution 1 - Twitter

Solution 2 - Twitter

Solution 3 - Twitter

Using oAuth you are able to make calls to get user info from your TwitterOAuth object.

e.g if

$oauth = new TwitterOAuth([keys here]);

$credentials = $oauth->get('account/verify_credentials');

echo "Connected as @" . $credentials->screen_name ."\n";

Solution 4 - Twitter

Solution respecting iOS:

  1. If you have a Twitter account (i.e. ACAccount from ACAccountStore) on your device, the only thing you need is to attach it to SLRequest and get all the user info from the returned dictionary:

    NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/account/verify_credentials.json"]; NSMutableDictionary *params = [NSMutableDictionary new]; [params setObject:[Twitter sharedInstance].session.userID forKey:@"user_id"]; [params setObject:@"0" forKey:@"include_entities"]; [params setObject:@"1" forKey:@"skip_status"]; [params setObject:@"1" forKey:@"include_email"];

    SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:url parameters:params]; [request setAccount:twitterAccount]; //one of the Twitter Acc [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { if (responseData) { NSDictionary *twitterData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:NULL]; });

     }else {
         NSLog(@"Error while downloading Twitter user data: %@", error);
     }
    

    }];

  2. Otherwise - you need to send OAuth (X-Auth) user credentials. The easiest way is to use TWTROAuthSigning class to retrieve OAuth-params:

    TWTROAuthSigning *oauthSigning = [[TWTROAuthSigning alloc] initWithAuthConfig: [Twitter sharedInstance].authConfig authSession:session]; NSDictionary *authHeaders = [oauthSigning OAuthEchoHeadersToVerifyCredentials];

and than send the ordinary request attaching credentials as header fields:

  NSString *oauthParameters = authHeaders[@"X-Verify-Credentials-Authorization"];
                         NSString *urlString = authHeaders[@"X-Auth-Service-Provider"];
                         NSMutableURLRequest *request =
                         [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
                         request.allHTTPHeaderFields = @{@"Authorization":oauthParameters};
                         [request setHTTPMethod:@"GET"];
                         NSOperationQueue *queue = [[NSOperationQueue alloc]init];
                         [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response,NSData *data,NSError *error){
                              NSDictionary *twitterData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:NULL];
                         }];

If you support an old version of Twitter SDK or searching for more options I'd recommend to look at STTwitter lib

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionThomasView Question on Stackoverflow
Solution 1 - TwitterSwiftView Answer on Stackoverflow
Solution 2 - TwitterPablo TorrecillaView Answer on Stackoverflow
Solution 3 - Twittermatrim_cView Answer on Stackoverflow
Solution 4 - TwitterDavidView Answer on Stackoverflow