Get current user's info from Twitter API
TwitterTwitter 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
It was actually "account/verify_credentials" as seen here:
Solution 2 - Twitter
Link to the documentation for API v. 1.1: https://dev.twitter.com/docs/api/1.1/get/account/verify_credentials
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:
-
If you have a Twitter account (i.e.
ACAccount
fromACAccountStore
) 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); }
}];
-
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