Google Colaboratoryで動画の文字起こしを行い、その結果をGoogleドキュメントに保存する際に、認証情報の設定が必要です。
サービスアカウントを使用する方法
a. Google Cloud Consoleでサービスアカウントを作成します。
b. 必要な権限(Google Docs API)を付与します。
c. JSONキーファイルをダウンロードし、Colaboratoryにアップロードします。
d. 以下のコードを使用して認証を行います。




from google.oauth2 import service_account
from googleapiclient.discovery import build
SCOPES = ['https://www.googleapis.com/auth/documents']
SERVICE_ACCOUNT_FILE = '/path/to/service_account.json'
creds = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = build('docs', 'v1', credentials=creds)
OAuth 2.0クライアントIDを使用する方法:
a. Google Cloud ConsoleでOAuth 2.0クライアントIDを作成します。
b. JSONファイルをダウンロードし、Colaboratoryにアップロードします。
c. 以下のコードを使用して認証を行います。


from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import Flow
SCOPES = ['https://www.googleapis.com/auth/documents']
CLIENT_SECRET_FILE = '/path/to/client_secret.json'
flow = Flow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES)
creds = flow.run_local_server(port=0)
service = build('docs', 'v1', credentials=creds)
まとめ
これらの方法を使用することで、token.json
ファイルを必要とせずに認証を行い、Google Docs APIにアクセスできます。サービスアカウントを使用する方法が、自動化には適しています。
なお、不正利用や課金が発生する可能性があります。ファイルや認証情報の取り扱いにはくれぐれも注意ください。