# Amazon onebox fails on my site, but works here

**URL:** https://meta.discourse.org/t/amazon-onebox-fails-on-my-site-but-works-here/41609
**Category:** Bug
**Created:** [27 במרץ,‏ 2016,‏ 2:25am UTC](https://meta.discourse.org/t/amazon-onebox-fails-on-my-site-but-works-here/41609 "2016-03-27T02:25:35Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![mcwumbly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mcwumbly/32/103861_2.png) [@mcwumbly](https://meta.discourse.org/u/mcwumbly)
#### Post date: [27 במרץ,‏ 2016,‏ 2:25am UTC](https://meta.discourse.org/t/amazon-onebox-fails-on-my-site-but-works-here/41609/1 "2016-03-27T02:25:35Z")

</div>

I’ve tried adding both `amazon.com` and `www.amazon.com` as whitelisted domains.

I tried to pasted this link and many others from Amazon into a new topic on my site:

```plaintext
http://www.amazon.com/The-Advantage-Organizational-Everything-Business/dp/1491510803

```

When I do, I get the following in Chrome’s dev tools console:

```plaintext
https://www.myforumdomain.com/onebox?url=http%3A%2F%2Fwww.amazon.com%2FThe-Advantage-Organizational-Everything-Business%2Fdp%2F1491510803d&refresh=false
Failed to load resource: the server responded with a status of 404 ()

```

When I paste it here, it works… see?

> **[Advantage, The: Patrick Lencioni, Patrick Lencioni: 9781491510803: Amazon.com:...](https://www.amazon.com/Advantage-Organizational-Health-Everything-Business/dp/1491510803)**
>
> Advantage, The \[Patrick Lencioni, Patrick Lencioni\] on Amazon.com. \*FREE\* shipping on qualifying offers. Advantage, The

I see this other topic referencing a bug fixed before, but that was a while ago:

> [@Amazon onebox returning 404](https://meta.discourse.org/t/amazon-onebox-returning-404/35077):
>
> On my production site, I’m getting 404’s in the oneboxing of every Amazon link I enter. On my dev site, they resolve beautifully. At first I thought this was a conflict with plugins, in particular figured it was likely to be related to discourse-affiliate tacking a tag onto the query string. But it works fine on my dev machine. Any clue on why this would happen? I’ve been trying to trace the code of amazon\_onebox on my dev machine with a debugger to understand the details, but I can’t find th…

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [27 במרץ,‏ 2016,‏ 4:56pm UTC](https://meta.discourse.org/t/amazon-onebox-fails-on-my-site-but-works-here/41609/2 "2016-03-27T16:56:23Z")

</div>

What version of Discourse are you running? Do you have any plugins installed that might be conflicting?

---

<div class="post-metadata">

### Author: ![mcwumbly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mcwumbly/32/103861_2.png) [@mcwumbly](https://meta.discourse.org/u/mcwumbly)
#### Post date: [27 במרץ,‏ 2016,‏ 5:07pm UTC](https://meta.discourse.org/t/amazon-onebox-fails-on-my-site-but-works-here/41609/3 "2016-03-27T17:07:25Z")

</div>

I’m pretty well up to date and only have standard plugins enabled.

The ‘static pages’ plugin is installed, but disabled… let me try getting rid of that completely:

 ![](https://global.discourse-cdn.com/meta/original/3X/1/8/1844d52580bde530e25bcc4b3e4ef3ab5ecbac5a.png)

 ![](https://global.discourse-cdn.com/meta/original/3X/f/2/f2c04436684e0cff645b940ea0f58642d03abb26.png)

---

<div class="post-metadata">

### Author: ![mcwumbly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mcwumbly/32/103861_2.png) [@mcwumbly](https://meta.discourse.org/u/mcwumbly)
#### Post date: [27 במרץ,‏ 2016,‏ 5:19pm UTC](https://meta.discourse.org/t/amazon-onebox-fails-on-my-site-but-works-here/41609/4 "2016-03-27T17:19:38Z")

</div>

Same issue after rebuilding the container without that plugin:

 ![](https://global.discourse-cdn.com/meta/original/3X/6/8/68d176572306c5f7777256a8c8b0a44e574541c4.png)

 ![](https://global.discourse-cdn.com/meta/original/3X/d/5/d5e744e26fbbadf01fbfca8e7d4fcfbaf2598cef.png)

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [27 במרץ,‏ 2016,‏ 5:32pm UTC](https://meta.discourse.org/t/amazon-onebox-fails-on-my-site-but-works-here/41609/5 "2016-03-27T17:32:20Z")

</div>

Is amazon blocked on the network that server is on?

---

<div class="post-metadata">

### Author: ![mcwumbly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mcwumbly/32/103861_2.png) [@mcwumbly](https://meta.discourse.org/u/mcwumbly)
#### Post date: [27 במרץ,‏ 2016,‏ 6:31pm UTC](https://meta.discourse.org/t/amazon-onebox-fails-on-my-site-but-works-here/41609/6 "2016-03-27T18:31:41Z")

</div>

Hmm… It’s just a vanilla Digital Ocean install, but I should do some basic troubleshooting to check that.

---

<div class="post-metadata">

### Author: ![DeanMarkTaylor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/deanmarktaylor/32/102462_2.png) [@DeanMarkTaylor](https://meta.discourse.org/u/DeanMarkTaylor)
#### Post date: [27 במרץ,‏ 2016,‏ 7:30pm UTC](https://meta.discourse.org/t/amazon-onebox-fails-on-my-site-but-works-here/41609/7 "2016-03-27T19:30:05Z")

</div>

I’ve just tested this on a live Discourse instance, one that is running a little behind latest `v1.5.0.beta12 +135` - I will update and test again when I get chance… but…

Although I’ve white-listed the domain `www.amazon.co.uk` and restarted (not rebuild) Discourse I see the same results as @mcwumbly mentions in Chrome Dev Tools.

I’ve verified that I can connect to the URL:

```plaintext
root@forum:~# curl http://www.amazon.co.uk/Belkin-BSV103-SurgeCube-Protector-Charging/dp/B00P2GW7MG -o deleteme.html --verbose
* Hostname was NOT found in DNS cache
  % Total % Received % Xferd Average Speed Time Time Time Current
                                 Dload Upload Total Spent Left Speed
  0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Trying 176.32.108.186...
* Connected to www.amazon.co.uk (176.32.108.186) port 80 (#0)
> GET /Belkin-BSV103-SurgeCube-Protector-Charging/dp/B00P2GW7MG HTTP/1.1
> User-Agent: curl/7.35.0
> Host: www.amazon.co.uk
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Sun, 27 Mar 2016 18:07:47 GMT
* Server Server is not blacklisted
< Server: Server
< pragma: no-cache
< x-amz-id-1: 1QKR29Z3J28QM1BPTS6V
< p3p: policyref="https://www.amazon.co.uk/w3c/p3p.xml",CP="CAO DSP LAW CUR ADM IVAo IVDo CONo OTPo OUR DELi PUBi OTRi BUS PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA HEA PRE LOC GOV OTC "
< cache-control: no-cache, no-transform
< x-frame-options: SAMEORIGIN
< expires: -1
< x-ua-compatible: IE=edge
< Vary: Accept-Encoding,User-Agent
< Content-Type: text/html; charset=UTF-8
< Transfer-Encoding: chunked
<
{ [data not shown]
100 439k 0 439k 0 0 496k 0 --:--:-- --:--:-- --:--:-- 496k
* Connection #0 to host www.amazon.co.uk left intact
root@forum:~#

```

And again it embeds fine here on meta:

> **[Belkin Extension Lead with USB Slots x 2 (2.4 A Shared), 4 Way/4 Plug Extension,...](https://www.amazon.co.uk/Belkin-BSV401af2M-Protection-Extension-Charging/dp/B00OE43I9S)**
>
> Safeguard your electronics with this 4-outlet power strip featuring 2 USB ports for charging. Surge protection and a 2m cable provide convenient, reliable power.

And only the standard plugins for me:

 ![](https://global.discourse-cdn.com/meta/original/3X/6/5/6590acaea95122f21cfba5e65b5ab051586dd5d4.png)

Doing a bit of debugging I tried executing the following:

```plaintext
[4] pry(main)> url = "http://www.amazon.com/gp/product/B005T3GRNW/ref=s9_simh_gw_p147_d0_i2"
review}" == preview.t=> "http://www.amazon.com/gp/product/B005T3GRNW/ref=s9_simh_gw_p147_d0_i2"
[5] pry(main)> preview = Onebox.preview(url)
=> #<Onebox::Preview:0x007f2f1a97f270
 @cache=
  #<Moneta::Expires:0x007f2f1674ca38
   @adapter=
    #<Moneta::Transformer::JsonPrefixKeyJsonValue:0x007f2f1674cab0
     @adapter=#<Moneta::Adapters::Memory:0x007f2f16795710 @backend={}>,
     @features=[:increment, :create],
     @prefix="">,
   @default_expires=nil>,
 @engine_class=Onebox::Engine::AmazonOnebox,
 @options=
  #<OpenStruct cache=#<Moneta::Expires:0x007f2f1674ca38 @adapter=#<Moneta::Transformer::JsonPrefixKeyJsonValue:0x007f2f1674cab0 @adapter=#<Moneta::Adapters::Memory:0x007f2f16795710 @backend={}>, @prefix="", @features=[:increment, :create]>, @default_expires=nil>, connect_timeout=5, timeout=10, load_paths=["/var/www/discourse/vendor/bundle/ruby/2.0.0/gems/onebox-1.5.35/templates"], twitter_client=TwitterApi>,
 @url="http://www.amazon.com/gp/product/B005T3GRNW/ref=s9_simh_gw_p147_d0_i2">
[6] pry(main)> "#{preview}" == preview.to_s
NoMethodError: undefined method `value' for nil:NilClass
from /var/www/discourse/vendor/bundle/ruby/2.0.0/gems/onebox-1.5.35/lib/onebox/engine/amazon_onebox.rb:38:in `image'
[7] pry(main)> 

```

> <https://github.com/discourse/onebox/blob/63501e1096d2b52810d8465e3239be7f9d87bc02/lib/onebox/engine/amazon_onebox.rb#L38>

After executing this I can see now `raw` contains a value when viewing the object `preview` (too large to paste here)

This seems to indicate that:

1. the expected HTML response has changed
2. the parsing of the HTML doesn’t correctly catch an error which occurs during the “parsing” of the response.
3. an error is not being surfaced to the Discourse logs

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [27 במרץ,‏ 2016,‏ 7:32pm UTC](https://meta.discourse.org/t/amazon-onebox-fails-on-my-site-but-works-here/41609/8 "2016-03-27T19:32:38Z")

</div>

Hmm possibly @tgxworld can have a peek

---

<div class="post-metadata">

### Author: ![mcwumbly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mcwumbly/32/103861_2.png) [@mcwumbly](https://meta.discourse.org/u/mcwumbly)
#### Post date: [27 במרץ,‏ 2016,‏ 7:44pm UTC](https://meta.discourse.org/t/amazon-onebox-fails-on-my-site-but-works-here/41609/9 "2016-03-27T19:44:59Z")

</div>

Just tried a couple things:

1. From the DO box itself I can curl this URL fine:  
`http://www.amazon.com/The-Advantage-Organizational-Everything-Business/dp/1491510803`

2. If I curl from within the docker container, though, I get a 503:

```plaintext
* Connection #0 to host www.google.com left intact
root@host:/# curl http://www.amazon.com/The-Advantage-Organizational-Everything-Business/dp/1491510803 -v -o deleteme
* Hostname was NOT found in DNS cache
  % Total % Received % Xferd Average Speed Time Time Time Current
                                 Dload Upload Total Spent Left Speed
  0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Trying 54.239.17.6...
* Connected to www.amazon.com (54.239.17.6) port 80 (#0)
> GET /The-Advantage-Organizational-Everything-Business/dp/1491510803 HTTP/1.1
> User-Agent: curl/7.35.0
> Host: www.amazon.com
> Accept: */*
>
< HTTP/1.1 503 Service Unavailable
< Date: Sun, 27 Mar 2016 19:41:45 GMT
* Server Server is not blacklisted
< Server: Server
< Vary: Cookie,Accept-Encoding,User-Agent
< Last-Modified: Thu, 05 Nov 2015 22:42:39 GMT
< ETag: "562-523d2d81655c0"
< Accept-Ranges: bytes
< Content-Length: 1378
< Cneonction: close
< Content-Type: text/html

```

response body contains this at the top:

```plaintext
<!--
        To discuss automated access to Amazon data please contact api-services-support@amazon.com.
        For information about migrating to our APIs refer to our Marketplace APIs at https://developer.amazonservices.com/ref=rm_5_sv, or our Product Advertising API at https://affiliate-program.amazon.com/gp/advertising/api/detail/main.html/ref=rm_5_ac for advertising use cases.
-->

```

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [27 במרץ,‏ 2016,‏ 9:19pm UTC](https://meta.discourse.org/t/amazon-onebox-fails-on-my-site-but-works-here/41609/10 "2016-03-27T21:19:06Z")

</div>

So Amazon is restricting access from Digital Ocean.

---

<div class="post-metadata">

### Author: ![mcwumbly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mcwumbly/32/103861_2.png) [@mcwumbly](https://meta.discourse.org/u/mcwumbly)
#### Post date: [27 במרץ,‏ 2016,‏ 9:20pm UTC](https://meta.discourse.org/t/amazon-onebox-fails-on-my-site-but-works-here/41609/11 "2016-03-27T21:20:27Z")

</div>

Hmm… seems a little more nuanced than that since the call succeeds when made directly from the DO box, but not when made from within the container.

---

<div class="post-metadata">

### Author: ![DeanMarkTaylor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/deanmarktaylor/32/102462_2.png) [@DeanMarkTaylor](https://meta.discourse.org/u/DeanMarkTaylor)
#### Post date: [28 במרץ,‏ 2016,‏ 1:23am UTC](https://meta.discourse.org/t/amazon-onebox-fails-on-my-site-but-works-here/41609/12 "2016-03-28T01:23:53Z")

</div>

Tracing the code a more accurate request for testing is:

```bash
curl http://www.amazon.com/gp/aw/d/1491510803 -v -o deleteme -H "User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS
 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A405 Safari/7534.48.3"

```

Note the:

- manipulated URL
- from: `http://www.amazon.com/The-Advantage-Organizational-Everything-Business/dp/1491510803`
- to: `http://www.amazon.com/gp/aw/d/1491510803`
- Specific mobile User Agent.

I’ve now tested on latest:

- whilst the test `curl` request above returns `200 OK`
- I still see a `404 (Not Found)` error in Chrome Dev Tools for `/onebox?url=...`
- I have not seen a `503 (Service Unavailable)` error
- This Discourse instance is on DigitialOcean (LON1)

---

<div class="post-metadata">

### Author: ![mcwumbly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mcwumbly/32/103861_2.png) [@mcwumbly](https://meta.discourse.org/u/mcwumbly)
#### Post date: [28 במרץ,‏ 2016,‏ 1:26am UTC](https://meta.discourse.org/t/amazon-onebox-fails-on-my-site-but-works-here/41609/13 "2016-03-28T01:26:58Z")

</div>

Are you curling from inside the container (after doing `./auncher enter app`)?

---

<div class="post-metadata">

### Author: ![DeanMarkTaylor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/deanmarktaylor/32/102462_2.png) [@DeanMarkTaylor](https://meta.discourse.org/u/DeanMarkTaylor)
#### Post date: [28 במרץ,‏ 2016,‏ 1:33am UTC](https://meta.discourse.org/t/amazon-onebox-fails-on-my-site-but-works-here/41609/14 "2016-03-28T01:33:13Z")

</div>

It’s the same for me both inside and outside of the container (successful `200 OK`, no HTML comment message at top of page code).

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [28 במרץ,‏ 2016,‏ 5:53am UTC](https://meta.discourse.org/t/amazon-onebox-fails-on-my-site-but-works-here/41609/15 "2016-03-28T05:53:26Z")

</div>

It looks like @tgxworld may have fixed something here?

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [28 במרץ,‏ 2016,‏ 5:56am UTC](https://meta.discourse.org/t/amazon-onebox-fails-on-my-site-but-works-here/41609/16 "2016-03-28T05:56:57Z")

</div>

This is a super weird bug…

```plaintext
(byebug) Nokogiri::HTML(open(url, {read_timeout: timeout}.merge(http_params)).read).css("#main-image").first.attributes.keys
["alt", "src", "data-fling-asin", "data-fling-refmarker", "data-midres-replacement", "onload", "class", "id", "data-a-image-name", "data-a-hires"]
(byebug) Nokogiri::HTML(open(url, {read_timeout: timeout}.merge(http_params)).read).css("#main-image").first.attributes.keys
["alt", "src", "data-fling-asin", "data-fling-refmarker", "data-hires-replacement", "onload", "data-a-image-name", "class", "id", "data-a-dynamic-image", "style"]
(byebug) Nokogiri::HTML(open(url, {read_timeout: timeout}.merge(http_params)).read).css("#main-image").first.attributes.keys
["alt", "src", "data-fling-asin", "data-fling-refmarker", "data-hires-replacement", "onload", "data-a-image-name", "class", "id", "data-a-dynamic-image", "style"]
(byebug) Nokogiri::HTML(open(url, {read_timeout: timeout}.merge(http_params)).read).css("#main-image").first.attributes.keys
["alt", "src", "data-fling-asin", "data-fling-refmarker", "data-hires-replacement", "onload", "data-a-image-name", "class", "id", "data-a-dynamic-image", "style"]
(byebug) Nokogiri::HTML(open(url, {read_timeout: timeout}.merge(http_params)).read).css("#main-image").first.attributes.keys
["alt", "src", "data-fling-asin", "data-fling-refmarker", "data-hires-replacement", "onload", "data-a-image-name", "class", "id", "data-a-dynamic-image", "style"]
(byebug) Nokogiri::HTML(open(url, {read_timeout: timeout}.merge(http_params)).read).css("#main-image").first.attributes.keys
["alt", "src", "data-fling-asin", "data-fling-refmarker", "data-midres-replacement", "onload", "class", "id", "data-a-image-name", "data-a-hires"]
(byebug) 

```

For some reason, the attributes in the image keeps changing. Note how it alternates between `[data-a-hires, data-hires-replacement]`and `[data-a-dynamic-image, data-midres-replacement]`

Why? I’m not sure and I’ve spent too much time trying to figure out why. I did manage to reproduce the different image tag attributes in my browser by loading the same product URL normally and in incognito mode.

I’ll wait a day or two before merging my PR below to see if anyone might know why…

[https://github.com/discourse/onebox/pull/318](https://github.com/discourse/onebox/pull/318)

---

<div class="post-metadata">

### Author: ![mcwumbly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mcwumbly/32/103861_2.png) [@mcwumbly](https://meta.discourse.org/u/mcwumbly)
#### Post date: [28 במרץ,‏ 2016,‏ 6:00am UTC](https://meta.discourse.org/t/amazon-onebox-fails-on-my-site-but-works-here/41609/17 "2016-03-28T06:00:10Z")

</div>

😌 I was pretty sure I was just going crazy…

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [28 במרץ,‏ 2016,‏ 6:00am UTC](https://meta.discourse.org/t/amazon-onebox-fails-on-my-site-but-works-here/41609/18 "2016-03-28T06:00:22Z")

</div>

O anyway there was another bug fix here… We’re getting a 404 because our code blows up if it can’t retrieve an image. I fixed that for now so even if the image is not found. We’ll still have a nice onebox without the image.

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [29 במרץ,‏ 2016,‏ 2:57am UTC](https://meta.discourse.org/t/amazon-onebox-fails-on-my-site-but-works-here/41609/19 "2016-03-29T02:57:32Z")

</div>

Just updated onebox!

> **[Amazon.com: Dell UltraSharp U2715H 27-Inch Screen LED-Lit Monitor : Electronics](https://www.amazon.com/Dell-UltraSharp-27-Inch-LED-Lit-Monitor/dp/B00P0EQD1Q)**
>
> Superior screen resolution and real estate for a view you can't wait to get your eyes on. Work the way you want. Position your monitor for ultimate comfort with a full range of tilt, pivot, swivel and height adjustments along with easy connections to...

> **[Belkin Extension Lead with USB Slots x 2 (2.4 A Shared), 4 Way/4 Plug Extension,...](https://www.amazon.co.uk/Belkin-BSV401af2M-Protection-Extension-Charging/dp/B00OE43I9S)**
>
> Safeguard your electronics with this 4-outlet power strip featuring 2 USB ports for charging. Surge protection and a 2m cable provide convenient, reliable power.

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [31 במרץ,‏ 2016,‏ 2:58am UTC](https://meta.discourse.org/t/amazon-onebox-fails-on-my-site-but-works-here/41609/20 "2016-03-31T02:58:03Z")

</div>

This topic was automatically closed after 2 days. New replies are no longer allowed.
