2013-04-13 124 views
5

我開始用Twitter推我的頭髮,並試圖登錄用戶!我有Facebook,Google,OpenId都能正常工作,只是Twitter正在痛苦中。DotNetOpenAuth Twitter Authenticaion退回401未授權

當我嘗試運行我的代碼時,我一直在獲取401未授權,而我的生活中找不到原因。

我創建了一個twitter客戶端,並且我將它與DotNetOpenAuth示例解決方案中的InMemoryTokenManager結合使用。我的Twitter客戶端是在這裏

public class TwitterClient 
{ 
    private string UserName { get; set; } 

    private static readonly ServiceProviderDescription ServiceDescription = 
     new ServiceProviderDescription 
     { 
      RequestTokenEndpoint = new MessageReceivingEndpoint(
             "https://api.twitter.com/oauth/request_token", 
             HttpDeliveryMethods.GetRequest | 
             HttpDeliveryMethods.AuthorizationHeaderRequest), 

      UserAuthorizationEndpoint = new MessageReceivingEndpoint(
             "https://api.twitter.com/oauth/authorize", 
             HttpDeliveryMethods.GetRequest | 
             HttpDeliveryMethods.AuthorizationHeaderRequest), 

      AccessTokenEndpoint = new MessageReceivingEndpoint(
             "https://api.twitter.com/oauth/access_token", 
             HttpDeliveryMethods.GetRequest | 
             HttpDeliveryMethods.AuthorizationHeaderRequest), 

      TamperProtectionElements = new ITamperProtectionChannelBindingElement[] { new HmacSha1SigningBindingElement() }, 
     }; 

    IConsumerTokenManager _tokenManager; 

    public TwitterClient(IConsumerTokenManager tokenManager) 
    { 
     _tokenManager = tokenManager; 
    } 

    public void StartAuthentication() 
    { 
     var request = HttpContext.Current.Request; 
     using (var twitter = new WebConsumer(ServiceDescription, _tokenManager)) 
     { 
      var callBackUrl = new Uri(request.Url.Scheme + "://" + request.Url.Authority + "/Members/TwitterCallback"); 
      twitter.Channel.Send(
       twitter.PrepareRequestUserAuthorization(callBackUrl, null, null) 
      ); 
     } 
    } 

    public bool FinishAuthentication() 
    { 
     using (var twitter = new WebConsumer(ServiceDescription, _tokenManager)) 
     { 
      var accessTokenResponse = twitter.ProcessUserAuthorization(); 
      if (accessTokenResponse != null) 
      { 
       UserName = accessTokenResponse.ExtraData["screen_name"]; 
       return true; 
      } 
     } 

     return false; 
    } 
} 

在我MembersController的它與正確的憑據實例化InMemoryTokenManager的構造,我有以下

_tokenManager = new InMemoryTokenManager(ConfigUtils.GetAppSetting("TwitterAppId"), ConfigUtils.GetAppSetting("TwitterAppSecret")); 

和我的兩個動作是

public ActionResult LogonTwitter() 
    { 
     var client = new TwitterClient(_tokenManager); 
     client.StartAuthentication(); 
     return null; 
    } 

    public ActionResult TwitterCallback() 
    { 
     var client = new TwitterClient(_tokenManager); 

     if (client.FinishAuthentication()) 
     { 
      return new RedirectResult("/"); 
     } 

     // show error 
     return View("LogOn"); 
    } 

該錯誤出現在我的TwitterClient中的StartAuthentication()中。只要它調用此行

twitter.Channel.Send(
       twitter.PrepareRequestUserAuthorization(callBackUrl, null, null) 
      ); 

我收到以下錯誤

Error occurred while sending a direct message or getting the response. 
Inner Exception: The remote server returned an error: (401) Unauthorized. 

任何人有什麼建議嗎?我已經花了大部分時間在昨天,今天早上試圖對此進行分類。我所嘗試的所有在線示例似乎也獲得了401未授權回來? DotNetOpenAuth和Twitter有沒有一個已知的問題?

非常感謝任何幫助。

回答

5

我不記得確切的術語,但您是否在twitter應用程序(以及代碼中)中設置了回調URL?我最近有類似的問題,即使在本地開發,我相信你需要設置該值,即使它只是一個佔位符

+0

我正在尋找一個facebook代碼相同的,任何機會,你會分享你有什麼?我不斷收到錯誤400錯誤的請求 – user1186651

+0

Thankkkkkkkkksssssssssssss !!!!! – benjguin

+0

非常感謝! :) –