cancel
Showing results for 
Search instead for 
Did you mean: 

NoAuthorized http error when try to consume customers api rest

NoAuthorized http error when try to consume customers api rest

Hello everyone, 

 

I spend a coupled of weeks trying to consume customer api rest from Magento 2. following the oficial documentation. http://devdocs.magento.com/guides/v2.0//get-started/authentication/gs-authentication-oauth.html  , because I want to consume from a thrid-party app.  

I followed the next steps:

1.- enable magento api integration with full access to all resources(I got consumer key, consumer secret, access token and access secret) 

2.- on Postman(google app) I can consume the customers API rest, by doing a GET request filling on oauth information the previous generated keys.(see attached image)

3.- But when I try to consume from my own application passing the same keys, I got the next error:  StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: 

 

endpoint: http://localhost:81/magento2/index.php/rest/V1/customers/search?searchCriteria=*

this is my code:

Snippet

Dictionary<stringstring> header = new Dictionary<stringstring>();
sb.Clear();
sb.Append("oauth_consumer_key=" + Uri.EscapeDataString("aiw95k5ksfdgu0i..."));
sb.Append(",oauth_token=" + Uri.EscapeDataString("4q99ui0nd.."));
sb.Append(",oauth_signature_method=" + Uri.EscapeDataString("HMAC-SHA1"));
sb.Append(",oauth_timestamp=" + Uri.EscapeDataString("1498620801"));
sb.Append(",oauth_nonce=" + Uri.EscapeDataString("AiOITo"));
sb.Append(",oauth_version=" + Uri.EscapeDataString("1.0"));
sb.Append(",oauth_signature=" + "ZWRCm61qR9r0WBnIT57guEWhVE..");
header.Add("Authorization"$"OAuth {sb.ToString()}");
var response = call("rest/default/V1/customers/search?searchCriteria=*"method.get, header, new JObject());

Snippet

public JObject call(string endpoint, method method, Dictionary<stringstring> headers = nullJObject data = null)
       {
           try
           {
               using (var client = new HttpClient())
               {
                   client.BaseAddress = new Uri(baseurl);
                   client.DefaultRequestHeaders.Accept.Clear();
                   client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                   foreach (var head in headers)
                   {
                       client.DefaultRequestHeaders.Add(head.Key, head.Value);
                   }
                   //executing
                   HttpResponseMessage response;
                   response = client.GetAsync(endpoint).Result;
                   if (response.IsSuccessStatusCode)
                   {
                       string respon = response.Content.ReadAsStringAsync().Result;
                       return JObject.Parse(respon);
                   }
                   else
                       throw new Exception($"{response.StatusCode.ToString()} {response.ReasonPhrase}");
               }
           }
           catch (Exception ex)
           {
               throw ex;
           }
       }

any one knows about this issue?

 

thanks in advancePOSTMAN successful request.pngRequest Error.png