简体   繁体   English

Ruby的Net :: HTTP无法从Google OAuth服务器获得正确答案

[英]Ruby's Net::HTTP doesn't get the right answer from google OAuth server

I'm writing a small cli tool, that should check my calendar and do some stuff according to my appointments. 我正在编写一个小的cli工具,该工具应检查我的日历并根据我的约会做一些事情。
I'm struggling a little bit with the OAuth2 authentication. 我在OAuth2身份验证方面有些挣扎。 I've checked the scope and the client_id with the curl tool like this: 我已经使用curl工具检查了范围和client_id,如下所示:

curl -d "client_id=12345...&scope=scope=https://www.googleapis.com/auth/calendar.readonly" https://accounts.google.com/o/oauth2/device/code

This way, I get the right response. 这样,我得到正确的答复。

{
 "device_code" : "somestuff",
 "user_code" : "otherstuff",
 "verification_url" : "http://www.google.com/device",
 "expires_in" : 1800,
 "interval" : 5
}

But, when I try to use Net::HTTP in Ruby I just get HTTP state 200. I've done it this way: 但是,当我尝试在Ruby中使用Net :: HTTP时,我的HTTP状态只有200。我这样做是这样的:

res = Net::HTTP.post_form(uri, {'client_id' =>'1234....apps.googleusercontent.com', 'scope' => 'https://www.googleapis.com/auth/calendar.readonly' })

If I check the res variable afterwards I get the state 302, but I guess this is correct. 如果之后再检查res变量,则会得到状态302,但我猜这是正确的。
Can someone tell me what I'm, doing wrong so I don't get the JSON response? 有人可以告诉我我做错了什么,所以我没有得到JSON响应吗? Should I try something different than Net::HTTP? 我应该尝试使用不同于Net :: HTTP的方法吗?

res is a variable containing all the response data, not just the text of the response. res是一个变量,其中包含所有响应数据,而不仅仅是响应文本。 If you puts res.body after your post_form() call, you should find your JSON (which you can parse with the JSON module). 如果puts res.body放在post_form()调用之后,则应该找到JSON(可以使用JSON模块进行解析)。

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

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