1日に1回実行され、特定のフォルダー内のg-drive内のすべてのファイルを削除するPythonのスクリプトがあります。ローカルマシンでテストしたところ、Gmailアカウントを使用してログインするように求められました。スクリプトを実行するたびにログインする必要がないように、それを実行して資格情報ファイルを作成しました。
現在、AWSでラムダ関数を作成し、1日1回トリガーしようとしています。展開パッケージを作成してアップロードしましたが、実行されているようですが、ブラウザを開いてg-driveに再度ログインしようとし続けます。ログで見つけたものは次のとおりです。
Your browser has been opened to visit:
https://accounts.google.com/o/oauth2/auth?client_id=removed from this example
If your browser is on a different machine then exit and re-run this
application with the command-line parameter
--noauth_local_webserver
コードは次のとおりです。
from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
SCOPES = 'https://www.googleapis.com/auth/drive'
store = file.Storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('drive', 'v3', http=creds.authorize(Http()))
def list_folders(drive_id):
body = {'teamDriveId': drive_id}
parent = "parent_id"
drives = service.files().list(corpora="teamDrive", teamDriveId=drive_id, includeTeamDriveItems=True, supportsTeamDrives=True, fields='files').execute().get('files')
for drive in drives:
if parent in drive['parents']:
service.files().delete(fileId=drive['id'], supportsTeamDrives=True).execute()
print("Deleted all files!")
def lambda_handler(event, context):
list_folders("0k9PVA")(not real id)
return 'Hello from Lambda'
ローカルマシンで生成されたclient_secret.jsonとcredentials.jsonの両方をAWSラムダ関数にアップロードしましたが、credsがすでに存在し、これは私のマシンで正常に実行されるため、機能すると思いました。どうすればこれを機能させることができますか?
わかりました、私は何が悪かったのか理解しました。authentication.jsonファイルへのアクセス許可が不十分であり、読み取ることができませんでした。ファイルを圧縮してLambdaにアップロードする前に、アクセス許可を変更する必要がありました。これで問題は解決しました。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加