繁体   English   中英

RestSharp C# HTTP POST Oauth 1

[英]RestSharp C# HTTP POST Oauth 1

我在使用 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;
    }

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

谢谢

好的,我终于开始工作了,尝试自己实现签名创建的方式同样错误......因为我错误地解释了 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();
}

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

谢谢皮特,这段代码为我节省了数小时的尝试和错误! 我在这里添加的唯一代码是Realm

OAuth1Authenticator lAuthenticator = OAuth1Authenticator.ForAccessToken(consumer_key, consumer_secret, access_token, token_secret, RestSharp.Authenticators.OAuth.OAuthSignatureMethod.HmacSha256);
lAuthenticator.Realm = "my_Realm";
client.Authenticator = lAuthenticator;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM