RestSharp C# HTTP POST Oauth 1

luc*_*cas 5 c# api http restsharp

我在使用 HTTP POST 和 Oauth 1、RestClient C# 时遇到了困难。我能够使用 HTTP GET 和 Oauth 进行成功调用,但由于某种原因 HTTP Post 失败。我能够使用 Postman 使用相同的凭据成功进行 HTTP POST 调用,但不能使用 RestSharp。也许有人可以帮助我解决这个问题。

以下是 HTTP POST Oauth1 Postman 调用工作的屏幕截图:

在此输入图像描述

在此输入图像描述

在此输入图像描述

上面的 Postman 设置工作得很好,这是我到目前为止在 C# 和 RestClient 中的设置:

        public bool CreateShippingTemplate(string storeProviderStoreId, string consumerKey, string consumerSecret, string oAuthToken, string oAuthTokenSecret)
    {
        string url = "/shipping/templates";
        var request = GenerateSecureRequest(url, RequestType.POST, consumerKey, consumerSecret, oAuthToken, oAuthTokenSecret);

        var dataObj = new //ShippingTemplate
        {
            title = "Test Title 2",
            origin_country_id = "209",
            primary_cost = "1",
            secondary_cost = "1"
        };

        string dataObjJson = JsonConvert.SerializeObject(dataObj);

        request.AddParameter("application/json", dataObjJson, ParameterType.RequestBody);
        request.RequestFormat = DataFormat.Json;

        request.Method = Method.POST;
        request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
        var response = _restClient.ExecuteAsPost(request,"POST");

        return true;
    }



        private RestRequest GenerateSecureRequest(string url, RequestType requestType, string consumerKey, string consumerSecret, string oAuthToken, string oAuthTokenSecret)
    {
        OAuthBase oAuth = new OAuthBase();

        string nonce = oAuth.GenerateNonce();
        string timeStamp = oAuth.GenerateTimeStamp();
        string normalizedUrl;
        string normalizedRequestParameters;

        string relativeUri = url;
        string sig = oAuth.GenerateSignature(new Uri(BASE_URL.ToString() + relativeUri), consumerKey, consumerSecret, oAuthToken, oAuthTokenSecret, requestType.ToString(), timeStamp, nonce, out normalizedUrl, out normalizedRequestParameters);

        var request = new RestRequest(relativeUri);
        request.Resource = string.Format(relativeUri);
        request.Method = Method.GET;
        request.AddParameter("oauth_consumer_key", consumerKey);
        request.AddParameter("oauth_token", oAuthToken);
        request.AddParameter("oauth_nonce", nonce);
        request.AddParameter("oauth_timestamp", timeStamp);
        request.AddParameter("oauth_signature_method", "HMAC-SHA1");
        request.AddParameter("oauth_version", "1.0");
        request.AddParameter("oauth_signature", sig);

        return request;
    }
Run Code Online (Sandbox Code Playgroud)

我用 RestClient 和 C# 尝试了很多东西,但没有任何效果。为了满足工作邮递员的要求,我缺少什么。HTTP GET 在 RestSharp 中为我工作,只有 HTTP Post 不起作用。

谢谢

Pet*_*zak 3

好吧,我终于开始工作了,尝试自己实现签名创建时也犯了同样的错误。因为我错误地解释了 PostMan RestSharp 代码。这可以更容易地完成,因为 RestSharp 会为您完成这一切!我希望这是 PostMan 作为“代码”示例给出的版本。无论如何,这在 C# 中对我有用(快速控制台应用程序):

const string consumer_key = "abcd1234";
const string consumer_secret = "1234abcd";
const string access_token = "1a2b3c4d";
const string token_secret = "a12b3c4d";
const string URL = "https://aa.web.site.net/history/api/account/123456789/givemedata/search?offset=0&limit=50";

static void Main(string[] args)
{
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
var client = new RestClient(URL);
client.Authenticator = OAuth1Authenticator.ForAccessToken(consumer_key, consumer_secret, access_token, token_secret, RestSharp.Authenticators.OAuth.OAuthSignatureMethod.HmacSha1);
client.Timeout = -1;
var request = new RestRequest(URL, Method.POST);

request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\"start\": {\"from\": 1583074556000, \"to\": 1588258556000 } }", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
Console.ReadKey();
}
Run Code Online (Sandbox Code Playgroud)

是的,我知道,没有标头,没有参数排序和散列..没有 nonce-nse :)