从 python 脚本使用 google 联系人 API 的简单方法

Simple way to use the google contacts API from a python script

我目前正在尝试编写脚本,我需要访问我的 GMail 联系人才能完成它。我试着用谷歌搜索了一下,看起来有几个库可以做到这一点,但有些答案似乎很老,有些则不太清楚。

目前从脚本访问联系人 API 的正确方法是什么?使用 https://github.com/google/gdata-python-client 我假设 ?

任何最近的响应似乎都涉及用户手动将 link 复制到他的浏览器中,然后被重定向到一些不存在的 URL 并被要求将 URL 复制回去在脚本中解析其中的代码。这看起来非常荒谬,我希望有一种正确的方法来访问非网络应用程序的 API ?

我将是该脚本的唯一用户,所以我不介意输入我的完整电子邮件地址和密码,如果这样做更容易的话,只要我不必重新登录- 手动验证。只想授权一次,让它永远工作。我只是想从 phone 号码中找到联系人的姓名,所以我什至不需要访问所有内容。

这是我最后使用的:

if arg == "--init":
    gflow = OAuth2WebServerFlow(client_id='<ID>', client_secret='<secret>',
                            scope='https://www.googleapis.com/auth/contacts.readonly',
                            redirect_uri='http://localhost');
    gflow.params['access_type'] = 'offline';
    auth_uri = gflow.step1_get_authorize_url();
    weechat.prnt("", "Please go to " + auth_uri + " and come back here with the code. Run /gauth code");
else:
    credentials = gflow.step2_exchange(arg);
    storage.put(credentials);
    http = httplib2.Http();
    http = credentials.authorize(http);
    people = build('people', 'v1', http=http);

与此配合得很好。我确实需要手动复制/粘贴 URL,然后从重定向中获取代码,但由于我将 access_type 置于离线状态,所以我只需执行一次。