简体   繁体   English

如何使用ASP.NET Core中的访问令牌向Twitter api发送请求?

[英]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. 我正在一个项目中,该项目正在接收前端客户端的访问令牌,并使用该访问令牌,因此我必须向Twitter API发出请求才能获取用户详细信息,包括电子邮件地址和个人资料图片网址。

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. 在Facebook的情况下,这只是正常的获取请求,在Google和Microsoft的情况下,我只需要在Header中添加访问令牌作为Bearer令牌,但是,我找不到用于Twitter的方法。

This is the url where I have to make request. 这是我必须提出要求的网址。

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

Here is the code for Facebook , Google and Microsoft. 这是Facebook,Google和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;
        }
    }

In Twitter you should have the access token & access token secret. 在Twitter中,您应该具有访问令牌和访问令牌密钥。 then you could call the verification API: https://api.twitter.com/1.1/account/verify_credentials.json?include_email=true 那么您可以调用验证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 使用任何Twitter库进行此验证都更加容易,例如: https : //github.com/CoreTweet/CoreTweet

or any .NET library for Twitter. 或任何适用于Twitter的.NET库。

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: 您也可以从邮递员那里尝试: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在asp.net核心中发送带有防伪令牌的post ajax请求不访问控制器方法 - Send post ajax request with antiforgery token in asp.net core do not access controller method 如何在ASP.NET Core中为Azure AD请求正确的访问令牌以访问Microsoft Graph - How do request a correct access token in ASP.NET Core for Azure AD to access Microsoft Graph ASP.net核心web api:使用Facebook / Google OAuth访问令牌进行身份验证 - ASP.net core web api: Using Facebook/Google OAuth access token for authentication 如何在 ASP.NET Core 服务器中存储外部 API 的访问令牌? - How do I store an access token for an external API in an ASP.NET Core server? 如何在 ASP.NET Core API 中查看 Azure AD 生成的访问令牌? - How to view access token generated by Azure AD in ASP.NET Core API? 如何在 ASP.NET CORE 中将 Bearer token 添加到请求中 - How to add Bearer token to a request in ASP.NET CORE ASP.NET Core中如何添加token请求header - How to add token to request header in ASP.NET Core 如何从 Internet Explorer 向 Asp.Net Core API 发送 GET 请求? - How to send GET request from internet explorer to Asp.Net Core API? 使用 ASP.net 内核 web api 发送多个文件 - Send multiple files using ASP.net core web api ASP NET Core Twitter OAuth 请求令牌问题 - ASP NET Core Twitter OAuth Request Token Issues
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM