Ruby on rails 使用Ruby在OpenID中检索XRDS文档
我只是在玩OpenID协议。我正在尝试发送发现请求并从google检索XRDS文档。当我尝试使用curl从终端执行此操作时,我得到以下输出Ruby on rails 使用Ruby在OpenID中检索XRDS文档,ruby-on-rails,ruby,openid,google-openid,xrds,Ruby On Rails,Ruby,Openid,Google Openid,Xrds,我只是在玩OpenID协议。我正在尝试发送发现请求并从google检索XRDS文档。当我尝试使用curl从终端执行此操作时,我得到以下输出 curl --url "https://www.google.com/accounts/o8/id" <?xml version="1.0" encoding="UTF-8"?> <xrds:XRDS xmlns:xrds="xri://$xrds" xmlns="xri://$xrd*($v*2.0)">
curl --url "https://www.google.com/accounts/o8/id"
<?xml version="1.0" encoding="UTF-8"?>
<xrds:XRDS xmlns:xrds="xri://$xrds" xmlns="xri://$xrd*($v*2.0)">
<XRD>
<Service priority="0">
<Type>http://specs.openid.net/auth/2.0/server</Type>
<Type>http://openid.net/srv/ax/1.0</Type>
<Type>http://specs.openid.net/extensions/ui/1.0/mode/popup</Type>
<Type>http://specs.openid.net/extensions/ui/1.0/icon</Type>
<Type>http://specs.openid.net/extensions/pape/1.0</Type>
<URI>https://www.google.com/accounts/o8/ud</URI>
</Service>
</XRD>
</xrds:XRDS>
如何通过代码获取XRD,以及为什么它显示不同的输出。有人能解释一下吗?谢谢谷歌希望使用https协议,尽管在ruby示例中使用http,因此出现了302错误。下面的代码片段将为您提供xrds文档:
require 'net/http'
require 'net/https'
require 'uri'
uri = URI.parse('https://www.google.com/accounts/o8/id')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
puts "#{response.read_body}"
如您所见,当您从ruby获取文档时,它返回302状态代码,这意味着您应该查找
location
头并遵循它,就像curl一样
另一个答案建议只对有效的url进行硬编码,但这不是一个正确的解决方案,因为谷歌也可以让它返回302并将文档移动到其他地方
更不用说,您应该执行完整的发现,而不是希望从url获得XRDS文档(因为,例如,Google可能会决定它是解释OpenID的好位置,并使用X-XRDS-location
header将XRD移动到其他地方)
require 'net/http'
require 'net/https'
require 'uri'
http = Net::HTTP.new(uri.host, uri.port)
response = Net::HTTP.get_response(URI.parse("http://www.google.com/accounts/o8/id"))
puts "#{response.read_body}"
require 'net/http'
require 'net/https'
require 'uri'
uri = URI.parse('https://www.google.com/accounts/o8/id')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
puts "#{response.read_body}"