Php Curl with Shopify 403 Forbidden (Scope undefined for API access)
I am receiving a 403 Forbidden when trying to create a smart_collection using CURL. The response I am getting is:
"errors":"Scope undefined for API access: collections.
I have on previous private Shopify apps used the same CURL block of code to create products successfully. I have also reviewed all permissions on Shopify for the private app and can confirm they are set to highest.
My question is, what is additionally required in order to successfully POST a smart_collection to Shopify. How do I define the scope when posting?
<?php
//this gets the collection name from the URL
if(isset($_GET['id'])){
$collection_name = $_GET['id'];
}
$collection_array = array(
"smart_collection"=>array(
"title"=> $collection_name,
"rules"=>array(
array(
"column" => "tag",
"relation" => "equals",
"condition" => $collection_name
),
array(
"column" => "variant_inventory",
"relation" => "greater_than",
"condition" => 0
)
)
)
);
echo json_encode($collection_array);
echo "<br />";
$url ="https://apikey:password@mystore.myshopify.com
/admin/smart_collections.json";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:
application/json'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 0);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS,
json_encode($collection_array));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
$response = curl_exec ($curl);
curl_close ($curl);
echo "<pre>";
print_r($response);
Expected Results: A smart_collection should be created on Shopify as per the $colletion_array, example:
{
"smart_collection": {
"title": "3DLightFX",
"rules": [
{
"column": "tag",
"relation": "equals",
"condition": "3DLightFX"
},
{
"column": "variant_inventory",
"relation": "greater_than",
"condition": 0
}
]
}
}
Actual results: I am getting a
403 forbidden
and the response is:
{"errors":"Scope undefined for API access: collections. Valid scopes: admin_notifications, ..."}
Answer
Solution:
Update: I was posting to the incorrect URL. I have since updated.
For anyone else struggling with this, when creating a "smart_collection", use the following URL:
https://___:___@___.myshopify.com//admin/smart_collections.json
Source
Didn't find the answer?
Our community is visited by hundreds of Shopify development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.