2014-12-01 120 views
1

我有這樣的工作代碼:LinqToTwitter檢查授權

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帳戶撤銷對應用程序的訪問時,如何在此頁面上檢測到應用程序不再有權向用戶發佈Tweets並需要重新授權?

回答

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頁面再次授權您的應用。然後,您可以重新保存這些用戶憑證,以防他們更改。