【发布时间】: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