Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Re: Help with 401 Not Authorized in C#

Solved

Help with 401 Not Authorized in C#

rhackl
Visitor
1 0 0

Hello all, 

 

I am trying to write some C# code to update content on a Shopify page in my store. When I use https://{apiKey}:{apiPassword}@phytools1.myshopify.com/admin/api/2023-01/pages/103974338796.json, I can retrieve the page information. However, when I use the following C# code, I get a 401 unauthorized error. The only API credentials that I show on my Custom App in my Shopify Admin are (Admin API access token, API key, API secret key). Any help is apreciated. 

 

using (var client = new HttpClient())
{
client.BaseAddress = new Uri($"https://phytools1.myshopify.com/admin/api/2023-01/");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
"Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($"{apiKey}:{apiPassword}")));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

var response = await client.PutAsync($"pages/{pageId}.json",
new StringContent(jsonPayload, Encoding.UTF8, "application/json"));

if (response.IsSuccessStatusCode)
{
var responseContent = await response.Content.ReadAsStringAsync();
Console.WriteLine($"New page created successfully: {responseContent}");
}
else
{
Console.WriteLine($"Error creating page: {response.StatusCode}");
}
}

Accepted Solution (1)

garyrgilbert
Shopify Partner
432 41 190

This is an accepted solution.

 

I think you need to use the accesstoken using the header X-Shopify-Access-Token instead of basic authentication.

 

Cheers,

 

Gary

- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution

View solution in original post

Reply 1 (1)

garyrgilbert
Shopify Partner
432 41 190

This is an accepted solution.

 

I think you need to use the accesstoken using the header X-Shopify-Access-Token instead of basic authentication.

 

Cheers,

 

Gary

- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution