Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 我如何点击;授权;Magento上的按钮';s使用Mechanize授权应用程序页面_Ruby_Oauth_Mechanize - Fatal编程技术网

Ruby 我如何点击;授权;Magento上的按钮';s使用Mechanize授权应用程序页面

Ruby 我如何点击;授权;Magento上的按钮';s使用Mechanize授权应用程序页面,ruby,oauth,mechanize,Ruby,Oauth,Mechanize,我试图点击一个按钮来授权访问,但它似乎不起作用。这可能是JavaScript和机械化的问题吗?页面的HTML代码说明需要启用JavaScript才能“利用此网站的功能” 以下是我尝试过的: require 'mechanize' m = Mechanize.new auth_page = m.get('http://www.narek.nl/ecommerce/admin/oauth_authorize?oauth_callback=http%3A%2F%2Fwww.narek.nl%2Fe

我试图点击一个按钮来授权访问,但它似乎不起作用。这可能是JavaScript和机械化的问题吗?页面的HTML代码说明需要启用JavaScript才能“利用此网站的功能”

以下是我尝试过的:

require 'mechanize'

m = Mechanize.new

auth_page = m.get('http://www.narek.nl/ecommerce/admin/oauth_authorize?oauth_callback=http%3A%2F%2Fwww.narek.nl%2Fecommerce&oauth_token=3bda9a0a5ed8debd87c926b8cb2f31f6')

# # =>
# #<Mechanize::Page
#  {url
#   #<URI::HTTP:0x007ff594791990 URL:http://www.narek.nl/ecommerce/admin/oauth_authorize?oauth_callback=http%3A%2F%2Fwww.narek.nl%2Fecommerce&oauth_token=3bda9a0a5ed8debd87c926b8cb2f31f6>}
#  {meta_refresh}
#  {title "Magento Administrator"}
#  {iframes}
#  {frames}
#  {links}
#  {forms
#   #<Mechanize::Form
#    {name nil}
#    {method "POST"}
#    {action "http://www.narek.nl/ecommerce/admin/oauth_authorize/index/"}
#    {fields
#     [hidden:0x3ffaca3a0ee4 type: hidden name: form_key value: lNSCf22s2HVdBuXz]
#     [text:0x3ffaca3a0d68 type: text name: login[username] value: ]
#     [field:0x3ffaca3a0b9c type: password name: login[password] value: ]
#     [hidden:0x3ffaca3a09d0 type: hidden name: oauth_token value: dcff49ccbd34ad5e8ce5f8ca4d67dc26]}
#    {radiobuttons}
#    {checkboxes}
#    {file_uploads}
#    {buttons [button:0x3ffaca39dcd0 type: submit name:  value: ] [button:0x3ffaca39dbf4 type:  name:  value: ]}>}>

form = auth_page.form_with(:action => 'http://www.narek.nl/ecommerce/index.php/admin/oauth_authorize/index/')

authorisation_button = form.buttons[0]

# => [button:0x3ffaca15089c type: submit name:  value: ]

authorise = m.submit(form, authorisation_button)

# # => #<Mechanize::Page
#  {url
#   #<URI::HTTP:0x007ff593028600 URL:http://www.narek.nl/ecommerce/admin/oauth_authorize?oauth_callback=http%3A%2F%2Fwww.narek.nl%2Fecommerce&oauth_token=3bda9a0a5ed8debd87c926b8cb2f31f6>}
#  {meta_refresh}
#  {title "Magento Administrator"}
#  {iframes}
#  {frames}
#  {links}
#  {forms
#   #<Mechanize::Form
#    {name nil}
#    {method "POST"}
#    {action "http://178.62.173.99/index.php/admin/oauth_authorize/index/"}
#    {fields
#     [hidden:0x3ffacb001774 type: hidden name: form_key value: pvWnuhmmADOfmvbk]
#     [text:0x3ffacb001620 type: text name: login[username] value: ]
#     [field:0x3ffacb0014cc type: password name: login[password] value: ]
#     [hidden:0x3ffacb001350 type: hidden name: oauth_token value: dcff49ccbd34ad5e8ce5f8ca4d67dc26]}
#    {radiobuttons}
#    {checkboxes}
#    {file_uploads}
#    {buttons [button:0x3ffacb000a7c type: submit name:  value: ] [button:0x3ffacb0009b4 type:  name:  value: ]}>}>
这是页面在浏览器中的外观:

如何单击
授权
按钮?

有时
元素会让JavaScript事件提交表单。尝试告诉表单提交自己:

form.submit
我无法单击“授权”按钮,因为页面上没有任何内容。我首先需要使用管理员凭据登录,然后才能看到授权应用程序页面

这是我用来成功单击Magento授权应用程序页面上的“授权”按钮的代码

require 'oauth'
require 'mechanize'

@m = Mechanize.new

@callback_url = "http://www.yourwebsite.nl/"
@consumer = OAuth::Consumer.new(
  "key",
  "secret",
  :request_token_path => "/oauth/initiate",
  :authorize_path=>"/admin/oauth_authorize",
  :access_token_path=>"/oauth/token",
  :site => "http://www.yourwebsite.nl"
)

@session = {}

@request_token = @consumer.get_request_token(:oauth_callback => @callback_url)
@session[:request_token] = @request_token
@session[:authorize_url] = @request_token.authorize_url(:oauth_callback => @callback_url)

@m.get(@session[:authorize_url]) do |login_page|
  auth_page = login_page.form_with(:action => 'http://www.yourwebsite.nl/index.php/admin/oauth_authorize/index/') do |form|
    form.elements[1].value = 'insert_admin_username_here'
    form.elements[2].value  = 'insert_admin_password_here'
  end.submit

  authorize_form = auth_page.forms[0]

  authorize_form.submit
end

非常感谢。

也不起作用。这可能是JavaScript和机械化的问题吗?该页面的HTML代码说明需要启用JavaScript才能“利用此网站的功能”。这是一条关键信息。Mechanize不执行JavaScript。请使用此信息更新您的问题。您需要在提交表单之前设置用户名/密码。无意冒犯,但这应该是显而易见的。你应该考虑雇人来帮你解决这个问题。@帕格迪亚里奥,我在这个问题上加了一页截图。没有要填写的表格。
require 'oauth'
require 'mechanize'

@m = Mechanize.new

@callback_url = "http://www.yourwebsite.nl/"
@consumer = OAuth::Consumer.new(
  "key",
  "secret",
  :request_token_path => "/oauth/initiate",
  :authorize_path=>"/admin/oauth_authorize",
  :access_token_path=>"/oauth/token",
  :site => "http://www.yourwebsite.nl"
)

@session = {}

@request_token = @consumer.get_request_token(:oauth_callback => @callback_url)
@session[:request_token] = @request_token
@session[:authorize_url] = @request_token.authorize_url(:oauth_callback => @callback_url)

@m.get(@session[:authorize_url]) do |login_page|
  auth_page = login_page.form_with(:action => 'http://www.yourwebsite.nl/index.php/admin/oauth_authorize/index/') do |form|
    form.elements[1].value = 'insert_admin_username_here'
    form.elements[2].value  = 'insert_admin_password_here'
  end.submit

  authorize_form = auth_page.forms[0]

  authorize_form.submit
end