File size: 1,166 Bytes
677c9b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import os
import json
from google.oauth2 import service_account

# 環境変数からサービスアカウントのJSON内容を取得
service_account_info = os.getenv('GOOGLE_APPLICATION_CREDENTIALS_CONTENT')

if service_account_info is None:
    raise ValueError("サービスアカウントのJSON内容が設定されていません。")

# JSON文字列を辞書に変換
service_account_info_dict = json.loads(service_account_info)

# サービスアカウント情報を使用して認証情報を作成
credentials = service_account.Credentials.from_service_account_info(service_account_info_dict)

# これでcredentialsを使用してGoogle Chat APIにアクセスできます。
# 例えば、Google Chat APIクライアントを作成するには次のようにします。
from googleapiclient.discovery import build

chat_service = build('chat', 'v1', credentials=credentials)

# 例: メッセージを送信する
space_name = 'spaces/your-space-id'
message = {
    'text': 'Hello from the Google Chat API!'
}

response = chat_service.spaces().messages().create(
    parent=space_name,
    body=message
).execute()

print('Message sent: ', response)