我有这个工作代码:
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"));
}
}
}
如果未存储授权,则会重定向到Twitter身份验证请求。否则,它会继续在页面上。
我面临的问题是当用户通过他们的Twitter帐户撤销对该应用的访问权限时,如何在此页面上检测到该应用不再有权为用户发布推文并需要重新授权?
答案 0 :(得分:2)
您可以查询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);
}
如果失败,您可以通过删除用户的凭据密钥并仅使用ConsumerKey / ConsumerSecret授权来强制授权。这将使用户进入Twitter页面再次授权您的应用程序。然后,您可以重新保存这些用户凭据,以防它们发生更改。