c# - mvc.net 从 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;
}
我正在寻找一个类似的推特。
TIA
解决方案
所以这就是我所做的主要基于这个
首先,我 按照上面的@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 CA
"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,因此无法检查权限选项卡下的选项以请求用户电子邮件。仅供参考。
希望这可以帮助!!
推荐阅读
- javascript - Shift选项卡不保留元素焦点
- python - 如何将 3 个列表值与 If 条件进行比较
- c# - 为什么 xUnit 测试没有变成绿色?
- python - 使用 unittest 测试烧瓶应用程序时出错
- ruby - 使用 OptParser 在 Ruby 脚本中访问命令行选项文字
- r - 如果存在多行,则 R_exclude 具有包含值的列的行
- android - 运行从 android APK 编译的 ELF 二进制文件需要什么?(了解 android 内部结构)
- sql - 如何将 YYYYMM 转换/提取为年份和月份名称?Teradata SQL
- ibm-doors - 如何创建一个视图来显示单个项目下所有正式模块中的所有现有属性 DOORS?
- r - rtweet“search_tweets”函数不返回坐标