我想通过用户在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("-------------------------------------------");
但用户的电子邮件为空。
答案 0 :(得分:0)
根据Twitter VerifyCredentials Documentation,您需要列入白名单才能接收电子邮件地址。否则,它们为空。
那就是说,你的查询不起作用,因为LINQ to Twitter是异步的,你应该写下这样的具体化声明:
var user = await objAccount.SingleOrDefaultAsync();
注意await
上的Async
修饰符和SingleOrDefault
后缀。因为它是异步的,所以整个调用链需要是异步的。
在您通过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上找到它们并在Twitter上编写代码,就好像它们就是您一样。