【问题标题】:Microsoft Live authorization GET request returns Net::HTTPBadRequest 400Microsoft Live 授权 GET 请求返回 Net::HTTPBadRequest 400
【发布时间】:2015-04-07 11:55:42
【问题描述】:

我正在关注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 的值,一切都很完美。

【问题讨论】:

    标签: ruby post get http-error


    【解决方案1】:

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

    实际上 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多