Get current user information from the Twitter API

I am creating a Twitter application (with oAuth) and I cannot find information on how I can get current user information.

By current user, I mean the user who has granted permissions for my application and for whom the application makes API requests.

Any suggestions?

+47
twitter
Jun 27 '10 at 18:14
source share
4 answers

It was actually "account / verify_credentials", as shown here:

https://dev.twitter.com/docs/api/1.1/get/account/verify_credentials

+59
Jul 14 '10 at 5:54
source share
+3
Apr 03
source share

Using oAuth, you can make calls to get user information from the TwitterOAuth object.

for example if

$oauth = new TwitterOAuth([keys here]); $credentials = $oauth->get('account/verify_credentials'); echo "Connected as @" . $credentials->screen_name ."\n"; 
+2
Mar 23 '12 at 11:35
source share

IOS related solution:

1) If your device has a Twitter account (i.e. ACAccount from ACAccountStore ), the only thing you need is to connect it to SLRequest and get all the user information 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 user credentials (X-Auth). The easiest way is to use the TWTROAuthSigning class to retrieve OAuth parameters:

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

and then send regular requests containing 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 the old version of the Twitter SDK or are looking for more options, I would recommend looking at the STTwitter lib

0
May 11 '15 at 6:55
source share



All Articles