2016-08-18 28 views
2

Postmanを使用すると、PostmanのOAuth 1.0 Authorizationを使用して、Twitter APIを使用して適切にクエリし、作成したユーザーを作成できました。しかしRestSharpで同じことをしようとするとUnauthorizedエラーが出ます。OAuth1 RestSharpでのTwitter APIのGETおよびPOSTメソッドの認証

"UNAUTHORIZED_ACCESS" - "このリクエストは正しく認証されていません" -

私のGET要求は正常に認証されますが、POST要求は失敗します。

 _twitterRestClient = new RestClient("https://ads-api.twitter.com/1") 
     { 
      Authenticator = OAuth1Authenticator.ForProtectedResource(ConsumerKey, ConsumerSecret, AccessToken, AccessSecret) 
     }; 

     var restRequest1 = new RestRequest(string.Format("/accounts/{0}/tailored_audiences", TwitterAccountId), Method.GET); 
     //this works and gives me a list of my tailored audiences 
     var response1 = _twitterRestClient.Execute(restRequest1); 

     var restRequest2 = new RestRequest(string.Format("/accounts/{0}/tailored_audiences?name=SampleAudience2&list_type=EMAIL", TwitterAccountId), Method.POST); 
     // this results in an "Unauthorized" status code , and the message {\"code\":\"UNAUTHORIZED_ACCESS\",\"message\":\"This request is not properly authenticated\"} 
     var response2 = _twitterRestClient.Execute(restRequest2); 

答えて

1

これは、RestSharp OAuth1実装の特徴が原因であることが判明しました。私はこの問題に関連すると思う - https://www.bountysource.com/issues/30416961-oauth1-not-specifing-parameter-type。 OAuth1署名の作成には、要求内のすべてのパラメータとその他の詳細を収集し、すべてをハッシュします。 HTTPメソッドがPOSTの場合のように、RestSharpはクエリーストリングのパラメータを期待していません(それは理にかなっています)。とにかく、パラメータを明示的に追加すると、それらは取得され、OAuth1の署名が機能します。 (これらのパラメータがポストボディにある場合はtwitter APIが機能するので、クエリ文字列に明示的に追加する必要はありません)。更新されたコードは現在動作します:

 _twitterRestClient = new RestClient("https://ads-api.twitter.com/1") 
     { 
      Authenticator = OAuth1Authenticator.ForProtectedResource(ConsumerKey, ConsumerSecret, AccessToken, AccessSecret) 
     }; 

     var restRequest1 = new RestRequest(string.Format("/accounts/{0}/tailored_audiences", TwitterAccountId), Method.GET); 
     var response1 = _twitterRestClient.Execute(restRequest1); 

     var restRequest2 = new RestRequest(string.Format("/accounts/{0}/tailored_audiences", TwitterAccountId), Method.POST); 
     restRequest2.AddParameter("name", "SampleAudience2"); 
     restRequest2.AddParameter("list_type", "EMAIL"); 
     var response2 = _twitterRestClient.Execute(restRequest2); 
関連する問題