# Solicitações Onebox sendo redirecionadas incorretamente devido ao user-agent

**URL:** https://meta.discourse.org/t/onebox-requests-being-incorrectly-redirected-due-to-user-agent/120557
**Category:** Support
**Created:** [Junho 17, 2019, 7:07am UTC](https://meta.discourse.org/t/onebox-requests-being-incorrectly-redirected-due-to-user-agent/120557 "2019-06-17T07:07:06Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![JezPez](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jezpez/32/145071_2.png) [@JezPez](https://meta.discourse.org/u/JezPez)
#### Post date: [Junho 17, 2019, 7:07am UTC](https://meta.discourse.org/t/onebox-requests-being-incorrectly-redirected-due-to-user-agent/120557/1 "2019-06-17T07:07:07Z")

</div>

I’ve never developed with Ruby before so I’m struggling to understand why OneBox is returning with a blank preview from our link, when iFramely and Slack render it fine.

Here is a sample link we are trying to render, it has been whitelisted in our Discourse instance.  
`https://link.jig.space/kEhFbFwkcW`

A working iFramely link is here: [Welcome to Iframely](https://iframely.com/embed/https%3A%2F%2Flink.jig.space%2FgXszoFiWkX)  
It extracts the appropriate open graph tags to make the preview.

We are using `branch.io` as our link shortener, with some intelligent redirects if the user is on iOS and has our app installed then it opens the app, but if they’re on desktop it goes to our microsite `https://jig.space`. Could this have something to do with why OneBox can’t infer the open graph tags while iFramely can?

I tried putting a few `puts` in the code to figure out why OneBox returns a 404 when previewing our links, and although the Discourse codebase is easy enough to read, I’m too new with Ruby to investigate further.

It would be an awesome feature to have some kind of debug flag for OneBox with more verbose logging as it seems lots of people on here (including me!) have some confusions about how oEmbed and Open Graph are supposed to work.

Could anyone help me debug OneBox further or let me know of any tools to figure this out?

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [Junho 17, 2019, 7:38am UTC](https://meta.discourse.org/t/onebox-requests-being-incorrectly-redirected-due-to-user-agent/120557/2 "2019-06-17T07:38:19Z")

</div>

> [@JezPez](#):
>
> [JigSpace: The world’s highest-rated AR presentation app](https://link.jig.space/kEhFbFwkcW)

Faceboook og debugger throws a couple of warnings:

 ![image](https://global.discourse-cdn.com/meta/original/3X/2/1/21d59ea53de607bdc6e93077a9572cb77861d8bf.png)

> **[Sharing Debugger - Meta for Developers](https://developers.facebook.com/tools/debug/?q=https%3A%2F%2Flink.jig.space%2FkEhFbFwkcW)**

Not sure if that’s what’s upsetting onebox

---

<div class="post-metadata">

### Author: ![JezPez](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jezpez/32/145071_2.png) [@JezPez](https://meta.discourse.org/u/JezPez)
#### Post date: [Junho 17, 2019, 7:50am UTC](https://meta.discourse.org/t/onebox-requests-being-incorrectly-redirected-due-to-user-agent/120557/3 "2019-06-17T07:50:05Z")

</div>

A curl request responds with html which seems like it’s actually rendering the whole card.  
`curl -X GET https://link.jig.space/gXszoFiWkX`

```plaintext
 <html amp>
   <!-- ... lots of header and style stuff, with some meta tags, then the body-->
<link rel="alternate" href="ios-app://1111193492/jigspaceviewer/open?link_click_id=link-665670162338762596">
       <link rel="apple-touch-icon" href="https://jig.space/images/jigs/jig-AOp18L7z-color.3.png">
       <meta property="al:ios:url" content="jigspaceviewer://open?link_click_id=link-665670162338762596">
       <meta name="twitter:app:url:iphone" content="jigspaceviewer://open?link_click_id=link-665670162338762596">
       <meta property="al:ios:app_store_id" content="1111193492">
       <meta name="twitter:app:id:iphone" content="1111193492">
       <meta property="al:ios:app_name" content="JigSpace">
       <meta name="twitter:app:name:iphone" content="JigSpace">
           <meta name="twitter:card" content="summary_large_image">
       <meta property="og:image" content="https://jig.space/images/jigs/jig-AOp18L7z-color.3.png">
         <meta name="twitter:image:src" content="https://jig.space/images/jigs/jig-AOp18L7z-color.3.png">
       <title>Macintosh - Complete</title>
       <meta property="og:title" content="Macintosh - Complete">
         <meta name="twitter:title" content="Macintosh - Complete">
       <meta property="og:description" content="Apple Macintosh Computer">
         <meta name="twitter:description" content="Apple Macintosh Computer">
       <meta name="twitter:app:country" content="US">
   <body>
     <div class="card center">
       <div class="main-image"></div>
       <div id="content-container">
         <div class="app-title text-bold">JigSpace</div>
         <div class="card-title text-light">Macintosh - Complete</div>
         <div class="app-content text-light">Apple Macintosh Computer</div>
       </div>

```

If OneBox’s issue is the image issue which FB is complaining about, then we should add the FB open graph debugger to the OneBox docs thread: [Creating rich link previews with Onebox](https://meta.discourse.org/t/rich-link-previews-with-onebox/98088)

I’ll try to put in the og:image:width and such as FB suggests. Thanks @merefield.

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [Junho 17, 2019, 7:51am UTC](https://meta.discourse.org/t/onebox-requests-being-incorrectly-redirected-due-to-user-agent/120557/4 "2019-06-17T07:51:46Z")

</div>

I feel your pain in general though.

When oneboxing fails could we have some additional logging to say exactly why it is turning its nose up?

---

<div class="post-metadata">

### Author: ![JezPez](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jezpez/32/145071_2.png) [@JezPez](https://meta.discourse.org/u/JezPez)
#### Post date: [Junho 17, 2019, 8:05am UTC](https://meta.discourse.org/t/onebox-requests-being-incorrectly-redirected-due-to-user-agent/120557/5 "2019-06-17T08:05:45Z")

</div>

Yeah it’s just that I can’t follow the code past here:

> <https://github.com/discourse/discourse/blob/102be5a9e3a063bebe6a62927a102f84904a9bdf/lib/oneboxer.rb#L289>

Then later on in the onebox\_controller.rb, `preview.blank` is true.  
I’m pretty new to Ruby so just trying to figure out how to go deeper and output the options passed into Onebox.preview etc.

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [Junho 17, 2019, 8:18am UTC](https://meta.discourse.org/t/onebox-requests-being-incorrectly-redirected-due-to-user-agent/120557/6 "2019-06-17T08:18:40Z")

</div>

[byebug](https://edgeguides.rubyonrails.org/debugging_rails_applications.html) is extremely useful. It’s already included in the project I believe, so just put the command ‘byebug’ on it’s own line of the code and the console will pop into the code at that point.

Type any variable to find it’s value.

Use commands ‘next’, ‘step’ and ‘up’ to get around. ‘continue’ to set it off on its merry way.

It’s a bit like ‘debugger’ in javascript.

This is an extreme length to go to for a failing onebox though! 🙂 (but a great educational exercise nonetheless ).

---

<div class="post-metadata">

### Author: ![JezPez](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jezpez/32/145071_2.png) [@JezPez](https://meta.discourse.org/u/JezPez)
#### Post date: [Junho 17, 2019, 8:21am UTC](https://meta.discourse.org/t/onebox-requests-being-incorrectly-redirected-due-to-user-agent/120557/7 "2019-06-17T08:21:11Z")

</div>

I didn’t realise onebox is it’s own gem. I’ve just discovered the onebox repo and it has some awesome points:  
[https://github.com/discourse/onebox](https://github.com/discourse/onebox)

Reading the readme there: I should be able to make an engine myself to get around any og tag issues.

I can’t see any onebox option for a debug flag or anything. So I guess that is the suggestion I’m making here: There should be a `debug:true` or `verbose:true` option to pass into onebox to understand some common issues.

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [Junho 17, 2019, 8:22am UTC](https://meta.discourse.org/t/onebox-requests-being-incorrectly-redirected-due-to-user-agent/120557/8 "2019-06-17T08:22:40Z")

</div>

Yup it’s a ‘separate’ project. I’m in complete agreement about the logging. This should be verbose by default for all failures. We shouldn’t be having to break into the code to see what’s going on: it’s too common a Production problem. Facebook og debug is a nice extra, but onebox has made the decision or failed the preview using it’s own rules and it should be transparent about when it does so - perhaps I’m missing something? I obviously respect the platform’s priorities, but it would be great if this was addressed.

---

<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: [Junho 17, 2019, 8:35am UTC](https://meta.discourse.org/t/onebox-requests-being-incorrectly-redirected-due-to-user-agent/120557/9 "2019-06-17T08:35:17Z")

</div>

Does it have actual text? Image only oneboxes aren’t supported.

---

<div class="post-metadata">

### Author: ![JezPez](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jezpez/32/145071_2.png) [@JezPez](https://meta.discourse.org/u/JezPez)
#### Post date: [Junho 17, 2019, 10:20am UTC](https://meta.discourse.org/t/onebox-requests-being-incorrectly-redirected-due-to-user-agent/120557/10 "2019-06-17T10:20:20Z")

</div>

Hi @codinghorror!  
What do you mean by actual text?  
As an example here are some of the meta og tags returned from `https://link.jig.space/gXszoFiWkX`

```plaintext
<meta property="og:title" content="Macintosh - Complete">
<meta property="og:description" content="Apple Macintosh Computer">
<meta property="og:image" content="https://jig.space/images/jigs/jig-AOp18L7z-color.3.png">

```

The og data renders like this in Slack:

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

In iframely they render it like this:

 ![image](https://global.discourse-cdn.com/meta/original/3X/b/c/bcc4528eb84268ccee8a95e31756489e6fe2dfe4.png)

So I wouldn’t say it’s just the image, though it is just those three tags used so there’s not much text.

---

<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: [Junho 17, 2019, 10:23am UTC](https://meta.discourse.org/t/onebox-requests-being-incorrectly-redirected-due-to-user-agent/120557/11 "2019-06-17T10:23:26Z")

</div>

My guess is you don’t meet our minimum text content requirement for the onebox.

---

<div class="post-metadata">

### Author: ![JezPez](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jezpez/32/145071_2.png) [@JezPez](https://meta.discourse.org/u/JezPez)
#### Post date: [Junho 17, 2019, 10:27am UTC](https://meta.discourse.org/t/onebox-requests-being-incorrectly-redirected-due-to-user-agent/120557/12 "2019-06-17T10:27:50Z")

</div>

What’s the minimum?

I have descriptions like this too which also don’t render a preview from onebox:  
`<meta property="og:description" content="Create and share interactive 3D knowledge for anything, and bring it to life with Augmented Reality." />`

---

<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: [Junho 17, 2019, 10:32am UTC](https://meta.discourse.org/t/onebox-requests-being-incorrectly-redirected-due-to-user-agent/120557/13 "2019-06-17T10:32:23Z")

</div>

You’d need to look at the source code to be sure; the main time I see people get confused about this is when they expect blank or missing descriptions to work.

Other than that, we don’t see issues with this feature in general.

---

<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: [Junho 17, 2019, 10:34am UTC](https://meta.discourse.org/t/onebox-requests-being-incorrectly-redirected-due-to-user-agent/120557/14 "2019-06-17T10:34:50Z")

</div>

Oh wait, I remembered another thing that trips people up. If you are hosting on a platform that commonly blacklists `user-agent` strings, you’ll run into problems. For example, WPEngine is notorious for blacklisting pretty much every unknown user agent, so oneboxing WordPress blogs on WPEngine tends to fail as a result.

Discourse requests the onebox using the Discourse user agent.

---

<div class="post-metadata">

### Author: ![JezPez](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jezpez/32/145071_2.png) [@JezPez](https://meta.discourse.org/u/JezPez)
#### Post date: [Junho 17, 2019, 10:45am UTC](https://meta.discourse.org/t/onebox-requests-being-incorrectly-redirected-due-to-user-agent/120557/15 "2019-06-17T10:45:43Z")

</div>

Thanks @codinghorror and @merefield

When I try a curl with the header Discourse, I get a completely different result.  
`curl -X GET https://link.jig.space/gXszoFiWkX -H 'User-Agent: Discourse'`

It takes me to a 304 temp redirect to a page with a script that makes a 2nd redirect back to our home page:  
`window.top.location = validate("http://jig.space/?_branch_match_id=667900471741258199");`

And of course there are no `og:` tags in those responses.

So it is something to do with my redirects upon getting the Discourse user agent.

Thanks again 🙂 I’ll report back when I fix it.

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [Junho 17, 2019, 11:00am UTC](https://meta.discourse.org/t/onebox-requests-being-incorrectly-redirected-due-to-user-agent/120557/17 "2019-06-17T11:00:06Z")

</div>

User agent is found here:

> [@Onebox Does Not Display - iFramely Passes](https://meta.discourse.org/t/onebox-does-not-display-iframely-passes/116694/4):
>
> Yes, I can reproduce that from a VPS using curl -A "-string-"! Can we have a setting to change the user agent? wink In this case that would be a lot less hassle slight_smile Oh wait, I see a file: discourse/config/initializers/100-onebox\_options.rb require\_dependency 'twitter\_api' Onebox.options = { twitter\_client: TwitterApi, redirect\_limit: 3, user\_agent: "Discourse Forum Onebox v#{Discourse::VERSION::STRING}" } Take the word forum out? I could easily add this to my plugin o…

---

<div class="post-metadata">

### Author: ![JezPez](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jezpez/32/145071_2.png) [@JezPez](https://meta.discourse.org/u/JezPez)
#### Post date: [Junho 19, 2019, 1:21am UTC](https://meta.discourse.org/t/onebox-requests-being-incorrectly-redirected-due-to-user-agent/120557/18 "2019-06-19T01:21:50Z")

</div>

Just to close this off:  
The devs at [branch.io](http://branch.io) haven’t told me a way to detect and redirect differently for the Discourse User agent so I had to change the code on the Discourse side to remove the User Agent completely before Onebox.preview.

So if it’s a onebox request for one of our links, I remove the user agent. This is filthy but it works:

```ruby
def self.external_onebox(url)
    Rails.cache.fetch(onebox_cache_key(url), expires_in: 1.day) do
      fd = FinalDestination.new(url, ignore_redirects: ignore_redirects, ignore_hostnames: blacklisted_domains, force_get_hosts: force_get_hosts, preserve_fragment_url_hosts: preserve_fragment_url_hosts)
      uri = fd.resolve
      return blank_onebox if uri.blank? || blacklisted_domains.map { |hostname| uri.hostname.match?(hostname) }.any?

      options = {
        cache: {},
        max_width: 695,
        sanitize_config: Sanitize::Config::DISCOURSE_ONEBOX
      }

      options[:cookie] = fd.cookie if fd.cookie

      if uri.to_s.start_with?('https://link.jig.space')
        # Our branch.io links would redirect the Onebox.preview if there is a user agent so we set it blank here
        Onebox.options = { 
          user_agent: ""
        }
      else
        # But some other websites might rely on this useragent to deliver special stuff to Onebox or Discourse, so set it back
        Onebox.options = { 
          user_agent: "Discourse Forum Onebox v#{Discourse::VERSION::STRING}"
        }
      end
      if Rails.env.development? && SiteSetting.port.to_i > 0
        Onebox.options = { allowed_ports: [80, 443, SiteSetting.port.to_i] }
      end

      r = Onebox.preview(uri.to_s, options)

      { onebox: r.to_s, preview: r&.placeholder_html.to_s }
    end
  end

```

It’s not just the UserAgent for Onebox though. Before the onebox request, there is a call to `FinalDestination.new`. This takes the `ignore_redirects` array where I added our our link domain:

> <https://github.com/discourse/discourse/blob/73a45048a015890a4bce6ec7c203430900adfa02/lib/oneboxer.rb#L26>

Now it works for us,  
Thanks again for your help guys 🙂

---

<div class="post-metadata">

### Author: ![Stephen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/stephen/32/95011_2.png) [@Stephen](https://meta.discourse.org/u/Stephen)
#### Post date: [Junho 19, 2019, 2:23am UTC](https://meta.discourse.org/t/onebox-requests-being-incorrectly-redirected-due-to-user-agent/120557/19 "2019-06-19T02:23:52Z")

</div>

That’s really going to confuse anyone who tries to onebox one of your links on any other discourse instance globally.

---

<div class="post-metadata">

### Author: ![JezPez](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jezpez/32/145071_2.png) [@JezPez](https://meta.discourse.org/u/JezPez)
#### Post date: [Junho 19, 2019, 4:46am UTC](https://meta.discourse.org/t/onebox-requests-being-incorrectly-redirected-due-to-user-agent/120557/20 "2019-06-19T04:46:44Z")

</div>

Yeh I know hence:

> [@JezPez](#):
>
> This is filthy

But without being able to get [branch.io](http://branch.io) to detect Discourse AND FinalDestination user-agents, and distinguish them from other browser user agents, I don’t have a choice right now.

If I end up resolving this with [branch.io](http://branch.io) support, then I’ll come back and edit these comments because it’s not ideal.

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [Novembro 28, 2022, 5:03am UTC](https://meta.discourse.org/t/onebox-requests-being-incorrectly-redirected-due-to-user-agent/120557/21 "2022-11-28T05:03:39Z")

</div>



[Próxima página](https://meta.discourse.org/t/onebox-requests-being-incorrectly-redirected-due-to-user-agent/120557.md?page=2)
