Issues getting started, guidance needed

New Member
4 0 0

Hello everyone, I'm just starting, and the code is not working.

I'm on a Macbook, using VSCode to code in Python. I installed ShopifyAPI from the command line.

I followed the instruction to create what is I believe a test store and a private app.

 

I want to test this little sample:

 

import shopify

# Replace the following with your shop URL
shop_url = "https://7bd1cfd1def58542c7e10bca7c2682d5:2f661fc0d0d542b54859fbdad69d7573@easyflexibility-store.myshopify.com/admin/"
shopify.ShopifyResource.set_site(shop_url)

# Create a new product
new_product = shopify.Product()
new_product.title = "The Best App Ever"
new_product.product_type = "Software"
new_product.save()

# Create a new order
new_order = shopify.Order()
new_order.line_items = [{
    "quantity": 1,
    "variant_id": new_product.variants[0].id
}]
new_order.save()

I get this very long error message... I have no idea what to do.

 

Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pyactiveresource/connection.py", line 286, in _open
http_response = self._handle_error(self._urlopen(request))
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pyactiveresource/connection.py", line 316, in _urlopen
return urllib.request.urlopen(request, timeout=self.timeout)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 531, in open
response = meth(req, response)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 641, in http_response
'http', request, response, code, msg, hdrs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 569, in error
return self._call_chain(*args)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 503, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "creation_test.py", line 11, in <module>
new_product.save()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pyactiveresource/activeresource.py", line 824, in save
data=self.encode())
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pyactiveresource/connection.py", line 364, in post
return self._open('POST', path, headers=headers, data=data)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/shopify/base.py", line 23, in _open
self.response = super(ShopifyConnection, self)._open(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pyactiveresource/connection.py", line 288, in _open
http_response = self._handle_error(err)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pyactiveresource/connection.py", line 406, in _handle_error
raise ForbiddenAccess(err)
pyactiveresource.connection.ForbiddenAccess: Response(code=403, body="b'{"errors":"[API] This action requires merchant approval for write_products scope."}'", headers={'Server': 'nginx', 'Date': 'Fri, 05 Apr 2019 01:13:44 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Transfer-Encoding': 'chunked', 'Connection': 'close', 'Vary': 'Accept-Encoding', 'X-Sorting-Hat-PodId': '59', 'X-Sorting-Hat-ShopId': '9233662012', 'Referrer-Policy': 'origin-when-cross-origin', 'X-Frame-Options': 'DENY', 'X-ShopId': '9233662012', 'X-ShardId': '59', 'X-Stats-UserId': '', 'X-Stats-ApiClientId': '2843777', 'X-Stats-ApiPermissionId': '110984659004', 'X-Shopify-API-Terms': 'By accessing or using the Shopify API you agree to the Shopify API License and Terms of Use at https://www.shopify.com/legal/api-terms', 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT': '1/40', 'X-Shopify-Shop-Api-Call-Limit': '1/40', 'Strict-Transport-Security': 'max-age=7889238', 'X-Request-Id': 'c2341a63-72fb-41ac-a6ce-db364f73d6ef', 'X-Shopify-Stage': 'production', 'Content-Security-Policy': "default-src 'self' data: blob: 'unsafe-inline' 'unsafe-eval' https://* shopify-pos://*; block-all-mixed-content; child-src 'self' https://* shopify-pos://*; connect-src 'self' wss://* https://*; frame-ancestors 'none'; img-src 'self' data: blob: https:; script-src https://cdn.shopify.com https://checkout.shopifycs.com https://js-agent.newrelic.com https://bam.nr-data.net https://dme0ih8comzn4.cloudfront.net https://api.stripe.com https://mpsnare.iesnare.com https://appcenter.intuit.com https://www.paypal.com https://maps.googleapis.com https://www.google-analytics.com https://v.shopify.com https://widget.intercom.io https://js.intercomcdn.com 'self' 'unsafe-inline' 'unsafe-eval'; upgrade-insecure-requests; report-uri /csp-report?source%5Baction%5D=create&source%5Bapp%5D=Shopify&source%5Bcontroller%5D=admin%2Fproducts&source%5Bsection%5D=admin_api&source%5Buuid%5D=c2341a63-72fb-41ac-a6ce-db364f73d6ef", 'X-Content-Type-Options': 'nosniff', 'X-Download-Options': 'noopen', 'X-Permitted-Cross-Domain-Policies': 'none', 'X-XSS-Protection': '1; mode=block; report=/xss-report?source%5Baction%5D=create&source%5Bapp%5D=Shopify&source%5Bcontroller%5D=admin%2Fproducts&source%5Bsection%5D=admin_api&source%5Buuid%5D=c2341a63-72fb-41ac-a6ce-db364f73d6ef', 'X-Dc': 'chi2,gcp-us-east1'}, msg="Forbidden")

Manager for ElasticSteel and EasyFlexibility - dtkach@elasticsteel.com
0 Likes
Highlighted
Shopify Partner
21 0 3

Hey @Daniel_Tkach ,

 

It seems to be an authorization issue. If you read the logs carefully you will find:

urllib.error.HTTPError: HTTP Error 403: Forbidden // This tells me this is an authorization issue

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "creation_test.py", line 11, in <module>
new_product.save() // Probably missing write_products permission.

My guess is that you are missing write_products scope for that API-key. Try generating an API-key with those scopes and try again.

Read more about scopes here.

Founder of Evermark - Smart and automated Facebook ads for your Shopify store.
0 Likes
Tourist
4 0 0

Thank you Emil.

I checked the scopes, I think they are correct. Could it be that the app is not installed in the test store, or if it's a private app that's good enough? App says "unpublished" and I don't know how to publish it. When I go to "test your app" and I try to install it I get this in the browser:

This site can’t be reached www.easyflexibility-store.com’s server IP address could not be found.
DNS_PROBE_FINISHED_NXDOMAIN

0 Likes
Shopify Partner
21 0 3

The app should be installed in the test store, but it is good enough as a private app.

If you added it as a private app, I would check API permissions in the private app settings once again at

https://yourstore.myshopify.com/admin/apps/private/#APPID

 

I did a quick test with the following

import shopify

# Replace the following with your shop URL
shop_url = "https://7bd1cfd1def58542c7e10bca7c2682d5:2f661fc0d0d542b54859fbdad69d7573@easyflexibility-store.myshopify.com/admin/"
shopify.ShopifyResource.set_site(shop_url)
shop = shopify.Shop.current()
print shop.name
print shopify.ProductListing.product_ids()

#Output
$ python index.py
EasyFlexibility Store
[]

So the script is able to connect to your store, but not create a new product. So I would check permissions again. Hope it helps, otherwise, I am out of clues for now. 

Founder of Evermark - Smart and automated Facebook ads for your Shopify store.
0 Likes
Tourist
4 0 0

Thank you Emil.

So I erased that app and created a new one and that worked. Changing the permissions wasn't doing it, but the new one I created I did it with read and write access.

Now what I tried is this:

import shopify
import requests
import json

shop_url = "https://cf923a6188cf6d278eac68a8ebb7bc14:4b2196c3565b89b2e4a061abea0f67bc@easyflexibility.myshopify.com/admin/orders.json"

orders = requests.get(shop_url).json()

json_string = json.dumps(orders)
print(json_string)

The issue is I get 8 orders at random. How do I pick what orders to get? If I want to get orders from certain date.

0 Likes
Shopify Partner
21 0 3
You can filter orders from API by passing parameters with the url, for example: /admin/orders.json?updated_at_min=2005-07-31T15:57:11-04:00

Take a look here https://help.shopify.com/en/api/reference/orders/order#index
Founder of Evermark - Smart and automated Facebook ads for your Shopify store.
0 Likes
Tourist
4 0 0

Yea, that one wasn't working with T-03:00, different time zone... but -04 works.

Now how do I access a "subfield" in the JSON, for example customer.last_name, that one didn't work.

To do something like this

fields=email,customer.last_name,name,total-price,line_items.sku

I think the "." is not correct, or this can't be done?

0 Likes
Shopify Partner
21 0 3

Hi again Daniel,

 

You are right. It is not possible to retrieve subfields from the REST API like that.

 

Your options are:

1. Filter it on the client.

2. Get orders first and then do separate call to retrieve the customers (/customers.json) for those orders.

3. Use the GraphAPI instead which supports subfield queries.

 

Option 1 should work fine in most scenarios.

Founder of Evermark - Smart and automated Facebook ads for your Shopify store.
0 Likes