Reputation:
Both
twitterContext.Authorizer.CredentialStore.UserID
and
twitterContext.Authorizer.CredentialStore.ScreenName
are null after successful authorisation.
Querying the API for the User object requires a UserID or ScreenName.
Upvotes: 1
Views: 131
Reputation: 7513
If they're already logged in (authorized), you can do an Account/VerifyCredentials query, like this:
var verifyResponse =
await
(from acct in twitterCtx.Account
where acct.Type == AccountType.VerifyCredentials
select acct)
.SingleOrDefaultAsync();
This returns an Account entity with a User property for a User entity that gives you the ScreenNameResponse and UserIDResponse.
BTW, When a person first logs in, with only two keys (consumerKey and consumerSecret), the twitterContext.Authorizer.CredentialStore will have their UserID and ScreenName. That's because UserID and ScreenName are provided at the end of the authorization process. However, if you are using SingleUserAuthorizer or setting the CredentialStore with all 4 keys, those values aren't there. LINQ to Twitter will bypass authorization because it already has the info it needs (the 4 keys) to properly sign requests. Bypassing authorization is more efficient, but the result is that you don't populate the CredentialStore with UserID and ScreenName. So, it might be useful to detect whether this is the first time that a particular user logged into your application, and store the UserID and ScreenName at that time, which would help you avoid the additional query for VerifyCredentials.
Upvotes: 1