1

我正在尝试使用 Instagram API 创建一个 Rails 后台工作人员来查询主题标签。我不需要登录任何其他用户,但我自己也不想使用任何浏览器,只需 RESTful 调用。

我正在尝试使用 gem“rest-client”(https://github.com/rest-client/rest-client)在 Ruby 脚本中自动获取我的访问令牌

我可以在浏览器中成功导航到以下 url 并从响应 url 中获取访问令牌

我已经使用了这两个 URL: https ://www.instagram.com/oauth/authorize/?client_id=xxx&redirect_uri=xxx&response_type=token

但是当我使用 RESTful gemresponse = RestClient.get(url)时,它 response.headers['location']是 nil

我也尝试过使用 Instagram API URL,但没有运气:https ://api.instagram.com/oauth/authorize/?client_id=xxx&redirect_uri=xxx&response_type=code

任何人都知道如何在 Ruby 中完全以编程方式获取访问令牌?

我想我错过了登录用户(这将是我)的步骤。不确定如何以编程方式执行此操作。

我可以将 instagram API 与永不更改的代码或访问令牌一起使用吗?

4

1 回答 1

0

rest-client 自动请求重定向主机。

https://github.com/rest-client/rest-client/blob/master/lib/restclient/abstract_response.rb

我从未使用过 instagram API,但在 302 上获取“位置”的代码如下所示。

# client
require 'net/http'
require 'uri'
url = URI.parse('http://localhost:2000')
res = Net::HTTP.start(url.host, url.port) do |http|
  http.get('/')
end
puts "#{res.header['Location']} is https://google.com"
# test server
require 'socket'
server = TCPServer.new 2000
loop do
  socket = server.accept
  while header = socket.gets
    break if header.chomp.empty?
    puts header.chomp
  end
  socket.puts "HTTP/1.0 302"
  socket.puts "Location: https://google.com"
  socket.close
end

我希望这些信息对您有所帮助。

于 2019-01-27T00:53:01.330 回答