AWS Google API身份验证 [英] AWS google api authentication

查看:116
本文介绍了AWS Google API身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a script in python that will run once a day and delete all files in g-drive in a particular folder. I have tested it on my local machine and it asked me to login using my gmail account. I did that and created a credentials file so I don't have to login each time I run the script.

我现在正在尝试在AWS中创建一个lambda函数,并每天触发一次.我已经创建了部署程序包,将其上传并似乎正在运行,但是它一直试图打开浏览器以再次登录到g-drive.这是我在日志中找到的内容:

I'm now trying to create a lambda function in AWS and trigger it once a day. I have created deployment package, uploaded it and it seems to be running however it keeps trying to open a browser to login to g-drive again. Here's what I found in the logs:

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和凭据.json都上传到了AWS lambda函数,并认为它可以正常工作,因为凭据已经存在,并且在我的计算机上运行良好.我怎样才能使它正常工作?

I uploaded both client_secret.json and credentials.json generated on my local machine to AWS lambda function and thought it'd work since the creds are already there and this runs fine on my machine. How can I get this to work?

推荐答案

好,我知道出了什么问题.对凭据.json文件的权限不足,无法读取.在将文件压缩并上传到Lambda之前,我必须更改权限.这解决了问题.

Ok, I figured out what was wrong. Permissions to credentials.json file were insufficient and couldn't be read. Before zipping and uploading the file to Lambda I had to change change the permissions. This solved the issue.

这篇关于AWS Google API身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆