7

I am attempting to login to Amazon using the Ruby gem Mechanize. I always get kicked back to the sign in page without any sort of error message. I am wondering if this is a bug with Mechanize or if Amazon blocks this sort of access. I have code below that you can irb to test.

@mechanizer = Mechanize.new

@mechanizer.user_agent_alias = 'Mac Safari'

@page = @mechanizer.get("https://www.amazon.com/ap/signin?_encoding=UTF8&openid.assoc_handle=usflex&openid.return_to=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fyourstore%3Fie%3DUTF8%26ref_%3Dpd_irl_gw&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.pape.max_auth_age=0&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select")

form = @page.form_with(:id => "ap_signin_form")

field = form.field_with(:name => "email")
field.value = "fake@email.com"

radiobutton = form.radiobutton_with(:name => 'create', :value => '0')
radiobutton.check

button = form.button_with(:id => "signInSubmit")

@page = form.submit button
starball
  • 20,030
  • 7
  • 43
  • 238
batch
  • 93
  • 1
  • 6

1 Answers1

8

Try this,

#!/usr/bin/env ruby

require "rubygems"
require "mechanize"

class AmazonCrawler
  def initialize
    @agent = Mechanize.new do |agent|
      agent.user_agent_alias = 'Mac Safari'
      agent.follow_meta_refresh = true
      agent.redirect_ok = true
    end
  end

  def login
    login_url = "https://www.amazon.com/ap/signin?_encoding=UTF8&openid.assoc_handle=usflex&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fyourstore%2Fhome%3Fie%3DUTF8%26ref_%3Dgno_signin"
    @agent.get(login_url)
    form = @agent.page.forms.first
    form.email = "user@example.com"
    form['ap_signin_existing_radio'] = "1"
    form.password = "password"
    dashboard = @agent.submit(form)
    File.open('dashboard.html', 'w') {|file| file << dashboard.body }
  end
end

AmazonCrawler.new.login

The mechanize documentation has some cool examples. This cheat sheet is also handy for quick references.

Kibet Yegon
  • 2,763
  • 2
  • 25
  • 32
  • I'm trying to do something similar but am not having much success. The code as written above will still redirect you back to the login page. Any ideas? – joelpoloney Jul 31 '13 at 01:08
  • I'd like to know as well – Zack Shapiro Oct 17 '13 at 05:03
  • @fffanatics, @Zack I've updated the code to print out the page to file. Make sure you change the values for `form.email` and `form.password` to match your Amazon user credentials. Its still working for me. – Kibet Yegon Oct 18 '13 at 14:00
  • So that definitely works for amazon.com. I was trying to login to developer.amazon.com rather than the main website. I wasn't able to get it working through Mechanize, but was able to get it working through the Watir and Headless gems – joelpoloney Oct 18 '13 at 17:36