I have a Mechanize script that currently goes to a login form and properly logs a user in. I'm seeing plenty of documentation to follow links, but I'd like to go to an ad-hoc page that isn't linked on the main page after I login. The page requires authentication and that's why I force the login first. Is there a way to change to another URL (that's still part of the same site) with Ruby's Mechanize gem and have it retain all of the cookies from the login? I looked up methods such as link_with but that's to follow a link on the current page. I'd like to go to a different url within the same website.
I believe you just need to make a subsequent get call after your initial transaction is complete.
client = Mechanize.new
client.get('http://example.com/login') do
# handle login
end
client.get('http://example.com/something-else') do
# another action
end
Related
For an app I'm, building I need to be able to determine a Facebook user's relation to the page of which the app is being shown within. I hope to provide the following functionality:
1) If the user likes the page, direct them to another page
2) If the user doesn't like a page, direct them to another page
3) If the user is an admin of the current page, direct them to another page
The Auth Hash schema (https://github.com/intridea/omniauth/wiki/Auth-Hash-Schema) doesn't provide info of how to access user likes, or indicate a way whether they: a) Like or don't like the current page, or B) are an admin of the current page.
Furthermore I've researched around the internet but cannot find any specific Ruby or Sinatra example of how to do this. The closest that I've come to is (https://github.com/chrissloan/sinatra-book/blob/master/app.rb), however this users FBGraph (I'm using Omniauth-Facebook) and this script doesn't make any distinction between admin users of the page.
Therefore I'm wondering if my method of distinguishing between users on the page tab is inherently wrong and am wondering if there is another process of achieving the desired goals.
So in summary, I'm attempting to create:
A backend that is accessible by the app admin. When an app admin goes on the page, the admin panel is displayed.
A front end that displays whether the user has liked the page or not, and shows them specific content based upon this state - a 'like-gate'.
Thanks for reading and if you could help it would be very much appreciated.
Some code I've thought up, but from the docs I'm unsure of whether it is valid syntax or not. From begin is the experimental piece, the code above works and is currently being used.
get '/auth/:provider/callback' do
content_type 'application/json'
JSON.generate(request.env)
auth = request.env["omniauth.auth"]
puts auth
=begin
if auth['page']['admin'] == true ***not sure if admin is valid syntax***
#check user database and move to admin side
else if auth['page']['liked'] == true
#allow to download endpoint
else
auth['page']['liked'] = false
#direct to wall to like
end
I solved this by authorising with OmniAuth and then getting the signed_request generate from Facebook when the user opens the app on a Facebook page.
I can't maintain session surfing through a website once logged in.
I do can successfully login on the site (i specify that whatever the page is, after the login you will be redirected to the homepage) but then I have to move to another page. First I tried with page.open() then with page.evaluate changing the location.href window property, but in both cases unfortunately the result is that I'm not logged in anymore. I traced the login status just rendering the page on every page load event with incremental png names (1.png, 2.png, etc) . I also tried with --cookies-file=cookies.txt param but it didn't help much.
My questions are:
What is the best way to "move" through site pages with phantomjs?
Is there a specific way to handle sessions in these cases (maybe sending cookies manually on each .open(), just saying)?
Thanks for help.
Sessions require cookies. You have to add an extra argument in phantomjs.
--cookies-file=/path/to/cookies.txt
Look here for more info.
Edit :
Does your cookies.txt contains something ?
I had issues with the cookies file approach. The file would be written, and I could see "cookie information" in the file, but future requests would be interpreted as unauthenticated. As a last ditch effort, I simply took the page.cookies array, serialized it to JSON and saved it to a file. My next script would open the file, deserialize it to a variable and set the page.cookies to the variable. Sure enough this worked! Just thought I would pass it along.
If you carefully notice facebook, you will understand that the when you visit one page to another then the whole page is not refreshed or redirected but the URL changes when navigating to new page. It is more clear when chat windows are open, they remains static during page loading. As other website does this by using the # but facebook does not.
There is another example came to me: http://www.davidwalsh.name/. Visit the site and open other pages within their site and you will understand what I mean. Don't forget to notice the URL change.
How they do this?
Added More: I want a way that the page content will be loaded with ajax, change the URL for bookmark feature but when changing URL it should not reload / refresh the page by not using # (hash).
You have two questions:
For the URL change you can put the path instead of the # in the href property of the anchor (e.g /otherlink).
For opening the chat or opening some div does not require to send it in the url, its the onclick event on the div and its expands. Also the chat remains open may be thay set cookie or flag when the chat is first time opened and checking and changing the chat with flag they are setting.
As suggested by #andytuba
For Facebook, Google "hash navigation ajax".
For DavidWalsh, google "history API".
I'm trying to dig around into an idea where we want to close a webiste for upgrades. So when a user hits our site, they are promptly re-directed to a page that tells them that the site is down for maintenance.
Theoretically, the entire process starts in the global.asax under the event session_start. We fire a command to the webservice to see if the service is down - if it is down, a false value is returned and the user is rerouted to a page that says the site is down otherwise normal operations continue. This is a non-password site.
Here's the idea with pages:
welcome.aspx
siteisdown.aspx
page1.aspx
page2.aspx
When the user visits the site, the code checks to see if the rest of the site is down for maintenance and if so, then the user is re-routed to siteisdown.aspx
We have that basic part down - but the real understanding is, the user - while still in the siteisdown.aspx page, could enter page1.aspx and continue on their merry way.
What we want to is is if the user enters page1.aspx they should be kicked back to siteisdown.aspx
What articles should I be looking at for this type of method? Forms Authentication?
There is already a feature for this built in. Deploy a static HTML file called "App_Offline.htm" to the root of your application and all requests will be routed there.
http://weblogs.asp.net/scottgu/archive/2005/10/06/426755.aspx
Alright, so here's the dealio: I'm working on a Ruby app that'll take data from a website, and aggregate that data into an XML file.
The website I need to take data from does not have any APIs I can make use of, so the only thing I can think of is to login to the website, sequentially load the pages that have the data I need (in this case, PMs; I want to archive them), and then parse the returned HTML.
The problem, though, is that I don't know of any ways to programatically simulate a login session.
Would anyone have any advice, or know of any proven methods that I could use to successfully login to an https page, and then programatically load pages from the site using a temporary cookie session from the login? It doesn't have to be a Ruby-only solution -- I just wanna know how I can actually do this. And if it helps, the website in question is one that uses Microsoft's .NET Passport service as its login/session mechanism.
Any input on the matter is welcome. Thanks.
Mechanize
Mechanize is ruby library which imititates the behaviour of a web browser. You can click links, fill out forms und submit them. It even has a history and remebers cookies. It seems your problem could be easily solved with the help of mechanize.
The following example is taken from http://docs.seattlerb.org/mechanize/EXAMPLES_rdoc.html:
require 'rubygems'
require 'mechanize'
a = Mechanize.new
a.get('http://rubyforge.org/') do |page|
# Click the login link
login_page = a.click(page.link_with(:text => /Log In/))
# Submit the login form
my_page = login_page.form_with(:action => '/account/login.php') do |f|
f.form_loginname = ARGV[0]
f.form_pw = ARGV[1]
end.click_button
my_page.links.each do |link|
text = link.text.strip
next unless text.length > 0
puts text
end
end
You can try use wget to fetch the page. You can analyse login process with this app www.portswigger.net/proxy/.
For what it's worth, you could check out Webrat. It is meant to be used a tool for automated acceptance tests, but I think you could use it to simulate filling out the login fields, then click through links by their names, and grab the needed HTML as a string. Haven't tried doing anything like it, tho.