【问题标题】:How do I click on the "Authorize" button on Magento's Authorize Application page with Mechanize如何使用 Mechanize 在 Magento 的授权申请页面上单击“授权”按钮
【发布时间】:2014-08-19 11:54:04
【问题描述】:

我正在尝试单击一个按钮来授权访问,但它似乎不起作用。会不会是 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: ]}>}>

当我尝试提交authorisation_button 时,它只会刷新当前页面。当我访问授权网址时,它仍然显示授权按钮。如果授权成功,按钮应该不会显示。

这就是 HTML 的样子:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Magento Admin</title>
<link rel="icon" href="http://www.narek.nl/ecommerce/skin/adminhtml/default/default/favicon.ico" type="image/x-icon"/>
<link rel="shortcut icon" href="http://www.narek.nl/ecommerce/skin/adminhtml/default/default/favicon.ico" type="image/x-icon"/>

<script type="text/javascript">
    var BLANK_URL = 'http://www.narek.nl/ecommerce/js/blank.html';
    var BLANK_IMG = 'http://www.narek.nl/ecommerce/js/spacer.gif';
    var BASE_URL = 'http://www.narek.nl/ecommerce/index.php/admin/index/index/key/491c19cb98a4e669b0f21b52089840a7/';
    var SKIN_URL = 'http://www.narek.nl/ecommerce/skin/adminhtml/default/default/';
    var FORM_KEY = 'GGvnhfOGCuVG25a2';
</script>

<link rel="stylesheet" type="text/css" href="http://www.narek.nl/ecommerce/skin/adminhtml/default/default/reset.css" media="all" />
<link rel="stylesheet" type="text/css" href="http://www.narek.nl/ecommerce/skin/adminhtml/default/default/boxes.css" media="all" />
<link rel="stylesheet" type="text/css" href="http://www.narek.nl/ecommerce/skin/adminhtml/default/default/custom.css" media="all" />
<link rel="stylesheet" type="text/css" href="http://www.narek.nl/ecommerce/skin/adminhtml/default/default/xmlconnect/boxes.css" media="all" />
<link rel="stylesheet" type="text/css" href="http://www.narek.nl/ecommerce/skin/adminhtml/default/default/print.css" media="print" />
<script type="text/javascript" src="http://www.narek.nl/ecommerce/js/prototype/prototype.js"></script>
<script type="text/javascript" src="http://www.narek.nl/ecommerce/js/scriptaculous/effects.js"></script>
<script type="text/javascript" src="http://www.narek.nl/ecommerce/js/prototype/validation.js"></script>
<script type="text/javascript" src="http://www.narek.nl/ecommerce/js/mage/translate.js"></script>
<script type="text/javascript" src="http://www.narek.nl/ecommerce/js/mage/adminhtml/form.js"></script>
<script type="text/javascript" src="http://www.narek.nl/ecommerce/js/mage/adminhtml/tools.js"></script>
<!--[if lt IE 8]>
<link rel="stylesheet" type="text/css" href="http://www.narek.nl/ecommerce/skin/adminhtml/default/default/iestyles.css" media="all" />
<![endif]-->
<!--[if lt IE 7]>
<link rel="stylesheet" type="text/css" href="http://www.narek.nl/ecommerce/skin/adminhtml/default/default/below_ie7.css" media="all" />
<![endif]-->
<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="http://www.narek.nl/ecommerce/skin/adminhtml/default/default/ie7.css" media="all" />
<![endif]-->
<!--[if lte IE 8]>
<link rel="stylesheet" type="text/css" href="http://www.narek.nl/ecommerce/skin/adminhtml/default/default/xmlconnect/iestyles.css" media="all" />
<![endif]-->

<script type="text/javascript">
    Fieldset.addToPrefix(1);
</script>

<script type="text/javascript">//<![CDATA[
        var Translator = new Translate([]);
        //]]></script></head>
<body class="page-popup adminhtml-oauth-authorize-index">
<div>
            <noscript>
        <div class="noscript">
            <div class="noscript-inner">
                <p><strong>JavaScript seems to be disabled in your browser.</strong></p>
                <p>You must have JavaScript enabled in your browser to utilize the functionality of this website.</p>
            </div>
        </div>
    </noscript>
    <div class="login-container auth-confirm">
    <div class="login-box">
        <div class="login-form auth-confirm">
            <div class="page-title">
                <h1>Authorize application</h1>
            </div>

                            <h2><strong>Admin</strong> requests access to your account</h2>
                <p>After authorization application will have access to you account.</p>
                <form id="oauth_authorize_confirm" action="http://www.narek.nl/ecommerce/index.php/admin/oauth_authorize/confirm/" method="get">
                    <input type="hidden" name="oauth_token" value="3bda9a0a5ed8debd87c926b8cb2f31f6">
                    <button type="submit" class="button" title="Authorize"><span><span>Authorize</span></span></button>
                </form>
                <form id="oauth_authorize_reject" action="http://www.narek.nl/ecommerce/index.php/admin/oauth_authorize/reject/" method="get">
                    <input type="hidden" name="oauth_token" value="3bda9a0a5ed8debd87c926b8cb2f31f6">
                    <button type="submit" class="button" title="Reject"><span><span>Reject</span></span></button>
                </form>
                    </div>
        <p class="legal">Magento is a trademark of Magento Inc. Copyright &copy; 2014 Magento Inc.</p>
        <div class="bottom"></div>
    </div>
</div>
    </div>
</body>
</html>

这是页面在浏览器中的样子:

如何点击Authorize 按钮?

【问题讨论】:

  • 提交表单前需要设置用户名/密码。没有冒犯,但这应该是显而易见的。您应该考虑雇用某人来帮助您。
  • @pguardiario 我已将页面的屏幕截图添加到问题中。没有要填写的表格。

标签: ruby oauth mechanize


【解决方案1】:

有时&lt;button&gt; 元素有 JavaScript 事件提交表单。尝试告诉表单自己提交:

form.submit

【讨论】:

  • 也不起作用。会不会是 JavaScript 和机械化问题?该页面的 HTML 代码声明需要启用 JavaScript 才能“利用本网站的功能”。
  • 这是一条关键信息。 Mechanize 不执行 JavaScript。请使用此信息更新您的问题。
【解决方案2】:

我无法点击“授权”按钮,因为页面上没有。在看到授权应用程序页面之前,我首先需要使用我的管理员凭据登录。

这是我用来成功单击 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

非常感谢pguardiario

【讨论】:

    猜你喜欢
    • 2021-06-05
    • 1970-01-01
    • 2012-06-01
    • 1970-01-01
    • 2011-12-07
    • 1970-01-01
    • 2021-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多