Bipn Paul
Bipn Paul

Reputation: 1475

How to send request to Twitter api using access token in ASP.NET core?

I'm working on a project which is receiving the access-token from the Front-end client and using that access token I have to make request to Twitter API in order to get the user details including email address and profile picture url.

in the case of Facebook it is just a normal get request , in the case of Google and Microsoft I just have to add access token as Bearer token in Header but, I'm not able to find a way for Twitter.

This is the url where I have to make request.

https://api.twitter.com/1.1/account/verify_credentials.json

Here is the code for Facebook , Google and Microsoft.

private async Task<Profile> ProfileAsync(string token,string providerName)
    {
        using (HttpClient client = new HttpClient())
        {
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            if((providerName=="Google") || (providerName=="Microsoft"))
            {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
            }
            var formatters = new List<MediaTypeFormatter>()
            {
                new JsonMediaTypeFormatter()
            };
            string url;
            Profile profile = null;
            if (providerName=="Facebook")
            {
                 url = $"https://graph.facebook.com/me?fields=id,name,email&access_token={token}";
            }
            else if(providerName=="Google")
            {
                url = $"https://www.googleapis.com/userinfo/v2/me";
            }
            else if(providerName=="Microsoft")
            {
                url = $"https://graph.microsoft.com/v1.0/me/";
            }
            else
            {
                throw new Exception("Unsupported grant type.");
            }
            HttpResponseMessage response = await client.GetAsync(url);

            if (response.IsSuccessStatusCode)
            {
                profile = await response.Content.ReadAsAsync<Profile>(formatters);
            }
            if(providerName=="Microsoft")
            {
                profile.email = profile.userPrincipalName;
                profile.name = profile.displayName;
            }
            return profile;
        }
    }

Upvotes: 2

Views: 1501

Answers (1)

Wajdy Essam
Wajdy Essam

Reputation: 4340

In Twitter you should have the access token & access token secret. then you could call the verification API: https://api.twitter.com/1.1/account/verify_credentials.json?include_email=true

Its easier to use any Twitter library for this verification, for example: https://github.com/CoreTweet/CoreTweet

or any .NET library for Twitter.

For example:

Tokens tokens = new Tokens()
{
    AccessToken = "xxx",
    AccessTokenSecret = "xxx",
    ConsumerKey = "xxx",
    ConsumerSecret = "xxx",
};

IDictionary<string, object> dict = new Dictionary<string, object>();
dict.Add("include_email", "true");
var response = tokens.Account.VerifyCredentials(dict); // will throw exception if not authorized

Console.WriteLine(response.ScreenName + " " + response.Email + " " + response.Id);

Also you could try it from postman: enter image description here

Upvotes: 1

Related Questions