しかし、Contacts APIは、People APIに置き換わっていくそうなので、これからGoogle連絡先と連携するアプリケーションを作成する際はPeople APIの方を用いると良いかも知れません。
Posted by Amos Yuen, Software Engineer, and Wesley Chun ( @wescpy ), Developer Advocate, G Suite Starting today, the Google People API w...
I found there is batch operation in Contact API. But it's XML form and I prefer to use JSON form in People API. Is it possible?
PythonからPeople APIを使う
Google People APIをPythonから使うためのサンプルコードとしては、公式ドキュメントに記載してあるPython Quickstartがあります。本サイトでは、会社のデータベースや年賀状作成ソフトから出力した住所録のcsvをGoogle連絡先に同期させることを目標として、プログラムを作成しようと思います。
本記事では、Python Quickstart内のquickstart.pyを元にGoogle連絡先のデータを取得・表示するコードを作成したものを公開します。
People APIの有効化と必要なライブラリのインストール
まずは、Python Quickstartの内容に従って、サンプルコードquickstart.pyが動く状態にします。下記のコードでは、ここでダウンロードしたcredentials.jsonをそのまま利用します。
また、People APIは、現時点ではPython 2.6以上に対応していますが、Python 2系統はサポートが終了し、Python 3系への移行が推奨されることから、
本サイト記載のコードは、Python 3系環境で実行することを想定しています。
問題点:people.connections.listのpageSizeの上限
公式ドキュメントによると、記事作成時(2019.06.19)時点では、pageSizeは1~1000の間しか設定出来ない様です。(少し前まで上限は2000だった様な…?)
この上限を超える件数の連絡先を取得したい場合は、nextPageTokenを利用すると良い様です。
I am working on Google API with Node.js. I need to retrieve all the contacts list from google contacts. There are more than 2000 contacts in the list but the page size limit is 2000,so I am getting...
作成したコード
必要なファイル:credentials.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pickle | |
import os.path | |
from googleapiclient.discovery import build | |
from google_auth_oauthlib.flow import InstalledAppFlow | |
from google.auth.transport.requests import Request | |
# If modifying these scopes, delete the file token.pickle. | |
SCOPES = ['https://www.googleapis.com/auth/contacts'] | |
CLIENT_SECRET_FILE = 'credentials.json' | |
class QuickstartMod(object): | |
def __init__(self): | |
creds = None | |
# The file token.pickle stores the user's access and refresh tokens, and is | |
# created automatically when the authorization flow completes for the first time. | |
if os.path.exists('token.pickle'): | |
with open('token.pickle', 'rb') as token: | |
creds = pickle.load(token) | |
# If there are no (valid) credentials available, let the user log in. | |
if not creds or not creds.valid: | |
if creds and creds.expired and creds.refresh_token: | |
creds.refresh(Request()) | |
else: | |
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES) | |
creds = flow.run_local_server(port=0) | |
# Save the credentials for the next run | |
with open('token.pickle', 'wb') as token: | |
pickle.dump(creds, token) | |
creds = creds | |
self.service = build('people', 'v1', credentials=creds) | |
def get_all_contacts(self): | |
# Keep getting 1000 connections until the nextPageToken becomes None | |
connections_list = [] | |
next_page_token = '' | |
while True: | |
if not (next_page_token is None): | |
# Call the People API | |
results = self.service.people().connections().list( | |
resourceName = 'people/me', | |
pageSize = 1000, | |
personFields = 'names,emailAddresses', | |
pageToken = next_page_token | |
).execute() | |
connections_list = connections_list + results.get('connections', []) | |
next_page_token = results.get('nextPageToken') | |
else: | |
break | |
return connections_list | |
def print_all_contacts(self): | |
connections_list = self.get_all_contacts() | |
if connections_list: | |
for i, person in enumerate(connections_list): | |
resource_name = person.get('resourceName', []) | |
names = person.get('names', []) | |
if names: | |
display_name = names[0].get('displayName') | |
else: | |
display_name = None | |
print('no.%s %s %s' % (i+1, display_name, resource_name)) | |
else: | |
print('No connections found.') | |
def main(): | |
sync_contacts = QuickstartMod() | |
sync_contacts.print_all_contacts() | |
if __name__ == '__main__': | |
main() |
続き
前回 に引き続き、 Google People API を触ってみます。 会社のデータベースや年賀状作成ソフトから出力した住所録のcsvをGoogle連絡先に同期させることを目標として設定しました。 本記事では、csvのファイルを読み込んで、Google連絡先に追加する部分...
0 件のコメント:
コメントを投稿