Cannot Login to Amazon with Ruby Mechanize

Login to amazon partnernet using Ruby Mechanize

So.. I debugged the code, this version is working now as expected:

desc "Cron Task for Email Notifications"
task :email_amazon_stats => :environment do
puts "Start: Fetch and send Amazon Sales from yesterday (#{Time.now})"

agent = Mechanize.new
agent.cookie_jar.clear!
agent.user_agent_alias = 'Mac Firefox'
agent.follow_meta_refresh = true
agent.redirect_ok = true

dashboard_url = "https://partnernet.amazon.de/gp/associates/network/reports/report.html?__mk_de_DE=%C3%85M%C3%85%C5%BD%C3%95%C3%91&tag=&reportType=earningsReport&program=all&deviceType=all&periodType=preSelected&preSelectedPeriod=yesterday&startDay=1&startMonth=11&startYear=2016&endDay=2&endMonth=11&endYear=2016&submit.display.x=87&submit.display.y=16&submit.display=Auf+der+Seite+anzeigen"

agent.get(dashboard_url)

form = agent.page.form_with(:name => 'sign_in')
form.username = ENV['AZON_PARTNER_USR']
form.password = ENV['AZON_PARTNER_KEY']
form.submit

dashboard = agent.get(dashboard_url)
form2 = dashboard.form_with(:name => 'htmlReport')
button = form2.button_with(:name => 'submit.download_XML')
xml = agent.submit(form2, button)

doc = Nokogiri::XML(xml.body)
@sales = []
doc.xpath("//Item").each do |item|
@sales << {
:sale_itemasin => item['ASIN'],
:sale_itemname => item['Title'].truncate(80),
:sale_date => Time.at(item['EDate'].to_i).strftime("%Y-%m-%d %H:%M:%S").to_s,
:sale_amount => '%.2f' % item['Revenue'].gsub(',','.').to_f,
:sale_commission => '%.2f' % item['Earnings'].gsub(',','.').to_f
}
end

earnings = 0
@sales.each do |s|
earnings += s[:sale_commission].to_f
end

@total_commission = '%.2f' % earnings

ReportsMailer.daily_dashboard(@total_commission,@sales).deliver
puts "Done: Fetch and send Amazon Sales from yesterday (#{Time.now})"
end

As you can see yourself this is pretty ugly, because I try to go to the deeplink directly, which redirects me to the login page. There I login and try again to go to the dashboard. This time it works. Why ugly? Because if I try to go to the login page directly the code does not work, I somehow need this redirect. Any idea why? Would be interesting to understand this...

Unable to submit Amazon form via Mechanize (Ruby)

Figured it out. I was setting the name with the == rather than = which was causing the field to be empty

Ruby Mechanize Login form submit error

Here's what the browser sends:

  • currentTime: MTQ5MTg3OTYzNDAwMA==
  • userId: foo
  • password: 37b51d194a7513e45b56f6524f2d51f2
  • password1: bar

The password looks like a md5 and the currentTime is a base64 of a timestamp (1491879634000 in this case).

Remote Login to Amazon

with Selenium see http://www.seleniumhq.org/
the login was possible in a straightforward way. See sample code below

Please note: AmazonUser is just a helper class to hold the elements needed for login:

  • email
  • password

Example usage

Amazon amazon = new Amazon();
String html = amazon
.navigate("/gp/css/order-history/ref=oh_aui_menu_open?ie=UTF8&orderFilter=open");

Amazon helper class

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

/**
* Amazon handling
* @author wf
*
*/
public class Amazon {
FirefoxDriver driver = new FirefoxDriver();
String root="https://www.amazon.de";

/**
* signIn
* @throws Exception
*/
public void signIn() throws Exception {
// <input type="email" autocapitalize="off" autocorrect="off" tabindex="1" maxlength="128" size="30" value="" name="email" id="ap_email" />
WebElement emailField=driver.findElement(By.id("ap_email"));
// get user and password
AmazonUser auser=AmazonUser.getUser();
emailField.sendKeys(auser.getEmail());
// <input type="password" class="password" onkeypress="displayCapsWarning(event,'ap_caps_warning', this);" tabindex="2" size="20" maxlength="1024" name="password" id="ap_password" />
WebElement passwordField=driver.findElement(By.id("ap_password"));
passwordField.sendKeys(auser.getPassword());
// signInSubmit-input
WebElement signinButton=driver.findElement(By.id("signInSubmit-input"));
signinButton.click();
}

/**
* navigate to the given path
* @param path
* @return
* @throws Exception
*/
public String navigate(String path) throws Exception {
driver.get(root+path);
String html=driver.getPageSource();
if (html.contains("ap_signin_form")) {
signIn();
}
html=driver.getPageSource();
return html;
}

public void close() {
driver.close();
driver.quit();
}


}


Related Topics



Leave a reply



Submit