2017-05-06 105 views
1

我在這裏有一些代碼,我越來越和錯誤401未經授權。我已使用微博框查看了登錄信息。twitter未授權401 c#oauth/request_token

public async Task get_Oauth_token_TaskAsync() 
    { 


     //variables for action 
     string oauth_nonce; 
     string oauth_callback; 
     string oauth_signature_method; 
     string oauth_timestamp; 
     string oauth_consumer_key; 
     string oauth_signature; 
     string oauth_version; 
     string req_url; 
     //set values to variables 
     var dt = DateTime.Now; 
     var ticks = dt.Ticks; 
     var seconds = ticks/TimeSpan.TicksPerSecond; 
     oauth_timestamp = seconds.ToString(); 
     oauth_callback = "http://www.localhost:55593/TwitterSync.aspx"; 
     oauth_nonce = Convert.ToBase64String(Guid.NewGuid().ToByteArray()).Substring(0, 16).ToString(); //random string 
     oauth_signature_method = "HMAC-SHA1"; 
     oauth_consumer_key = "xxxx"; 
     oauth_version = "1.0"; 
     req_url = "https://api.twitter.com/oauth/request_token"; 
     oauth_signature = create_auth_Signature_Task(oauth_nonce,oauth_callback,oauth_signature_method,oauth_timestamp,oauth_consumer_key,oauth_version,req_url); 

     ///action 
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(req_url); 
     request.Method = "POST"; 
     request.UserAgent = "themattharris' HTTP Client"; 
     request.Host = "api.twitter.com"; 

     request.Headers.Add(HttpRequestHeader.Authorization, "OAuth oauth_callback = \"" + Uri.EscapeDataString(oauth_callback) + "\"" + ",oauth_consumer_key = \"" + oauth_consumer_key + "\",oauth_nonce = " + "\"" + oauth_nonce + "\", oauth_signature = \"" + oauth_signature + "\", oauth_signature_method = \"HMAC-SHA1\", oauth_timestamp = \"" + oauth_timestamp + "\", oauth_version = \"1.0\""); 


     WebResponse response = await request.GetResponseAsync(); 
     Stream str = response.GetResponseStream(); 
     StreamReader reader = new StreamReader(str); 
     dynamic resp = reader.ReadToEndAsync(); 

    } 

    private string create_auth_Signature_Task(string oauth_nonce, string oauth_callback, 
     string oauth_signature_method, string oauth_timestamp, string oauth_consumer_key, string oauth_version, string req_url) 
    { 
     string oauth_nonce_enc = Uri.EscapeDataString(oauth_nonce); 
     string oauth_callback_enc = Uri.EscapeDataString(oauth_callback); 
     string oauth_signature_method_enc = Uri.EscapeDataString(oauth_signature_method); 
     string oauth_timestamp_enc = Uri.EscapeDataString(oauth_timestamp); 
     string oauth_consumer_key_enc = Uri.EscapeDataString(oauth_consumer_key); 
     string oauth_version_enc = Uri.EscapeDataString(oauth_version); 
     string req_url_enc = Uri.EscapeDataString(req_url); 
     string secret = "xxxx"; 
     string signature_base = "POST&" + req_url_enc + "&" + Uri.EscapeDataString("oauth_consumer_key=" + oauth_consumer_key_enc + "&oauth_nonce=" + oauth_nonce + "&oauth_signature_method=" + oauth_signature_method_enc + "&oauth_timestamp=" + oauth_timestamp_enc + "&oauth_version=" + oauth_version_enc); 


     return ShaHash(signature_base, secret); 
    } 

    private string ShaHash(string value, string key) 
    { 
     using (var hmac = new HMACSHA1(Encoding.UTF32.GetBytes(key))) 
     { 
      return ByteToString(hmac.ComputeHash(Encoding.UTF32.GetBytes(value))); 
     } 
    } 

    static string ByteToString(IEnumerable<byte> data) 
    { 
     return string.Concat(data.Select(b => b.ToString("x2"))); 
    } 

我根據這page做了一切。現在我迷失了,任何想法爲什麼它不起作用?

回答

2

您沒有正確計算oauth_timestamp。這應該被計算爲Unix時間是的秒數因爲00:00:00 UTC上1月1日1970年

int unixTimestamp = (int)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds; 

,或者使用.NET 4.6

long unixTimestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); 

也用於簽名密鑰創建簽名應該是百分號編碼的消費者密碼和由&字符連接的編碼密碼百分比(&)的串聯。在獲取請求令牌的情況下,令牌密鑰還不知道,因此您應該只使用消費者密碼,然後使用&符號(&)。

var signingKey = oauth_consumer_key_enc + "&"; 

最後,你應該返回Base64編碼字符串,你計算你的散列後讓你的函數變爲:

private string ShaHash(string value, string signingKey) 
{ 
    using (var hmac = new HMACSHA1(Encoding.ASCII.GetBytes(signingKey))) 
    { 
    return Convert.ToBase64String(hmac.ComputeHash(Encoding.ASCII.GetBytes(value))); 
    } 
}