2016-08-18 2 views
2

Postman을 사용하여 Postman의 OAuth 1.0 Authorization을 사용하여 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 서명이 작동합니다. (이 매개 변수가 게시물 본문에있는 경우 트위터 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);