【问题标题】:Instagram API authentification Rails 2017 request the access_tokenInstagram API 身份验证 Rails 2017 请求 access_token
【发布时间】:2017-05-18 17:46:56
【问题描述】:

如何从网址下载数据而不被重定向到该网址?

我已经设法从使用以下代码的 instagram API 获得身份验证:

  1. 用户点击按钮并在instagram登录页面登录。
  2. 用户被重定向到我的网址
  3. 由于 url 中的 Code 参数,触发了一些 javascript 将带有数据的表单提交到 api
  4. 我被重定向到带有 json 数据的 instagram api

这会将我重定向到带有 Json 数据的 instagram api,但我不确定如何使用/下载信息。

我确信这不是最好的方法,但我已经尝试了很多事情,这是我让它发挥作用的唯一方法。

 <a href=<%="https://api.instagram.com/oauth/authorize/?client_id=#{ENV['CLIENT_ID']}&redirect_uri=http://localhost:3000/edit&response_type=code"%>>Link my instagram account</a>
            <% if params[:code] %>
            <form id="target" method="post" action="https://api.instagram.com/oauth/access_token">
              <div>
                <input type="text" name="client_id" value=<%= ENV['CLIENT_ID']%> hidden="true">
                <input type="text" name="client_secret" value=<%= ENV['CLIENT_SECRET']%> hidden="true">
                <input type="text" name="grant_type" value="authorization_code" hidden="true">
                <input type="text" name="redirect_uri" value="http://localhost:3000/edit" hidden="true">
                <input type="text" name="code" value=<%= params[:code]%> hidden="true" >
              </div>
            </form>
            <% end %>

【问题讨论】:

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


    【解决方案1】:

    我确实可以使用以下代码:

    所以首先你需要这个请求来获取第一个代码:

    https://api.instagram.com/oauth/authorize/?client_id=#{ENV['CLIENT_ID']}&redirect_uri=http://localhost:3000&response_type=code
    

    然后使用以下post请求中的代码获取访问令牌和数据:

    result = HTTParty.post("https://api.instagram.com/oauth/access_token",
      {
        :body =>  { "client_id" => ENV['CLIENT_ID'], "client_secret" => ENV['CLIENT_SECRET'],
        "grant_type" => "authorization_code",
        "redirect_uri" => 'http://localhost:3000', "code" => session[:code] },
        :headers => { 'Content-Type' => 'application/x-www-form-urlencoded', 'Accept' => 'application/json'}
      })
    

    为了找到用户ID,只需添加以下内容:

    result.parsed_response["user"]["id"]
    

    希望这会对某人有所帮助。我花了一段时间才了解这些请求的实际工作原理。

    【讨论】:

    • 嗨,如何处理:{"error_type": "OAuthForbiddenException", "code": 403, "error_message": "You are not a sandbox user of this client"}我想用ajax调用来展示它。这可能吗?
    猜你喜欢
    • 2012-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-30
    • 1970-01-01
    • 2015-03-26
    相关资源
    最近更新 更多