2013-04-04 29 views
2

我想要呼叫OpenPaths.cc rest service API這需要雙腿OAuth。爲此,我使用DevDefined.OAuth library(我嘗試了nuget包和最新的github)。它在我不傳遞查詢字符串中的參數時有效,但在傳遞參數時返回400 NOT AUTHORIZED。在OpenPaths.cc中傳遞查詢字符串參數與OAuth呼叫呼叫不起作用

工作樣品與無參數:

public class OpenPathsRequest 
{ 
    private const string accessKey = "your personal access key"; 
    private const string secretKey = "your personal secret"; 
    private const string url = "https://openpaths.cc/api/1"; 
    private OAuthSession session; 

    public OpenPathsRequest() 
    { 
     var consumerContext = new OAuthConsumerContext 
     { 
      ConsumerKey = accessKey, 
      ConsumerSecret = secretKey, 
      SignatureMethod = SignatureMethod.HmacSha1, 
      UseHeaderForOAuthParameters = true 
     }; 
     session = new OAuthSession(consumerContext, url, url, url); 
    } 

    private string GetWebResponseAsString(HttpWebResponse response) 
    { 
     Encoding enc = System.Text.Encoding.GetEncoding(1252); 
     StreamReader loResponseStream = new StreamReader(response.GetResponseStream(), enc); 
     return loResponseStream.ReadToEnd(); 
    } 

    public string GetResponse() 
    { 
     HttpWebResponse response = session.Request().Get().ForUrl(url).ToWebResponse(); 
     var result = GetWebResponseAsString(response); 
     return result; 
    } 
} 

class Program 
{ 
    static void Main(string[] args) 
    { 
     // create new OpenPathsRequest and get result 
     var request = new OpenPathsRequest(); 
     var response = request.GetResponse(); 
     Console.WriteLine(response); 
     Console.WriteLine("Press any key..."); 
     Console.ReadKey(); 
    } 
} 

但是,當我改變的GetResponse方法和在2個參數傳遞(START_TIME和END_TIME)像這樣:

public string GetResponse() 
{    
    HttpWebResponse response = session.Request().Get().ForUrl(url).WithQueryParameters(
     new { start_time = 1364962612, end_time = 1364991412 }).ToWebResponse(); 
    var result = GetWebResponseAsString(response); 
    return result; 
} 

這導致下面的HTTP請求(消費者鑰匙省略):

GET https://openpaths.cc/api/1?start_time=1364962612&end_time=1364991412 HTTP/1.1 
Authorization: OAuth oauth_nonce="7b5da37a-6227-4ded-ae8b-a695e789ef90",oauth_consumer_key="**********",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1365058952",oauth_version="1.0",oauth_signature="tAk4KMj%2FsiG6BTLSmvDNKXbBpNs%3D" 
Host: openpaths.cc 
Connection: Keep-Alive 

我得到一個錯誤re提供:

HTTP/1.1 400 Bad Request 
Date: Thu, 04 Apr 2013 07:03:26 GMT 
Content-Type: text/html; charset=UTF-8 
Connection: keep-alive 
Content-Length: 19 
Server: TornadoServer/2.0 
Set-Cookie: _xsrf=bf20487382b64eeb8646d31b0770db85; Path=/ 
Set-Cookie: session_id=ZTk2Mjk1MzIzNWNiMmRjMTY1ZmY5Y2ExNWUwMGY5ZTAxZmY1NGIyODljZGJiNzRlMmIyMWI4NTA3YzUwYWJlYg==|1365059006|7459a7ff95039279e9686ceb76b58918fd9f3e48; expires=Thu, 04 Apr 2013 07:18:26 GMT; Path=/ 

400: NOT AUTHORIZED 

非常感謝所有幫助。提前致謝。

Harmen

回答

2

解決!顯然,OpenPaths.cc api要求在簽名基本字符串中不簽署附加查詢參數。我相信這不符合OAuth規範,是嗎?無論如何,與DevDefined.OAuth我可以很容易地解決這個問題之前調用WithQueryParameters像這樣調用SignWithoutToken:

public string GetResponse() 
{    
    HttpWebResponse response = session.Request().Get().ForUrl(url). 
     SignWithoutToken(). 
     WithQueryParameters(new { start_time = 1364962612, end_time = 1364991412 }). 
     ToWebResponse(); 
    var result = GetWebResponseAsString(response); 
    return result; 
}