57

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?

bkaid
  • 51,465
  • 22
  • 112
  • 128
Thomas
  • 4,641
  • 13
  • 44
  • 67

4 Answers4

67

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

dv3
  • 4,369
  • 5
  • 28
  • 50
Swift
  • 13,118
  • 5
  • 56
  • 80
  • 3
    Link is to old v1 docs. New (v1.1) docs: https://dev.twitter.com/docs/api/1.1/get/account/verify_credentials – joost Apr 30 '14 at 09:42
  • Updated. Thanks for the heads up. – Swift May 01 '14 at 16:37
  • 1
    If your app is not approved, set optional parameter `include_email` to false https://dev.twitter.com/rest/reference/get/account/verify_credentials otherwise you will get `Your credentials do not allow access to this resource` error. – shukshin.ivan May 07 '16 at 23:05
  • I think the link is outdated again. It seems that the new URL is https://developer.twitter.com/en/docs/accounts-and-users/manage-account-settings/api-reference/get-account-verify_credentials.html – kunambi Dec 20 '17 at 07:07
  • How to get user info in an android app from this link? I integrated twitter login but didn't find how to get user info afterwards. Should I parse the JSON data in my app? – ninbit Aug 01 '18 at 09:50
  • The name is not very descriptive. That's why it's harder than necessary to find this :) – Ardee Aram Jan 29 '21 at 06:43
4

Link to the documentation for API v. 1.1: https://dev.twitter.com/docs/api/1.1/get/account/verify_credentials

Pablo Torrecilla
  • 2,098
  • 20
  • 15
2

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";
FatalError
  • 560
  • 2
  • 18
matrim_c
  • 81
  • 1
  • 4
0

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

David
  • 1,061
  • 11
  • 18
  • Can you please share code for fetch twitter followers list? Currently its always return false when I I try to requestAccessToAccountsWithType – Anjali jariwala May 07 '18 at 05:22
  • @Anjalijariwala Do you have any example for getting profile details of twitter account including emailID . I have used the package "react-native-login-twitter" for my react-native application. Authentication is successfull and I got authToken, authTokenSecret of the profile but email is empty. Is there any second call required to get more profile infos using authToken & authTokenSecret . If so please help .. Thank you :) – Ajith Dec 11 '19 at 05:56