使用 Postman,我能够使用 Twitter API
以及 Postman 的 OAuth 1.0 授权成功查询和创建定制受众。然而,当尝试使用 RestSharp 执行相同操作时,我收到未经授权的错误。
"UNAUTHORIZED_ACCESS" - "This request is not properly authenticated".
我的 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);
最佳答案
事实证明,这是由于 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);
关于twitter - RestSharp 中针对 Twitter API GET 和 POST 方法的 OAuth1 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39016060/