如何在linqToTwitter中通过用户的屏幕名获取用户的电子邮件
本文关键字:用户 屏幕 获取 电子邮件 linqToTwitter | 更新日期: 2023-09-27 18:14:45
我想在linqToTwitter中通过用户的屏幕名获取用户的电子邮件,我使用这个代码:
var auth = new SingleUserAuthorizer
{
CredentialStore = new SingleUserInMemoryCredentialStore
{
ConsumerKey = "<Twitter consumer key>",
ConsumerSecret = "<Twitter consumer secret>",
AccessToken = "<Your access token>",
AccessTokenSecret = "<Your access token secret>"
}
};
var objTwitter = new TwitterContext(auth);
var objAccount = (from account in objTwitter.User
where account.Type == UserType.Show &&
account.ScreenName == sUser
select account);
var user = objAccount.SingleOrDefault();
Console.WriteLine("UserName : " + user.ScreenName);
Console.WriteLine("Name : " + user.Name);
Console.WriteLine("UserID : " + user.UserIDResponse);
Console.WriteLine("CreatedAt : " + user.CreatedAt);
Console.WriteLine("# of Friends : " + user.FriendsCount);
Console.WriteLine("# of Followers : " + user.FollowersCount);
Console.WriteLine("Email : " + user.Email);
Console.WriteLine("# of Tweets : " + user.StatusesCount);
Console.WriteLine("Photo URL : " + user.ProfileImageUrl);
Console.WriteLine("-------------------------------------------");
根据Twitter VerifyCredentials文档,您需要被列入白名单才能接收电子邮件地址。否则为空。
也就是说,你的查询不会工作,因为LINQ到Twitter是异步的,你应该像这样写物化语句:
var user = await objAccount.SingleOrDefaultAsync();
注意SingleOrDefault
上的await
修饰符和Async
后缀。因为它是异步的,所以你的整个调用链需要是异步的。
一旦您被Twitter列入白名单,您应该查询AccountType.VerifyCredentials
,如下所示:
try
{
var verifyResponse =
await
(from acct in twitterCtx.Account
where acct.Type == AccountType.VerifyCredentials &&
acct.IncludeEmail == true
select acct)
.SingleOrDefaultAsync();
if (verifyResponse != null && verifyResponse.User != null)
{
User user = verifyResponse.User;
Console.WriteLine(
$"Email for {user.ScreenNameResponse} is {user.Email}.");
}
}
catch (TwitterQueryException tqe)
{
Console.WriteLine(tqe.Message);
}
顺便说一句,你应该尽快重新输入你的应用程序。我对它进行了编辑,以便从代码中删除您的凭据,但它们仍然存在差异,许多人可以找到它们并在Twitter上编写代码,就像他们是您一样。