我试图使用Google Contacts API将Google通讯录拉入Rails应用程序.我已经完成了Oauth2握手,现在我使用我的访问令牌来请求受保护的资源.这是代码:
uri = URI('https://www.google.com/m8/Feeds/contacts/default/full') params = { :client_id => APP_CONFIG[:google_api_client_id],:access_token => auth.access_token,"max-results".to_sym => max_results } uri.query = URI.encode_www_form(params) res = Net::HTTP.get_response(uri)
解决方法
您正在请求HTTPS资源,因此您的GET请求需要使用SSL加密.
http://ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTP.html#method-i-use_ssl-3F
所以你的最后一行应该是:
http = Net::HTTP.new(uri.host,uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE # You should use VERIFY_PEER in production request = Net::HTTP::Get.new(uri.request_uri) res = http.request(request)