Getting or Updating Single Discount Codes API Calls Not Working for Me

Shopify Staff
Shopify Staff
469 36 92

Is that a request Id you are receiving in response from Shopify? As far as I can tell it is also not valid, you cannot provide us an ID, it is returned on your call.

 

If you don't log it, please provide a shop, or example of the call you are making.  Are you attempting to make a call to the URL you mentioned above?

 

/admin/api/2019-07/price_rules/384286621759/discount_codes/1646809677887.json
Developer Experience @ Shopify
0 Likes

OK, here is my full code below (with exception of actual login key/secret which I know works since I can do GET requests just fine).

I'd love to see full PHP / CURL code examples for the API calls such as this for GET/POST/PUT, and thought I'd read in this forum it was in the works?

I don't know where to read the headers info returned other than in Chrome / Inspector / Network area that I've copied further below;

 

Using the PUT request to update a price rule from here; https://help.shopify.com/en/api/reference/discounts/pricerule#update-2019-07

MY CODE:

$url = "https://MYKEY:MYPASSWORD@gravy-demo.myshopify.com/admin/api/2019-07/price_rules/384286621759.json";
$params = array('price_rule' => array(
'id' => 1646809677887,
'code' => 'COUPONTEST',
));
$shop_token = Session::get('shop_token');

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json;charset=utf-8'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params));
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-Shopify-Access-Token:'.$shop_token));
curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-Request-ID:30f14c6c1fc85cba12bfd093aa8f90e3'));
$json_response = curl_exec($curl);
curl_close($curl);
$result = json_decode($json_response, TRUE);

 

RESPONSE HEADERS:

  1. Cache-Control:
    no-cache, private
  2. Connection:
    Keep-Alive
  3. Content-Type:
    text/html; charset=UTF-8
  4. Date:
    Sun, 22 Sep 2019 17:05:06 GMT
  5. Keep-Alive:
    timeout=2, max=100
  6. Server:
    Apache
0 Likes
Highlighted

Also $scopes = "read_orders,read_products,write_products,read_price_rules,write_price_rules,read_product_listings";

0 Likes
Shopify Staff
Shopify Staff
469 36 92

Hey again,

 

Is your app a private app or public app? It looks to be an authentication issue, either incorrect password or access token depending on the app type.  If you can double check the authentication then try again.

 

Ryan

Developer Experience @ Shopify
0 Likes

It is a public app, not yet listed/published (still in progress) on the app store.  The GET requests work just fine, but not the PUT requests so it should not be an authentication issue if GET requests work, no?

0 Likes
Shopify Staff
Shopify Staff
469 36 92

GET and PUT requests are not handled the same way.  You are using two different authentication methods in your code it looks like.  You should only be using the access token, also make sure you aren't passing any cookies which are disallowed.

 

Making authenticated requests

 

PS. You are still trying to pass a request-ID header, it is returned by shopify, not something you provide.

Developer Experience @ Shopify
0 Likes

OK, that is helpful re only using the access token for public apps, thank you.   I will keep playing with this, but again, what would be most useful is actual PHP code examples of this POST / PUT API using cURL for public or private apps. I have seen this requested and promised in this forum, but no movement on it that I can see.

1 Like