使用Rails、HTTParty和Paperclip“OpenURI::HTTPError: 401未授权”

时间:2022-12-08 23:17:43

I'm using HTTParty to get data from the Eventbrite API.

我使用HTTParty从Eventbrite API获取数据。

response = HTTParty.get("https://www.eventbriteapi.com/v3/events/search?token=#{Figaro.env.eventbrite_oauth_token}")   

I am trying to save an Event attachment to my object. (I'm using paperclip to upload images).

我试图将事件附件保存到我的对象中。(我用paperclip上传图片)

@event.image = "https://img.evbuc.com/http%3A%2F%2Fcdn.evbuc.com%2Fimages%2F18699100%2F162201087004%2F1%2Foriginal.jpg?h=200&w=450&rect=0%2C194%2C2000%2C1000&s=67649c6c8e7ef66e409189bf08474203"

But I get the error:

但我得到了一个错误:

"OpenURI::HTTPError: 401 Unauthorized" with Rails, HTTParty, and Paperclip."

“OpenURI::HTTPError: 401未经授权”,“使用Rails、HTTParty和Paperclip。”

Any ideas on how to fix?

有什么办法吗?

2 个解决方案

#1


3  

I've noticed in newer versions of Paperclip, I've needed to wrap any URL strings in URI.parse when asking Paperclip to import the asset via URL.

我注意到在最新版本的Paperclip中,我需要将任何URL字符串封装到URI中。当请求Paperclip通过URL导入资产时进行解析。

So for your example:

所以对于你的例子:

@event.image = URI.parse("https://img.evbuc.com/http%3A%2F%2Fcdn.evbuc.com%2Fimages%2F18699100%2F162201087004%2F1%2Foriginal.jpg?h=200&w=450&rect=0%2C194%2C2000%2C1000&s=67649c6c8e7ef66e409189bf08474203")

#2


0  

I looked into Paperclip code to see why wrapping the URL in URI.parse works.

我研究了Paperclip代码,以了解为什么要用URI包装URL。解析工作。

As it turns out, Paperclip uses different adapter classes to handle different targets.

事实证明,Paperclip使用不同的适配器类来处理不同的目标。

The reason your attachment failed in the first place is because of a recent change in URL handling in HttpUrlProxyAdapter which handles URLs. This change is preventing from attaching files that have URLs that are already encoded.

您的附件首先失败的原因是HttpUrlProxyAdapter中URL处理的最近变化,它处理URL。此更改防止附加已编码的url的文件。

Wrapping the URL with URI.parse made sure that your attachment was handled by URI Adapter.

用URI包装URL。解析确保您的附件是由URI适配器处理的。

#1


3  

I've noticed in newer versions of Paperclip, I've needed to wrap any URL strings in URI.parse when asking Paperclip to import the asset via URL.

我注意到在最新版本的Paperclip中,我需要将任何URL字符串封装到URI中。当请求Paperclip通过URL导入资产时进行解析。

So for your example:

所以对于你的例子:

@event.image = URI.parse("https://img.evbuc.com/http%3A%2F%2Fcdn.evbuc.com%2Fimages%2F18699100%2F162201087004%2F1%2Foriginal.jpg?h=200&w=450&rect=0%2C194%2C2000%2C1000&s=67649c6c8e7ef66e409189bf08474203")

#2


0  

I looked into Paperclip code to see why wrapping the URL in URI.parse works.

我研究了Paperclip代码,以了解为什么要用URI包装URL。解析工作。

As it turns out, Paperclip uses different adapter classes to handle different targets.

事实证明,Paperclip使用不同的适配器类来处理不同的目标。

The reason your attachment failed in the first place is because of a recent change in URL handling in HttpUrlProxyAdapter which handles URLs. This change is preventing from attaching files that have URLs that are already encoded.

您的附件首先失败的原因是HttpUrlProxyAdapter中URL处理的最近变化,它处理URL。此更改防止附加已编码的url的文件。

Wrapping the URL with URI.parse made sure that your attachment was handled by URI Adapter.

用URI包装URL。解析确保您的附件是由URI适配器处理的。