[英]Rails - gem instagram - issues on access token and sandbox
我需要将客户的完整instagram feed放到网站上。 我尝试了几种访问instagram并获取整个feed的方法,但遇到一些错误。
使用宝石“ instagram”(最简单的方法)时,当我尝试获取访问令牌时遇到以下错误。
Instagram::BadRequest: POST https://api.instagram.com/oauth/access_token/: 400: OAuthException: Matching code was not found or was already used.
我尝试使用浏览器遵循instagram文档https://www.instagram.com/developer/authentication/上的指示,可以获取访问令牌,但无法在ruby中手动进行http调用。
我需要下载所有数据并将其保存在数据库中(就像我经常做的那样),因此我需要控制器中的所有内容。
我的代码很简单:
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
我确定在我的Instragram注册应用和代码中都包含相同的redirect_url。
如gem仓库中的示例应用程序所示,您将需要首先调用authorize_url:
Instagram.authorize_url(:redirect_uri => CALLBACK_URL)
然后您将在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
解决了! 我误解了api的用法。
我在此https://elfsight.com/blog/2016/05/how-to-get-instagram-access-token/之后获得了访问令牌
我可以在模型中使用我的帖子:
access_token = Instagram.access_token
client = Instagram.client(access_token: access_token)
response = client.user_recent_media
并在我的配置文件中:
Instagram.configure do |config|
config.client_id = "CLIENT_ID"
config.client_secret = "CLIENT_SECRET"
config.access_token = "ACCESS_TOKEN"
end
谢谢你的帮忙
您应在omniauth.auth哈希中处理回调时获得令牌:request.env ['omniauth.auth'] ['credentials'] ['token']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.