在独立的Python脚本中向Google进行身份验证的正确机制是什么? [英] What is the correct mechanism to authenticate to Google in a standalone Python script?

查看:57
本文介绍了在独立的Python脚本中向Google进行身份验证的正确机制是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码可用于将Gmail联系人的电子邮件地址提取到文本文件中.这是一个简单的Python脚本,可在cron作业中运行,并且基于 Python gdata库(当前为v2.0.18).

I've got some code that I'm using to extract email address from Gmail contacts into text file. It's a simple Python script that runs in a cron job, and is based on the Python gdata library (currently v2.0.18).

从本月初开始,由于Google 淘汰ClientLogin协议.产生的错误如下所示:

As of earlier this month, this no longer works due to Google deprecating the ClientLogin protocol. The resulting error looks like this:

{'status': 401, 'body': '<?xml version="1.0" encoding="UTF-8"?>\n<errors xmlns="http://schemas.google.com/g/2005">\n <error>\n  <domain>GData</domain>\n  <code>required</code>\n  <location type="header">Authorization</location>\n  <internalReason>Login Required</internalReason>\n </error>\n</errors>\n', 'reason': 'Unauthorized'}

我知道这将在其他地方(例如AppEngine应用程序)处理,但是忘记了我必须转换此脚本.现在我在这里,我发现我不知道如何进行这项工作.

I knew this was coming and dealt with it in other places (like AppEngine applications), but forgot that I would have to convert this script. Now that I'm in here, I find that I have no idea how I'm supposed to make this work.

我找到的所有参考,例如此处,或在此处在StackOverflow上此处,建议解决方案是使用OAuth2Token.但是,这需要来自Google API控制台的客户端ID和客户端密码(与应用程序绑定).我没有申请.我只想从我的脚本进行身份验证.

All of the references I've found, such as here on the Google Apps Developer Blog or here and here on StackOverflow, suggest that the solution is to use an OAuth2Token. However, that requires a client id and client secret from the Google APIs console -- which is tied to an application. I don't have an application. I just want to authenticate from my script.

有人可以建议在独立脚本中执行此操作的正确方法吗?还是我不走运,没有任何机制可以再完成这个任务?

Can someone please suggest the proper way to do this in a standalone script? Or am I out of luck and there's no mechanism to accomplish this any more?

这是现有联系人代码的胆量:

This is the guts of the existing contacts code:

from gdata.contacts.service import ContactsService, ContactsQuery

user = "myuser@gmail.com"
password = "mypassword"

addresses = set()
client = ContactsService(additional_headers={"GData-Version":"2"})
client.ssl = True
client.ClientLogin(user, password)
groups = client.GetGroupsFeed()
for group in groups.entry:
   if group.content.text == "System Group: My Contacts":
      query = ContactsQuery()
      query.max_results = 9999   # large enough that we'll get "everything"
      query.group = group.id.text
      contacts = client.GetContactsFeed(query.ToUri())
      for contact in contacts.entry:
         for email in contact.email:
            addresses.add(email.address.lower())
      break
return addresses

理想情况下,我正在寻找将client.ClientLogin()替换为其他一些使用gdata保留其余代码的机制.另外,如果使用gdata不能真正做到这一点,我愿意转换为其他具有类似功能的库.

Ideally, I'm looking to replace client.ClientLogin() with some other mechanism that preserves the rest of code using gdata. Alternately, if this can't really be done with gdata, I'm open to converting to some other library that offers similar functionality.

推荐答案

有人可以建议在独立脚本中执行此操作的正确方法吗?还是我不走运,没有任何机制可以再完成这个任务?

Can someone please suggest the proper way to do this in a standalone script? Or am I out of luck and there's no mechanism to accomplish this any more?

没有其他机制可以使用.您将必须设置 Cloud Developer项目,并使用OAuth2,然后重写脚本

There's no mechanism any more like the one you are using. You will have to set up a Cloud Developer project and use OAuth2, and rewrite your script.

要使其尽可能地适用于未来,您可以切换到最新的 Contacts API .使用此API,您可以使用 OAuth2设备流,它可能更易于使用情况.

To make it as futureproof as possible, you could switch to the newest Contacts API. With this API, you can use the OAuth2 Device flow, which might be simpler for your use case.

我知道,这不是您希望听到的答案,但我认为这是唯一的答案.

Not the answer you were hoping to hear, I know, but I think it's the only answer.

这篇关于在独立的Python脚本中向Google进行身份验证的正确机制是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆