Reputation: 61725
I have this working code:
public AspNetAuthorizer Authoriser { get; set; }
protected async void Page_Load(object sender, EventArgs e)
{
var currentUser = v2.Membership.Functions.GetLoggedInAsUser();
Authoriser = v3.Twitter.Auth.GetUserAuthorizer(currentUser.UserID,
v3.Arcade.Settings.TwitterScirraArcadeAppAppName, v3.Arcade.Settings.TwitterScirraArcadeAppConsumerKey,
v3.Arcade.Settings.TwitterScirraArcadeAppConsumerSecret);
if (!Page.IsPostBack && Request.QueryString["oauth_token"] != null)
{
await Authoriser.CompleteAuthorizeAsync(Request.Url);
var credentials = Authoriser.CredentialStore;
v3.Twitter.Auth.SaveAuthorisationDetails(currentUser.UserID,
v3.Arcade.Settings.TwitterScirraArcadeAppAppName, credentials.OAuthToken, credentials.OAuthTokenSecret,
credentials.ScreenName);
}
else
{
if (!Authoriser.CredentialStore.HasAllCredentials())
{
await Authoriser.BeginAuthorizeAsync(new Uri("https://127.0.0.1/newarcade/twitterpopup.aspx"));
}
}
}
If no authorisation is stored, it redirects to Twitter authentication request. Otherwise, it continues on the page.
The problem I am facing is when a user revokes access to the app via their Twitter account, how can I detect on this page that the app no longer has permission to post Tweets for the user and needs reauthorising?
Upvotes: 2
Views: 275
Reputation: 7513
You can query VerifyCredentials:
try
{
var verifyResponse =
await
(from acct in twitterCtx.Account
where acct.Type == AccountType.VerifyCredentials
select acct)
.SingleOrDefaultAsync();
if (verifyResponse != null && verifyResponse.User != null)
{
User user = verifyResponse.User;
Console.WriteLine(
"Credentials are good for {0}.",
user.ScreenNameResponse);
}
}
catch (TwitterQueryException tqe)
{
Console.WriteLine(tqe.Message);
}
If that fails, you can force authorization by dropping the user's credential keys and authorizing with only ConsumerKey/ConsumerSecret. That will bring the user to the Twitter page to authorize your app again. Then you can re-save those user credentials, in case they've changed.
Upvotes: 2