简体   繁体   中英

accessing google account via python

I am working on a prototype for an email application. I need to access to my personal gmail account in my application's python code in order to do this. I'm aware that I can probably accomplish this using google API but I thought it would be good practice for me to try to access directly through the standard user interface. I've outlined the steps I think are involved in this process and I would love input both on the structure I propose in the steps as well as how to actually implement in python.

STEP 1: open url for logging into gmail

  1. Namely: https://accounts.google.com/ServiceLogin
  2. This step is fairly easy:

    page=urllib2.urlopen("https://accounts.google.com/ServiceLogin")

STEP 2: submit post request to whatever page to page that authenticates login

  1. From html looks like authentication page is at https://accounts.google.com/ServiceLoginAuth
  2. I was also able to verify that login is handled by a form (named gaia_loginform)
  3. Couple of key questions in this step:
  4. How do I actually submit the post request through urllib? I assume urlopen only handles get requests?
  5. How do I save cookies, hidden form inputs, etc. from login page that are needed for authentication and include them in my get request?

As I think more about this I wonder if I am approaching it the right way. In principle, all I want the computer to do is go onto the login page, enter the login data and click the submit button. In excel you could accomplish this by recording a macro. You could envision the server literally going onto a web browser, typing in page address, filling in information and clicking submit. If there was an equivalent solution here it seems that it would be much easier than recreating the entire process through urllib commands. Hopefully above reasoning makes sense. Appreciate your thoughts.

Evan

Selenium is a python module which you can use to emulate what you want to do. It has excellent emulation capabilities and the webdriver instance (which a selenium script will start) can do everything which a normal browser does.

Just to show how easy it is to use selenium :-

 elem = driver.find_element_by_id('username').send_keys("USERNAME")

Similarly, commands like click, submit, find_element_by_class etc, make it very easy to use selenium.

Note: Selenium opens a webdriver instance (GUI) on the screen and thus, isn't entirely behind the scenes. If you need that, you may need to look at other libraries like mechanize.

All the best!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM