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

查看:111
本文介绍了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邮递员呼叫的屏幕截图:

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#尝试了很多方法,但没有任何效果.我要缺少什么,以便与工作中的邮递员要求相匹配.在RestSharp中,HTTP GET对我有用,只有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可以为您轻松完成所有工作!我希望这是PostMan给出的代码"版本例子.无论如何,这在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天全站免登陆