gpt4 book ai didi

ruby - Microsoft Live 授权 GET 请求返回 Net::HTTPBadRequest 400

转载 作者:数据小太阳 更新时间:2023-10-29 07:49:42 24 4
gpt4 key购买 nike

我正在关注 Microsoft live connect API documentation授权我的用户访问 onedrive。我正在尝试建立代码流身份验证。我按照说明获得了 AUTHORIZATION_CODE。现在,我试图借助它来获取 ACCESS_TOKEN:

Microsoft live connect API documentation ,据说要获取 ACCESS_TOKEN 我们需要提供一个请求,例如,

POST https://login.live.com/oauth20_token.srf

Content-type: application/x-www-form-urlencoded

client_id=CLIENT_ID&redirect_uri=REDIRECT_URI&client_secret=CLIENT_SECRET&
code=AUTHORIZATION_CODE&grant_type=authorization_code

我使用 ruby​​ 提供了相同的请求,但出现错误:

#<Net::HTTPBadRequest 400 Bad Request readbody=true>

然后我在microsoft forum找到了,请求是 GET 而不是 POST。所以,我在 ruby​​ 中创建了一个 GET 请求,如下所示:

access_code =params["code"]
uri = URI.parse("https://login.live.com/oauth20_token.srf")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == 'https'
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.read_timeout = 500
req = Net::HTTP::Get.new("https://login.live.com/oauth20_token.srf",
initheader = {'Content-Type' =>'application/x-www-form-urlencoded'})
data = URI.encode_www_form({'client_id'=> 'my_client_id' ,
'redirect_uri' =>'my_redirect_url',
'client_secret' =>'my_client_secret',
'code'=>access_code, 'grant_type' =>'authorization_code'})
req.body = data
res = http.start { |http| http.request(req) }

当我运行它时,我遇到了同样的 HTTPBadRequest 400 错误。

注意:我已经检查了 CLIENT_ID,REDIRECT_URI,CLIENT_SECRET,AUTHORIZATION_CODE 的值,一切正常。

最佳答案

我很遗憾看到那个论坛来解决这个问题,浪费了我的时间。

实际上 POST 请求在这种情况下会很好,如他们的文档所示。

这就是我得到回复的方式,

uri = URI.parse("https://login.live.com/oauth20_token.srf")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
req = Net::HTTP::Post.new("https://login.live.com/oauth20_token.srf")
req.content_type = "application/x-www-form-urlencoded"
data = URI.encode_www_form({'client_id'=> 'my_client_id' , 'redirect_uri' =>'my_redirect_ui', 'client_secret' =>'my_client_secret', 'code'=>access_code, 'grant_type' =>'authorization_code'})
req.body = data
response = http.request(req)

关于ruby - Microsoft Live 授权 GET 请求返回 Net::HTTPBadRequest 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29490997/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com