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?
rest
api.