RestSharp C# HTTP POST Oauth 1 [英] RestSharp C# HTTP POST Oauth 1

查看:31
本文介绍了RestSharp C# HTTP POST Oauth 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am running into a difficulty with HTTP POST and Oauth 1, RestClient C#. I am able to make successful calls with HTTP GET and Oauth, but for some reason HTTP Post fails. I was able to make successful HTTP POST calls with same credentials using Postman, but not with RestSharp. Perhaps someone could help me figuring out the issue.

以下是有效的 HTTP POST Oauth1 Postman 调用的屏幕截图:

Here are the screenshots with working HTTP POST Oauth1 Postman call:

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

The Postman setup above works just fine, and here is what I have so far in C# and 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;
    }

我用 RestClient 和 C# 尝试了很多东西,但没有任何效果.我错过了什么,以匹配工作邮递员的请求.HTTP GET 在 RestSharp 中对我有用,只有 HTTP Post 不起作用.

I tried many things with RestClient and C#, nothing worked. What am I missing, in order to match the working Postman request. HTTP GET is working for me in RestSharp, only HTTP Post is not working.

谢谢

推荐答案

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

Ok, I finally got my working, went the same wrong way trying to implement signature creation myself.. as I badly interpreted PostMan RestSharp code. This could be done much easier as RestSharp does it all for you! I wish this was the version PostMan gave as a "code" example. Anyway, this works for me in C# (quick console app):

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();
}

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

Yes, I know, no headers, no param sorting and hashing.. no nonce-nse :)

这篇关于RestSharp C# HTTP POST Oauth 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆