Get current user's info from Twitter API
I am b开发者_C百科uilding 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?
It was actually "account/verify_credentials" as seen here:
https://developer.twitter.com/en/docs/accounts-and-users/manage-account-settings/api-reference/get-account-verify_credentials.html
Link to the documentation for API v. 1.1: https://dev.twitter.com/docs/api/1.1/get/account/verify_credentials
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 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
精彩评论