Development discussions around Shopify APIs
In the API docs, the following are possible actions you can do with script tags:
But using Python API, I am unsure how I would go about replicating these calls. I have managed to create a script_tag given an URL, but how can I count them, list them, find one by URL and delete them?
My current working "create_script_tag" function looks like this:
def upsert_script_tag(self, src): script_reference={"event": "onload", "src": f"{src}"} print(f"UPSERTING SCRIPT TAG WITH '{script_reference}'") st=shopify.ScriptTag(script_reference) ret = st.save() if st.errors: print(f"ERROR: {st.errors.full_messages()}") print(f" + Return was :{ret}")
But my naive attempt at finding a script tag fails:
def find_script_tags(self, src): print(f"FINDING SCRIPT TAGS WITH src='{src}'") st=shopify.ScriptTag(src) # WARNING DOES NOT WORK!!! DO NOT COPY ret = st.find(src=src) if st.errors: print(f"ERROR: {st.errors.full_messages()}") print(f" + Return was :{ret}")
All hints welcome!
Solved! Go to the solution
This is an accepted solution.
Hey @devdude,
You're absolutely right. I was looking for a find_by method in pyactiveresource, but really the query params at the bottom of the readme is what was needed. Thanks for sharing.
In terms of your question about the session not getting created and activated properly, did the session get reset somehow? Each time your app server starts you would need to recreate and activate your session. Also, is it possible your access token is an online one? Online access tokens expire with admin sessions, so you may need to generate a new one if you specified that you wanted an online token initially.
Andrew | Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit the Shopify Help Center or the Shopify Blog
Hi @devdude,
I'd recommend taking a look at the pyactiveresource documentation, as it explains the find method. Essentially, you need to make sure you're searching by the object's id. So if you have a script tag with id of 123456789, you would want the following:
st = shopify.ScriptTag.find(123456789)
Andrew | Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit the Shopify Help Center or the Shopify Blog
Busfox,
thanks for the answer! But the original REST api accepts a filter on src. The way I understood it any named args I put will be forwarded as url parameters, so my src=should really work in this case!
Further after some testing I found that It worked perfectly a few times then stopped working without changes to the code. I then found what I think is a bug in activeresource. If I do the following it works but returns 401:
cls=shopify.ScriptTag print(f" + headers: {cls.headers}") if not cls.headers: cls.headers={'bob':'lol'} ret=cls.find(src=src)
The bug is that activeresources crashes with headers that has value "None" here:
File "/usr/local/lib/python3.7/site-packages/pyactiveresource/connection.py", line 267, in <listcomp> header_string = '\n'.join([':'.join((k, v)) for k, v in TypeError: sequence item 1: expected str instance, NoneType found
And after further testing I found that the header that was missing was actually the token header (from my print statement above):
{'User-Agent': 'ShopifyPythonAPI/5.1.2 Python/3.7.4', 'X-Shopify-Access-Token': None}
So now the original question was answered but a new one arises, why did my token not propegate to the ScriptTag query even if I had created and activated a session using the token?
If you can answer this I would be overjoyed!
This is an accepted solution.
Hey @devdude,
You're absolutely right. I was looking for a find_by method in pyactiveresource, but really the query params at the bottom of the readme is what was needed. Thanks for sharing.
In terms of your question about the session not getting created and activated properly, did the session get reset somehow? Each time your app server starts you would need to recreate and activate your session. Also, is it possible your access token is an online one? Online access tokens expire with admin sessions, so you may need to generate a new one if you specified that you wanted an online token initially.
Andrew | Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit the Shopify Help Center or the Shopify Blog
I found an error in my code that did not set the session up properly. It works now 🙂
User | RANK |
---|---|
10 | |
5 | |
3 | |
3 | |
3 |
Photo by Marco Verch Sales channels on Shopify are various platforms where you can sell...
By Ollie May 25, 2023Summary of EventsBeginning in January of 2023, some merchants reported seeing a large amo...
By Trevor May 15, 2023With 2-Factor Authentication being required to use Shopify Payments, we’re here to help yo...
By Imogen Apr 26, 2023