- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在关注 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/
我正在关注 Microsoft live connect API documentation授权我的用户访问 onedrive。我正在尝试建立代码流身份验证。我按照说明获得了 AUTHORIZATIO
我的 omniauth 应用程序有奇怪的行为。基本上,我有一个管理面板,需要访问它才能使用 Yandex 帐户进行身份验证。 问题:我按照多个指南中的要求进行了所有操作,自昨天以来一切正常,我尝试使用
我是一名优秀的程序员,十分优秀!