我在使用HTTP POST和Oauth 1,RestClient C#时遇到了困难。我可以使用HTTP GET和Oauth进行成功的调用,但是由于某种原因HTTP Post失败。我可以使用Postman使用相同的凭据成功进行HTTP POST调用,但是不能使用RestSharp。也许有人可以帮助我解决问题。
以下是工作正常的HTTP POST Oauth1邮递员呼叫的屏幕截图:
上面的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#尝试了很多方法,但没有任何效果。我要缺少什么,以便匹配工作中的邮递员要求。在RestSharp中,HTTP GET对我有用,只有HTTP Post不起作用。
谢谢
答案 0 :(得分:0)
好吧,我终于开始工作了,以自己尝试执行签名创建的错误方式进行了操作,因为我错误地解释了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:)