简体   繁体   English

通过python访问Google帐户

[英]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. 我需要在我的应用程序的python代码中访问我的个人Gmail帐户才能执行此操作。 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. 我知道我可以使用谷歌API完成这项工作,但我认为尝试直接通过标准用户界面访问是一种很好的做法。 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. 我已经概述了我认为在这个过程中涉及的步骤,我希望对我在步骤中提出的结构以及如何在python中实际实现进行输入。

STEP 1: open url for logging into gmail 第1 打开URL登录gmail

  1. Namely: https://accounts.google.com/ServiceLogin 即: 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 第2步: 将post请求提交到验证登录的页面的任何页面

  1. From html looks like authentication page is at https://accounts.google.com/ServiceLoginAuth 从html看来,身份验证页面位于https://accounts.google.com/ServiceLoginAuth
  2. I was also able to verify that login is handled by a form (named gaia_loginform) 我还能够验证登录是否由表单处理(名为gaia_loginform)
  3. Couple of key questions in this step: 这一步中的几个关键问题:
  4. How do I actually submit the post request through urllib? 如何通过urllib实际提交帖子请求? I assume urlopen only handles get requests? 我假设urlopen只处理获取请求?
  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? 如何从登录页面保存身份验证所需的cookie,隐藏表单输入等,并将其包含在我的get请求中?

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. 在excel中,您可以通过录制宏来实现此目的。 You could envision the server literally going onto a web browser, typing in page address, filling in information and clicking submit. 您可以设想服务器真正进入Web浏览器,输入页面地址,填写信息并单击提交。 If there was an equivalent solution here it seems that it would be much easier than recreating the entire process through urllib commands. 如果这里有一个等效的解决方案,那么通过urllib命令重建整个过程会更容易。 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. Selenium是一个python模块,您可以使用它来模拟您想要做的事情。 It has excellent emulation capabilities and the webdriver instance (which a selenium script will start) can do everything which a normal browser does. 它具有出色的仿真功能,而webdriver实例(selenium脚本将启动)可以完成普通浏览器所做的一切。

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. 类似地,click,submit,find_element_by_class等命令使得使用selenium变得非常容易。

Note: Selenium opens a webdriver instance (GUI) on the screen and thus, isn't entirely behind the scenes. 注意: Selenium在屏幕上打开一个webdriver实例(GUI),因此,并不完全在幕后。 If you need that, you may need to look at other libraries like mechanize. 如果您需要,您可能需要查看其他库,如mechanize。

All the best! 祝一切顺利!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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