Re: [API] Invalid API key or access token (unrecognized login or wrong password

Solved

[API] Invalid API key or access token (unrecognized login or wrong password

hookbeak
Shopify Partner
15 1 6

I'm using asp.net and restsharp to connect to the api.

 

The code i'm using can successfully get orders, products, complete orders, uncomplete orders and a lot of other functions. 

However, i'm trying to add a product and getting the error: [API] Invalid API key or access token (unrecognized login or wrong password

Code here:

 

     Public api_token = "APITOKENGOESHERE"
     Public api_client = New RestSharp.RestClient("https://MYSTORE.myshopify.com")
     Public base_api_path = "/admin/api/2022-04/"

     Dim api_path As String = base_api_path & "products.json"
     Dim api_request = New RestSharp.RestRequest(api_path, Method.POST)


     Dim product = New Product()
     product.Title = "TEST PRODUCT"
     product.body_html = "body here"
     product.vendor = "TESTING"
     Dim json_object = JsonConvert.SerializeObject(product)

     api_request.AddJsonBody(json_object)

     api_request.AddParameter("access_token", api_token)
     Dim api_response As New RestSharp.RestResponse
     api_response = api_client.Execute(api_request)
     Dim my_result_json = api_response.Content.ToString

 


The JSON generated seems fine, and i'm authenticating the same way i authenticate dozens of other api calls that all work fine. The header for the request is correctly showing as "application/json".

Any ideas? do these requests authenticate in a completely different way to other requests?

Accepted Solution (1)

hookbeak
Shopify Partner
15 1 6

This is an accepted solution.

Solution in case anyone has similar in future:

     Public api_token = "APITOKENGOESHERE"
     Public api_client = New RestSharp.RestClient("https://MYSTORE.myshopify.com")
     Public base_api_path = "/admin/api/2022-04/"

     Dim api_path As String = base_api_path & "products.json"
     Dim api_request = New RestSharp.RestRequest(api_path, Method.POST)

     api_request.AddHeader("X-Shopify-Access-Token", api_token)

     Dim product = New Product()
     product.Title = "TEST PRODUCT"
     product.body_html = "body here"
     product.vendor = "TESTING"
     product.product_type = "TEST"
     product.price = "1"

     Dim json_object = " { ""product"": " & JsonConvert.SerializeObject(product) & " }"

     api_request.AddJsonBody(json_object)

     api_request.AddParameter("access_token", api_token)
     Dim api_response As New RestSharp.RestResponse
     api_response = api_client.Execute(api_request)
     Dim my_result_json = api_response.Content.ToString

 

Essentially this was the crux of it:

 api_request.AddHeader("X-Shopify-Access-Token", api_token)


Why this function needs this, yet none of my other functions do - i don't know. But, success!

View solution in original post

Reply 1 (1)

hookbeak
Shopify Partner
15 1 6

This is an accepted solution.

Solution in case anyone has similar in future:

     Public api_token = "APITOKENGOESHERE"
     Public api_client = New RestSharp.RestClient("https://MYSTORE.myshopify.com")
     Public base_api_path = "/admin/api/2022-04/"

     Dim api_path As String = base_api_path & "products.json"
     Dim api_request = New RestSharp.RestRequest(api_path, Method.POST)

     api_request.AddHeader("X-Shopify-Access-Token", api_token)

     Dim product = New Product()
     product.Title = "TEST PRODUCT"
     product.body_html = "body here"
     product.vendor = "TESTING"
     product.product_type = "TEST"
     product.price = "1"

     Dim json_object = " { ""product"": " & JsonConvert.SerializeObject(product) & " }"

     api_request.AddJsonBody(json_object)

     api_request.AddParameter("access_token", api_token)
     Dim api_response As New RestSharp.RestResponse
     api_response = api_client.Execute(api_request)
     Dim my_result_json = api_response.Content.ToString

 

Essentially this was the crux of it:

 api_request.AddHeader("X-Shopify-Access-Token", api_token)


Why this function needs this, yet none of my other functions do - i don't know. But, success!