如何使用Mechanize单击Magento授权应用程序页面上的“授权”按钮

时间:2014-08-19 11:54:05

标签: ruby oauth mechanize

我试图点击按钮来授权访问权限,但它似乎无法正常工作。它可能是JavaScript和Mechanize问题吗?页面的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时,它只会刷新当前页面。当我访问授权URL时,它仍然显示授权按钮。如果成功授权,则不应显示该按钮。

这就是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>

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

如何点击Authorize按钮?

2 个答案:

答案 0 :(得分:0)

有时<button>个元素会在表单中提交JavaScript事件。尝试告诉表单提交自己:

form.submit

答案 1 :(得分:0)

我无法点击&#34;授权&#34;按钮,因为页面上没有。在授权应用程序页面可见之前,我首先需要使用我的管理员凭据登录。

这是我用来成功点击&#34;授权&#34;的代码。 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