1

Instagram API を使用してレール バックグラウンド ワーカーを作成し、ハッシュタグをクエリしようとしています。私は他のユーザーにログインする必要はありませんが、私自身はブラウザーを使用する必要はなく、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 gem を使用するresponse = 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 に答える