Google API Pythonクライアントは、GoogleのディスカバリーベースのAPI用のPythonクライアントライブラリです。このライブラリは完全であり、メンテナンスモードにあります。つまり、重大なバグとセキュリティ問題は対処しますが、新しい機能は追加しません。
インストール
Google API Pythonクライアントはpipを使用してインストールできます。以下のコマンドを実行します。
pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
使用方法
Google API Pythonクライアントを使用すると、GoogleのディスカバリーベースのAPIと通信できます。以下に、Google API Pythonクライアントを使用してGmail APIと通信する例を示します。
from __future__ import print_function
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from apiclient import errors
import base64
import csv
import email
import dateutil.parser
def gmail_init():
creds = None
if os.path.exists(tokenPath):
creds = Credentials.from_authorized_user_file(tokenPath, SCOPES)
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(credentialsPath, SCOPES)
creds = flow.run_local_server(port=0)
with open(tokenPath, 'w') as token:
token.write(creds.to_json())
service = build('gmail', 'v1', credentials=creds)
return service
service = gmail_init()
このコードは、Gmail APIとの接続を初期化し、その接続を使用してメールの情報を取得します。
注意点
このライブラリは、Googleが公式にサポートしていますが、このリポジトリのメンテナは、可能な場合はPythonのCloud Client Librariesを使用することをお勧めしています。新しいコード開発には、Cloud Client Librariesを使用することをお勧めします。
以上がGoogle API Pythonクライアントの基本的な使用方法です。詳細なドキュメンテーションについては、公式のドキュメントを参照してください。.