1

I'm attempting to use the AmazonFresh recipe API, which lets you specify a number of ingredients and then redirect the user to a shopping list on Amazon: https://www.amazon.com/afx/ingredients/verify

The basic functionality works well. However, when I create a URL using the API, my tag= parameter is missing, despite passing it in the URL parameters like the API documentation states:

  Optional URL Parameter for Amazon Associates: ?tag={your_associates_tag}

When I redirect the user to the resulting URL, the tag= parameter is missing. Which means I can't track or affiliate purchases :(

I've uploaded a simple example here: https://trinket.io/python3/7170bc788d -- I'd expect, based on the documentation, that the resulting URL output contain a tag= parameter.

Note that adding it manually doesn't help. Try going here, which I added the tag to manually and you'll see the cart, but the tag gets cleared.

I rather suspect I'm doing something very basic wrong, but I can't quite figure it out.

Any tips? Anyone using this successfully? It doesn't appear that the AmazonFresh recipe API is well known or supported, I haven't got anywhere despite trying several Amazon support channels.

paulh
  • 11
  • 2
  • Hi Paul, I am also trying to use the Amazon Fresh API. How are you getting a URL in the API response? When I call the API, the response contains the entire HTML page but no URL. How did you do this? – JimmyStrings Aug 31 '22 at 21:14
  • Also, did you find a solution to the missing tag parameter? – JimmyStrings Aug 31 '22 at 21:14
  • Hi @JimmyStrings -- I ended up extracting the URL from the HTML page. `if line.find("data-encoded-recipe-url") >= 0: return "https://amazon." + ext + r.partition("data-encoded-recipe-url=")[2][1:-1].strip()` Inelegant, but it seemed to work for my little prototype. However, I never figured out the missing tag parameter. If you figure it out, please let me know! (or anyone else reading this...) – paulh Sep 01 '22 at 11:28
  • (Several attempts to contact Amazon support regarding this went nowhere. I really got the impression this API has been abandoned and no-one there knows how it works.) – paulh Sep 01 '22 at 11:32
  • Hi @paulh - Thanks for your response! I copied the code from your trinket (https://trinket.io/python3/7170bc788d) directly into a Jupyter notebook - no edits. When I run it, the following segment of the code does not print anything: `for line in response.text.split("\n"): if line.find("data-encoded-recipe-url") >= 0: print(line)` Normally, I would think that the URL we are hunting for is simply not in the HTML. But clearly it has worked for you, and I can see the URL in the test page cited in the API documentation. What am I missing? – JimmyStrings Sep 04 '22 at 21:40
  • I have also attempted to contact Amazon dev support multiple times - no responses so far. There are one or two applications that seem to be utilising this API, however I think they may be working together privately with Amazon. – JimmyStrings Sep 04 '22 at 21:42
  • I came across this, which may be helpful to you, but is beyond the scope of knowledge/ability :) http://limedaley.com/post/amazon-fresh-api – JimmyStrings Sep 04 '22 at 21:45
  • Hey Jimmy -- yeah, welcome to my world, it's terribly documented. Who have you found who is using it? I can perhaps dig into what they're doing to see if they figured out the tag thing that stumped me. Without the tag it actually all *worked*... it's just that we couldn't affiliate-track the purchases, so the business model didn't work. If you output the entire response text (print response.text), is the recipe URL anywhere in there? Perhaps they renamed the data-encoded key? – paulh Sep 05 '22 at 16:25

1 Answers1

0

This should be a comment but my reputation isn't high enough.

I didn't see where you were doing anything wrong. As a tip I'd add print_roundtrip() to your code from https://stackoverflow.com/a/61803546 to see the redirects etc.

I tried wrapping your associates tag in braces, in case they are literal, but it didn't help. I also tried injecting your associates tag in data-auth-portal-redirect-url where it has none tag%3D& but it still got lost.

What the documentation says and what https://www.amazon.com/afx/ingredients/verify does seem very different!

I did notice that the tag is getting lost on the post's redirect. I tried making the request without allowing redirects and sending it myself, adding back the tag. The tag is then in the returned data-auth-portal-redirect-url and html but still not in data-encoded-recipe-url

sys.stdout.reconfigure(encoding='utf-8')  # https://stackoverflow.com/a/63573649

with requests.Session() as session:
    payload = "ingredients=" + json.dumps(ingredients, ensure_ascii=False)
    response = session.request('POST', url, data=payload, headers=headers, params=querystring, allow_redirects=False, hooks={'response': print_roundtrip})

    # append our tag to the relative url in the location header 
    redirect_url = "https://www.amazon.com" + response.headers["Location"] + "yuminc-21"
    response = session.request('GET', redirect_url, headers=headers, hooks={'response': print_roundtrip})

Metro Smurf
  • 37,266
  • 20
  • 108
  • 140
mustberuss
  • 140
  • 6
  • Hey, thanks for looking into this -- I'll have to have a look at the data-auth-portal URL and see if it's more useful :) – paulh Sep 13 '22 at 07:28
  • Update: nah, unfortunately the tag also gets stripped when you log into the page given by the auth-portal URL. Maddening. – paulh Sep 13 '22 at 07:32