通过在ruby中打开浏览器获得响应?

时间:2015-03-11 11:30:44

标签: ruby-on-rails ruby oauth-2.0 onedrive

我正在使用ruby -v 2.1.5。

我正在尝试使用onedrive API在我的网络应用中授权我的用户(oAuth2)。

我按照https://github.com/ronyv89/skydrive/blob/master/README.md

中指定的步骤操作

我通过使用以下步骤获得了auth_url,

require 'rubygems'
require 'skydrive'

class LivePartnerApp 

client_id = "MY_CLIENT_ID"
client_secret = "MY_CLIENT_SECRET"
call_back_url = "http://sample.samplewebsite.com/"


oauth_client = Skydrive::Oauth::Client.new(client_id, client_secret, call_back_url, "wl.skydrive_update,wl.offline_access")
puts "oauth_client --- #{oauth_client.inspect}"
auth_url = oauth_client.authorize_url
puts "auth_url - #{auth_url.inspect}"

response = system("open", "#{auth_url}")

puts "response #{response.inspect}"
end

当我尝试运行此文件时。我对oauth_client和auth_url得到了适当的回应。

 response = system("open", "#{auth_url}")

正确打开浏览器并重定向到我的redirect_url。

但我的问题是,   在此重定向步骤之后,我必然会得到一个代码,我应该使用访问令牌

但上面的响应变量只返回 true false ,具体取决于重定向是否成功。

有人可以建议一种方法以编程方式在ruby中获取此代码,以便我可以使用它来获取访问令牌吗?

提前致谢!

1 个答案:

答案 0 :(得分:1)

这是因为您使用system调用命令行(即shell):system不返回命令行结果,它只返回true或false。看看

http://tech.natemurray.com/2007/03/ruby-shell-commands.html

我通常使用反引号,例如

response = `open #{auth_url}`