0

I am successfully getting access token, but when I call customer service it is throwing error.

Error:Bad Request/Unauthorized

for this I have tried below code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Http;
using System.Net.Http.Headers;


namespace ConsoleApplication2
{


    class Program
    {

        private const string URL = "http://192.168.0.52/magento214/index.php/rest/V1/integration/admin/token";
        private const string URL1 = "http://192.168.0.52/magento214/index.php/rest/V1/customers/";
        private const string DATA = @"{""username"": ""admin"",""password"": ""pass@word1""}";
        private const string DATA1 = @"{""id"": ""1""}";

        static void Main(string[] args)
        {
            try
            {
                AddComponent();
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        private static void AddComponent()
        {

            try
            {
                System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
                //client.BaseAddress = new System.Uri(URL);
                byte[] cred = UTF8Encoding.UTF8.GetBytes("username:password");
                client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", Convert.ToBase64String(cred));
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                System.Net.Http.HttpContent content = new StringContent(DATA, UTF8Encoding.UTF8, "application/json");
                HttpResponseMessage messge = client.PostAsync(URL, content).Result;
                string description = string.Empty;
                if (messge.IsSuccessStatusCode)
                {
                    string result = messge.Content.ReadAsStringAsync().Result;
                    description = result;
                    AddComponent1(description);
                }
                else
                {
                    string result = messge.Content.ReadAsStringAsync().Result;
                    description = result;
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        private static void AddComponent1(string key)
        {

            try
            {
                var rowString = key.Replace('"', ' ').Trim();
                System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
                //client.BaseAddress = new System.Uri(URL);
                byte[] cred = UTF8Encoding.UTF8.GetBytes(rowString);
                client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", Convert.ToBase64String(cred));
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                System.Net.Http.HttpContent content = new StringContent(DATA1, UTF8Encoding.UTF8, "application/json");
                HttpResponseMessage messge = client.PostAsync(URL1, content).Result;
                string description = string.Empty;
                if (messge.IsSuccessStatusCode)
                {
                    string result = messge.Content.ReadAsStringAsync().Result;
                    description = result;
                    getCustomerList(description);
                }
                else
                {
                    string result = messge.Content.ReadAsStringAsync().Result;
                    description = result;
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }

}

Any help on this?

3
  • 1
    I'm voting to close this question as off-topic because , its a c# code Mar 22 '17 at 9:57
  • @MurtuzaZabuawala not a off-topic, here I am invoking magento 2 rest api. Mar 22 '17 at 10:07
  • 1
    I'm voting to close this question as off-topic because essentially this is a question about how to connect to ANY API in c#, not specifically about certain Magento API features
    – 7ochem
    Mar 23 '17 at 12:58
2

change your AddComponent1(string key) method

private static void AddComponent1(string key)
        {

            try
            {
                var rowString = key.Replace('"', ' ').Trim();
                System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
                //client.BaseAddress = new System.Uri(URL);
                byte[] cred = UTF8Encoding.UTF8.GetBytes(rowString);
                client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", Convert.ToBase64String(cred));
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                System.Net.Http.HttpContent content = new StringContent(DATA1, UTF8Encoding.UTF8, "application/json");
                HttpResponseMessage messge = client.PostAsync(URL1, content).Result;
                string description = string.Empty;
                if (messge.IsSuccessStatusCode)
                {
                    string result = messge.Content.ReadAsStringAsync().Result;
                    description = result;
                    getCustomerList(description);
                }
                else
                {
                    string result = messge.Content.ReadAsStringAsync().Result;
                    description = result;
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }

to

public static void AddComponent1(string key)  
        {
            var rowString = key.Replace('"', ' ').Trim();
            Uri geturi = new Uri("http://192.168.0.52/bcw214/index.php/rest/V1/customers/1"); //replace your url  
            System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", rowString);
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            System.Net.Http.HttpResponseMessage messge = client.GetAsync(geturi, 0).Result;
            if (messge.IsSuccessStatusCode)
            {
                string result = messge.Content.ReadAsStringAsync().Result;
            }
            else
            {
                string result = messge.Content.ReadAsStringAsync().Result;
            }
        }  

it will work. Please let me know if it is not working.

0

To answer a bit more generally:

You were POST-ing data when you should GET it. POST or PUT should only be used when you intend to change the data on the server. For just reading existing data, you should only ever use GETrequests. This is a basic tenet of REST.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.