AWS google api身份验证

时间:2018-05-29 12:08:34

标签: amazon-web-services google-api google-oauth

我在python中有一个脚本,每天运行一次并删除特定文件夹中g-drive中的所有文件。 我在我的本地计算机上测试了它,并要求我使用我的Gmail帐户登录。我这样做并创建了一个凭证文件,因此每次运行脚本时我都不必登录。

我现在正在尝试在AWS中创建一个lambda函数并每天触发一次。我已创建部署包,上传它似乎正在运行但是它一直试图打开浏览器再次登录到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 lambda函数并认为它已经工作,因为信用已经存在并且在我的机器上运行正常。我怎样才能让它发挥作用?

1 个答案:

答案 0 :(得分:1)

好的,我弄清楚出了什么问题。 credentials.json文件的权限不足,无法读取。在将文件压缩并上传到Lambda之前,我必须更改权限。这解决了这个问题。