简体   繁体   English

Rails-Gem Instagram-关于访问令牌和沙箱的问题

[英]Rails - gem instagram - issues on access token and sandbox

I need to put on a website the complete instagram feed of my customer. 我需要将客户的完整instagram feed放到网站上。 I have tried several ways to access to instagram and get the whole feed, but I get some errors. 我尝试了几种访问instagram并获取整个feed的方法,但遇到一些错误。

Using the gem "instagram" (the easiest and simplest method) i got the following error when I try to get the access token. 使用宝石“ instagram”(最简单的方法)时,当我尝试获取访问令牌时遇到以下错误。

Instagram::BadRequest: POST https://api.instagram.com/oauth/access_token/: 400: OAuthException: Matching code was not found or was already used.

I tried to follow the indication on the instagram documentation https://www.instagram.com/developer/authentication/ using my browser and I can get the access token, but I am not able to do the http calls manually in ruby. 我尝试使用浏览器遵循instagram文档https://www.instagram.com/developer/authentication/上的指示,可以获取访问令牌,但无法在ruby中手动进行http调用。

I need to download all the data and keep it inside my database (as I always done) so I need everything in the controller. 我需要下载所有数据并将其保存在数据库中(就像我经常做的那样),因此我需要控制器中的所有内容。

my code is very simple: 我的代码很简单:

accessToken = Instagram.get_access_token(Instagram.client_id, :redirect_uri => CALLBACK_URL)
client = Instagram.client(access_token: accessToken)
response = client.user_recent_media

#other code to cycle inside posts and put everything in db

i'm sure to have included the same redirect_url in my instragram registration app and in my code. 我确定在我的Instragram注册应用和代码中都包含相同的redirect_url。

As shown in the sample application in the gem's repo , you will need to call authorize_url first: gem仓库中的示例应用程序所示,您将需要首先调用authorize_url:

 Instagram.authorize_url(:redirect_uri => CALLBACK_URL)

and then you will get request on the CALLBACK_URL , where you should have an action with the code that you provided: 然后您将在CALLBACK_URL上收到请求,您应该在其中使用提供的代码进行操作:

 response = Instagram.get_access_token(params[:code], :redirect_uri => CALLBACK_URL)
 client = Instagram.client(:access_token => response.access_token)    
 # do stuff with the client

SOLVED! 解决了! I have misunderstood the usage of the api. 我误解了api的用法。

I got my access token following this https://elfsight.com/blog/2016/05/how-to-get-instagram-access-token/ 我在此https://elfsight.com/blog/2016/05/how-to-get-instagram-access-token/之后获得了访问令牌

and i can access my posts using in my model: 我可以在模型中使用我的帖子:

access_token  = Instagram.access_token
client = Instagram.client(access_token: access_token)
response = client.user_recent_media

and in my config file: 并在我的配置文件中:

Instagram.configure do |config|
config.client_id     = "CLIENT_ID"
config.client_secret = "CLIENT_SECRET"
config.access_token  = "ACCESS_TOKEN"
end

thanks a lot for your support 谢谢你的帮忙

您应在omniauth.auth哈希中处理回调时获得令牌:request.env ['omniauth.auth'] ['credentials'] ['token']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM