2012-09-28 5 views
0

트위터에서 비동기 적으로 요청 토큰을 가져 오는 데 문제가 있습니다 - 서버가 "원격 서버에서 오류를 반환했습니다 : (401) Unauthorized."Twitter에서 async - 401 오류가 발생했습니다.

public void AcquireRequestToken(Action<bool> response) 
{ 

    string oauth_nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString())); 

    // build the signature 
    var headers = new Dictionary<string,string>() 
    { 
     { "oauth_consumer_key", _oAuthConfig.ConsumerKey }, 
     { "oauth_nonce", oauth_nonce }, 
     { "oauth_signature_method", "HMAC-SHA1" }, 
     { "oauth_timestamp", MakeTimestamp() }, 
     { "oauth_version", "1.0" }, 
     { "oauth_callback", PercentEncode(_oAuthConfig.Callback) }, 
    }; 

    string signature = MakeSignature ("POST", _oAuthConfig.RequestTokenUrl, headers); 
    string compositeSigningKey = MakeSigningKey(_oAuthConfig.ConsumerSecret, null); 
    string oauth_signature = MakeOAuthSignature(compositeSigningKey, signature); 

    Uri fullUri = new Uri(_oAuthConfig.RequestTokenUrl); 

    var request = (HttpWebRequest)WebRequest.Create(fullUri); 
    request.Method = "POST"; 

    request.Headers.Add("oauth_consumer_key", PercentEncode(_oAuthConfig.ConsumerKey)); 
    request.Headers.Add("oauth_nonce", PercentEncode(oauth_nonce)); 
    request.Headers.Add("oauth_signature_method", PercentEncode("HMAC-SHA1")); 
    request.Headers.Add("oauth_timestamp", PercentEncode(MakeTimestamp())); 
    request.Headers.Add("oauth_version", "1.0"); 
    request.Headers.Add("oauth_callback", PercentEncode(_oAuthConfig.Callback)); 
    request.Headers.Add("oauth_signature", PercentEncode(oauth_signature)); 

    try 
    { 

     request.BeginGetResponse(new AsyncCallback(result => 
     { 
      string contents = String.Empty; 
      HttpWebRequest theRequest = (HttpWebRequest)result.AsyncState; 

      if (theRequest != null) 
      { 
       try 
       { 
        HttpWebResponse theResponse = (HttpWebResponse)theRequest.EndGetResponse(result); 

        using (Stream stream = theResponse.GetResponseStream()) 
        using (StreamReader reader = new StreamReader(stream)) 
        { 
         contents = reader.ReadToEnd(); 
        } 

        Dictionary<string, object> results = JsonConvert.DeserializeObject<Dictionary<string, object>>(contents); 

        _requestToken = (string)results ["oauth_token"]; 
        _requestTokenSecret = (string)results ["oauth_token_secret"]; 

        response(true); 
       } 
       catch (WebException e) 
       { 
        response(false); 
       } 
      } 
      else 
      { 
       response(false); 
      } 
     }), request); 

    } 
    catch (WebException e) 
    { 
     response(false); 
    } 

} 

내 오랜 (비 비동기) 코드 잘 작동 - -

여기에 내가 사용하고 코드입니다

public bool AcquireRequestToken() 
{ 
    var headers = new Dictionary<string,string>() 
    { 
     { "oauth_callback", PercentEncode(_oAuthConfig.Callback) }, 
     { "oauth_consumer_key", _oAuthConfig.ConsumerKey }, 
     { "oauth_signature_method", "HMAC-SHA1" }, 
     { "oauth_timestamp", MakeTimestamp() }, 
     { "oauth_version", "1.0" } 
    }; 

    string signature = MakeSignature ("POST", _oAuthConfig.RequestTokenUrl, headers); 
    string compositeSigningKey = MakeSigningKey(_oAuthConfig.ConsumerSecret, null); 
    string oauth_signature = MakeOAuthSignature(compositeSigningKey, signature); 

    var wc = new WebClient(); 
    headers.Add ("oauth_signature", PercentEncode(oauth_signature)); 
    wc.Headers [HttpRequestHeader.Authorization] = HeadersToOAuth(headers); 

    try 
    { 
     var result = HttpUtility.ParseQueryString(wc.UploadString (new Uri(_oAuthConfig.RequestTokenUrl), "")); 

     if (result ["oauth_callback_confirmed"] != null) 
     { 
      _requestToken = result ["oauth_token"]; 
      _requestTokenSecret = result ["oauth_token_secret"]; 

      return true; 
     } 
    } 
    catch (Exception e) 
    { 
     return false; 
    } 

} 

헤더는 새 버전에서 약간 다릅니다 만, 그들을 유지 같은 것은 도움이되지 않습니다.

의견을 보내 주셔서 감사합니다.

답변

0

머리글이 잘못되었습니다. 이 페이지는 어떻게 만드는 당신을 말할 것이다

Authorization: OAuth oauth_consumer_key="<the consumer key of your app>", oauth_nonce="<the nonce>", oauth_signature="<the signature>", oauth_signature_method="HMAC-SHA1", oauth_timestamp="<the timestamp>", oauth_token="<your token>", oauth_version="1.0" 

: https://dev.twitter.com/docs/auth/authorizing-requestAuthorization라는 하나의 HTTP 헤더가있다. 이 두 가지 방법을 제외한 모든 방법에 대해 동일합니다 : (당신이 그것을 가지고 있지 않기 때문에 일반)

  • request_tokenoauth_callback하지만 oauth_token이 필요합니다.
  • access_tokenoauth_verifier이 필요합니다 (두 번째 단계 이후에 표시됨).
관련 문제