简体   繁体   English

如何将CURL命令转换为对OAuth令牌的Rails POST请求?

[英]How to translate a CURL command into a Rails POST request for an OAuth token?

I am a web-development student and am trying to work with the Instagram API . 我是一名网络开发学生,正在尝试使用Instagram API I have never had to deal with OAuth before. 我以前从未处理过OAuth。

So far I can get the code from the user authorizing me to use their Instagram credentials but the POST request to get the actual token eludes me. 到目前为止,我可以从授权我使用他们的Instagram凭据的用户那里获取代码,但是获取实际令牌的POST请求使我难以理解。 I am trying to use Typhoeus for server side API requests but where I get hung up is in translating curl commands into usable rails controller script. 我正在尝试使用Typhoeus来处理服务器端API请求,但是我挂断的是将curl命令转换为可用的rails控制器脚本。

The Instagram API recommends the following curl command: Instagram API建议使用以下curl命令:

curl \-F 'client_id=CLIENT-ID' \
-F 'client_secret=CLIENT-SECRET' \
-F 'grant_type=authorization_code' \
-F 'redirect_uri=YOUR-REDIRECT-URI' \
-F 'code=CODE' \https://api.instagram.com/oauth/access_token

and here is my attempt at a Typhoeus POST request: 这是我对Typhoeus POST请求的尝试:

request = Typhoeus::Request.new(
  "https://api.instagram.com/oauth/access_token",
  method: :post,
  :body => {:client_id => CLIENT_ID,
              :client_secret => CLIENT_SECRET, 
              :grant_type => "authorization_code", 
              :redirect_uri => CALLBACK_URL, 
              :code => code}
  )
@results = JSON.parse((request.run).body)

And I get the following error when I try and log @results: 当我尝试记录@results时,出现以下错误:

{"code"=>400, "error_type"=>"OAuthException", "error_message"=>"You must provide a client_id"}

I have all the info that the API needs, I just don't know how to arrange it correctly... And yes, all of the constants are defined. 我掌握了API需要的所有信息,但我只是不知道如何正确安排它...是的,所有常量都已定义。 Any help appreciated! 任何帮助表示赞赏!

I recommend using the instagram-ruby-gem to make requests to the Instagram API. 我建议使用instagram-ruby-gem向Instagram API发送请求。 You'll find it much easier than using Typhoeus. 您会发现它比使用Typhoeus容易得多。

Somehow I solved my own question. 我以某种方式解决了自己的问题。 The above Typhoeus script works just fine, with lots of JSON love for everyone. 上面的Typhoeus脚本工作得很好,每个人都有很多JSON喜欢。

I think I might have been having obscure session issues with my rails server when @results kept coming back as nil. 我想当@results一直返回nil时,我的rails服务器可能遇到了晦涩的会话问题。

I wish I had a more specific answer as to why this wasn't working earlier, but I'll keep my nose to the ground as I move forward and hopefully have a more satisfying conclusion. 我希望对于为什么这个方法不能更早解决有一个更具体的答案,但是随着我的前进,我会坚持到底,并希望得出一个更令人满意的结论。

I had the same problem in powershell finally, I put the client_id and code in the query string also. 最后,我在Powershell中遇到了同样的问题,我也将client_id和代码放入查询字符串中。 then it works. 然后就可以了。 I mean something like this: 我的意思是这样的:

" https://api.instagram.com/oauth/access_token?client_id=CLIENT_ID&code=code " https://api.instagram.com/oauth/access_token?client_id=CLIENT_ID&code=code

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM