【问题标题】:How can I get username and/or id through Instagram API in Ruby?如何通过 Ruby 中的 Instagram API 获取用户名和/或 ID?
【发布时间】:2015-06-24 05:58:09
【问题描述】:

我不确定如何调用诸如屏幕名称之类的方法来获取 Instagram 和 Ruby 的用户名和/或 ID:

username = Instagram.configure do |config|
  config.client_id = "YOUR_CLIENT_ID"
  config.client_secret = "YOUR_CLIENT_SECRET"
  # For secured endpoints only
  #config.client_ips = '<Comma separated list of IPs>'
end

puts username.user

【问题讨论】:

  • 你签出样本Sinatra app了吗?它非常简单。如果您无法理解演示应用程序,那么您可能应该先尝试一些更简单的项目。

标签: ruby-on-rails ruby client instagram instagram-api


【解决方案1】:

首先,您需要提供一个链接,将您带到 Instagram,然后您将被要求输入您的用户名和密码,并在那里进行验证。

<%= link_to 'Click here to go to Instagram', '/oauth/connect' %>

在您的路线文件中,

get '/oauth/connect' => 'users#connect'

在您的UsersController 课程中,

def connect
  redirect Instagram.authorize_url(:redirect_uri => CALLBACK_URL)
end

CALLBACK_URL 在这里至关重要;这是您在 Instagram 网站上输入用户名和密码后,Instagram 将重定向您的链接。当 Instagram 访问您的 CALLBACK_URL 时,您可以执行以下操作来获取您在问题中提出的参数:

client = Instagram.client(:access_token => session[:access_token])
user = client.user
username = user.username

您可以通过here了解其他信息。

【讨论】:

  • 我在 .rb 文件中究竟应该在哪里输入链接?
  • 您说的是哪个 .rb 文件?您应该在路由文件和控制器文件中输入代码!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多