AmazonFresh recipe API issues with affiliate tag

798 Views Asked by At

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.

1

There are 1 best solutions below

2
mustberuss On

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})