SuzukiTakamasa / LINEBot

https://line-bot-lilac.vercel.app
0 stars 0 forks source link

csvで取得しているポケモンのデータをgspreadを使ってスプレッドシートから取得するようにする #12

Open SuzukiTakamasa opened 4 months ago

SuzukiTakamasa commented 4 months ago

Google Sheets APIの有効化とクレデンシャルファイルの取得:

Google Cloud Consoleで新しいプロジェクトを作成し、Google Sheets APIを有効化します。 サービスアカウントを作成し、JSON形式のクレデンシャルファイルをダウンロードします。

import gspread
from oauth2client.service_account import ServiceAccountCredentials

# スコープの設定
scope = [
    'https://spreadsheets.google.com/feeds',
    'https://www.googleapis.com/auth/drive'
]

# ダウンロードしたクレデンシャルファイルのパスを指定
creds = ServiceAccountCredentials.from_json_keyfile_name('path/to/your/credentials.json', scope)

# クライアントを認証してインスタンス化
client = gspread.authorize(creds)

# スプレッドシートの取得
spreadsheet = client.open('your_spreadsheet_name')

# シートの取得(シート名が "master.csv" の場合)
sheet = spreadsheet.worksheet('master.csv')

# 2次元配列として全データを取得
data = sheet.get_all_values()

# 結果を表示
for row in data:
    print(row)
SuzukiTakamasa commented 4 months ago

def get_secret(project_id, secret_id, version_id="latest"): client = secretmanager.SecretManagerServiceClient() secret_name = f"projects/{project_id}/secrets/{secret_id}/versions/{version_id}" response = client.access_secret_version(name=secret_name) secret_data = response.payload.data.decode('UTF-8') return secret_data

GCPプロジェクトIDとシークレットIDを指定

project_id = 'your-gcp-project-id' secret_id = 'your-secret-id'

シークレットを取得

credentials_json = get_secret(project_id, secret_id)