mvc.net从Twitter获取个人资料信息

时间:2019-04-25 03:26:09

标签: c# .net asp.net-mvc api twitter

使用标准的mvc.net外部身份验证进行身份验证后,我正在尝试从Twitter检索个人资料信息。

我可以使用ExternalLoginCallback函数中的以下代码(需要安装Facebook SDK)为facebook做到这一点。

 if (string.Equals(loginInfo.Login.LoginProvider, "facebook", StringComparison.CurrentCultureIgnoreCase))
        {
            var identity = AuthenticationManager.GetExternalIdentity(DefaultAuthenticationTypes.ExternalCookie);
            var access_token = identity.FindFirstValue("FacebookAccessToken");
            var fb = new FacebookClient(access_token);

            // you need to specify all the fields that you want to get back
            dynamic myInfo = fb.Get("/me?fields=email,first_name,last_name");
            email = myInfo.email;
            firstName = myInfo.first_name;
            lastName = myInfo.last_name;
        }

我正在寻找类似的Twitter。

TIA

1 个答案:

答案 0 :(得分:0)

所以这是我主要基于此所做的

Obtain twitter access token asp.net identity

首先,我按照上面的@Mate建议安装了TwitterinviAPI nuget软件包。

第二,我在StartUp.Auth.cs中更新了ConfigureAuth的twitter部分,以包括“提供程序”

app.UseTwitterAuthentication(new TwitterAuthenticationOptions()
        {
            ConsumerKey = ConfigurationManager.AppSettings["TwitterConsumerKey"],
            ConsumerSecret = ConfigurationManager.AppSettings["TwitterConsumerSecret"],
            Provider = new LinqToTwitterAuthenticationProvider(),
            BackchannelCertificateValidator = new Microsoft.Owin.Security.CertificateSubjectKeyIdentifierValidator(new[]
                {
                   "A5EF0B11CEC04103A34A659048B21CE0572D7D47", // VeriSign Class 3 Secure Server CA - G2
                   "0D445C165344C1827E1D20AB25F40163D8BE79A5", // VeriSign Class 3 Secure Server CA - G3
                   "7FD365A7C2DDECBBF03009F34339FA02AF333133", // VeriSign Class 3 Public Primary Certification Authority - G5
                   "39A55D933676616E73A761DFA16A7E59CDE66FAD", // Symantec Class 3 Secure Server CA - G4
                   "‎add53f6680fe66e383cbac3e60922e3b4c412bed", // Symantec Class 3 EV SSL CA - G3
                   "4eb6d578499b1ccf5f581ead56be3d9b6744a5e5", // VeriSign Class 3 Primary CA - G5
                   "5168FF90AF0207753CCCD9656462A212B859723B", // DigiCert SHA2 High Assurance Server C‎A 
                   "B13EC36903F8BF4701D498261A0802EF63642BC3" // DigiCert High Assurance EV Root CA
                 }),
        });

第三,我将LinqToTwitterAuthenticationProvider类添加到Model / IdentityModels中(尽管您可以将这段代码放在任何喜欢的地方)

public class LinqToTwitterAuthenticationProvider : TwitterAuthenticationProvider
{
    public const string AccessToken = "TwitterAccessToken";
    public const string AccessTokenSecret = "TwitterAccessTokenSecret";

    public override Task Authenticated(TwitterAuthenticatedContext context)
    {
        context.Identity.AddClaims(
            new List<Claim>
            {
            new Claim(AccessToken, context.AccessToken),
            new Claim(AccessTokenSecret, context.AccessTokenSecret)
            });

        return base.Authenticated(context);
    }

最后,我使用以下代码更新了Accounts控制器中的ExternalLoginCallback以从twitter获取用户详细信息

  else if (string.Equals(loginInfo.Login.LoginProvider, "twitter", StringComparison.CurrentCultureIgnoreCase))
        {
            // Generate credentials that we want to use
            var identity = AuthenticationManager.GetExternalIdentity(DefaultAuthenticationTypes.ExternalCookie);
            var access_token = identity.FindFirstValue(LinqToTwitterAuthenticationProvider.AccessToken);
            var access_token_secret = identity.FindFirstValue(LinqToTwitterAuthenticationProvider.AccessTokenSecret);
            var creds = new TwitterCredentials(ConfigurationManager.AppSettings["TwitterConsumerKey"], ConfigurationManager.AppSettings["TwitterConsumerSecret"],access_token, access_token_secret);
            var authenticatedUser = Tweetinvi.User.GetAuthenticatedUser(creds);
            email = authenticatedUser.Email;
            firstName = authenticatedUser.Name.Substring(0, authenticatedUser.Name.IndexOf(' '));
            lastName = authenticatedUser.Name.Substring(authenticatedUser.Name.IndexOf(' ') + 1);

        }

有一个小小的GOTCHYA。即便如此,电子邮件仍然是空的。问题出在我的Twitter应用程序设置中,我没有提供隐私或ToS URL,因此无法检查权限选项卡下的选项来请求用户电子邮件。仅供参考。

希望这会有所帮助!