[英]Restful API working in postman, C# not Working [Magento Integration]
I am integrating with Magento 2, using RESTful APIs.我正在使用 RESTful API 与 Magento 2 集成。 When I use postman, it works like charm, while in C# code, it returns "Unauthorized 401" exception.当我使用邮递员时,它就像魅力一样工作,而在 C# 代码中,它返回“未经授权的 401”异常。
However, It was working in C# code earlier, but suddenly it stopped working.但是,它之前在 C# 代码中工作,但突然停止工作。
I have tried every way, I tried (WebRequest, HTTPClient & RESTsharp) the same exception returned.我已经尝试了各种方法,我尝试了(WebRequest、HTTPClient 和 RESTsharp)返回相同的异常。
Also, I am using Fiddler 4 to catch & match the requests, I used Fiddler to C# plugins to extract C# code, also I used the RESTsharp Code of Postman same exception returned.此外,我使用 Fiddler 4 来捕获和匹配请求,我使用 Fiddler 到 C# 插件来提取 C# 代码,我还使用了 Postman 的 RESTsharp 代码返回相同的异常。
The remote server returned an error: (401) Unauthorized.远程服务器返回错误:(401) 未经授权。
//Calls request functions sequentially.
private string MakeRequests()
{
HttpWebResponse response;
if (Request_hatolna_co(out response))
{
//Success, possibly uses response.
string responseText = ReadResponse(response);
response.Close();
return responseText;
}
else
{
//Failure, cannot use response.
return "";
}
}
private static string ReadResponse(HttpWebResponse response)
{
using (Stream responseStream = response.GetResponseStream())
{
Stream streamToRead = responseStream;
if (response.ContentEncoding.ToLower().Contains("gzip"))
{
streamToRead = new GZipStream(streamToRead, CompressionMode.Decompress);
}
else if (response.ContentEncoding.ToLower().Contains("deflate"))
{
streamToRead = new DeflateStream(streamToRead, CompressionMode.Decompress);
}
using (StreamReader streamReader = new StreamReader(streamToRead, Encoding.UTF8))
{
return streamReader.ReadToEnd();
}
}
}
private bool Request_hatolna_co(out HttpWebResponse response)
{
response = null;
try
{
//Create a request to URL.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://MAGENTO.co/index.php/rest//V1/orders/items?searchCriteria[filter_groups][0][filters][0][field]=item_id&searchCriteria[filter_groups][0][filters][0][value]=1");
//Set request headers.
request.KeepAlive = true;
request.Headers.Set(HttpRequestHeader.Authorization, "Bearer xxxxxxxxxxxxxxxxxxxxxx");
request.Headers.Add("Postman-Token", @"1181fa03-4dda-ae84-fd31-9d6fbd035614");
request.Headers.Set(HttpRequestHeader.CacheControl, "no-cache");
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36";
request.ContentType = "application/json";
request.Accept = "*/*";
request.Headers.Set(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
request.Headers.Set(HttpRequestHeader.AcceptLanguage, "en-US,en;q=0.9,ar;q=0.8,la;q=0.7");
request.Headers.Set(HttpRequestHeader.Cookie, @"store=default; private_content_version=f16533d4f181d42a1b3f386fa6d2cdf1");
//Get response to request.
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException e)
{
//ProtocolError indicates a valid HTTP response, but with a non-200 status code (e.g. 304 Not Modified, 404 Not Found)
if (e.Status == WebExceptionStatus.ProtocolError) response = (HttpWebResponse)e.Response;
else return false;
}
catch (Exception)
{
if (response != null) response.Close();
return false;
}
return true;
}
Why Postman-Token has been set in C# code? 为什么用C#代码设置了Postman-Token? Remove it and then try. 删除它,然后尝试。
The problem was in the URL, where Magento's (Server's) Admin Changed it to [HTTPS] instead of [HTTP]. 问题出在URL,Magento(服务器)管理员将其更改为[HTTPS],而不是[HTTP]。
That concludes the difference between [Postman, Insomnia, or any other API app] & the C# code, that C# doesn't handle the [HTTP vs HTTPs], while the API app can handle it. 总结了[Postman,Insomnia或任何其他API应用程序]与C#代码之间的区别,即C#不处理[HTTP vs HTTPs],而API应用程序可以处理它。
I am facing the same issue with my Magento Authentication API.我的 Magento 身份验证 API 面临同样的问题。 It works fine on my machine (both C# and Postman), but when I deployed it to a staging server, Postman gives the right response (User Token), but with C#/RestSharp it's giving me this response:它在我的机器上运行良好(C# 和 Postman),但是当我将它部署到临时服务器时,Postman 给出了正确的响应(用户令牌),但是使用 C#/RestSharp 它给了我这个响应:
The account sign-in was incorrect or your account is disabled temporarily. Please wait and try again later.
anyone had the same problem ?有人有同样的问题吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.