Focuses on API authentication, access scopes, and permission management.
Hello everyone,
I do have problems in connecting my Shopify Admin REST -API via my own Server. Postman seems fine, with only the Access-Token.
What's wrong with my Code-Snippet? Did I use the wrong credentials?
It is structured like this:
var settings = {
"url": "https://{{api_key}}:{{api_password}}@{{store_name}}.myshopify.com/admin/api/{{api_version}}/custom_collections.json";
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "json/application",
"Authorization": "Basic *******"
},
"data": " {\"custom_collection\": {\r\n \"title\": \"MacbooksTEST123456\",\r\n \"body_html\": \"Description of the collection\", \n \"sort_order\": \"manual\", \n \"template_suffix\": \"your_template_suffix\", \n \"published_scope\": \"web\"\r\n }}",
};
Therefore it looks like that:
var settings = {
"url": "110a2*****:sh***@shop*****.myshopify.com/admin/api/2023-10/custom_collections",
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "json/application",
"Authorization": "Basic MTEwYTJiZWU1YjN********"
},
"data": " {\"custom_collection\": {\r\n \"title\": \"MacbooksTEST123456\",\r\n \"body_html\": \"Description of the collection\", \n \"sort_order\": \"manual\", \n \"template_suffix\": \"your_template_suffix\", \n \"published_scope\": \"web\"\r\n }}",
};
When I send my request in Postman, I receive a 200 Response and get redirected to the Login page of my shop, but nothing been uploaded/updated actually.
Here's my (anonymous) credentials:
API credentials
API key (=username)
110a***
API secret key
a41e***
API apiEndpointURL
https://shop***.myshopify.com/
Client credentials
Client ID
1bf1**
Client secret
d34**
Help's appreciated very much!
All the Best!
Patrick
Hey there Patrick, I spent some time working with this in my Python code on a previous project. Not sure what language you're using...but in general, the below approach may be of some assistance.
URL Format: The URL in your settings object should not include the API key and password directly in the URL. Instead, use your shopify acess token in the headers for authentication. Your URL should be in the format: "https://{{store_name}}.myshopify.com/admin/api/{{api_version}}/custom_collections.json".
Headers: The "X-Shopify-Access-Token" header should be set to your access token found in your admin profile / settings
var settings = {
"url": "https://{{store_name}}.myshopify.com/admin/api/{{api_version}}/custom_collections.json",
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "application/json",
"X-Shopify-Access-Token": ADMIN_API_ACCESS_TOKEN
},
"data": JSON.stringify({
"custom_collection": {
"title": "MacbooksTEST123456",
"body_html": "Description of the collection",
"sort_order": "manual",
"template_suffix": "your_template_suffix",
"published_scope": "web"
}
})
};
Make sure you have the correct API version, and ensure that your access token is current and valid. If everything is set up correctly, this code should create a custom collection in your Shopify store without any redirection issues.
Here is an example of a GET request I made to shopify API with Python and Flask, for reference.
@Anonymous_bp.route('/products', methods=['GET'])
def get_all_products():
# Define the URL for the REST API request
url = f'https://{SHOP_NAME}.myshopify.com/admin/api/{ADMIN_API_VERSION}/products.json'
# Headers for the REST API request
headers = {
'Content-Type': 'application/json',
'X-Shopify-Access-Token': ADMIN_API_ACCESS_TOKEN
}
# Make the REST API request
response = requests.get(url, headers=headers)
# Check if the request was successful
if response.status_code == 200:
return jsonify(response.json())
else:
return jsonify({'error': 'Unable to fetch products'}), response.status_code
Curious how this goes...let me know!
Hey there Jclewis,
Thanks a lot for your reply! I have tried your approach, which seems to be working fine in Postman for GETs and POSTs. But when I try to use (almost) the exact code (because the "data"-part ist missing in the POST), I do get at. I don't know. Seems like requess from another server do not work the same.
Cheers,
Patrick
Btw, I am using JavaScript, sorry, for not pointing out! 🙂
{
"responseCode": 401, "errorResponse": "{\"errors\":\"[API] Invalid API key or access token (unrecognized login or wrong password)\"}" }
Thanks Patrick, I can certainly help troubleshoot if you are able to provide the server-side code POST request you are making to the Shopify API.
You mentioned Javascript, are you using Node.js and Express as a backend?
If you are able to share your collection in POSTMAN as well, I can look at that too. I am very familiar with that API testing toolkit.
Hey Jclewis,
thanks for helping! 🙂
Sure, I can show you the serversided code:
For now I am not using Node or Express.
Do you think I have to, to make things work?
Here's the code:
This is the reponse in my tomcat-log:
Hope that helps!
Thanks a lot!
Cheers,
Patrick
Hello everyone,
is there anyone who can help me?
Thank you very much!
Patrick