0

aws lambda에 google's people api을 사용하려고합니다.aws 람다에 Google 사람들 api에 Oauth2

를 I이 함수와 비밀을 저장 :

from oauth2client.file import Storage 
from oauth2client import client, tools 

def get_credentials(): 
    credential_path = os.path.join(SCRIPT_DIR, 'people-api-secret.json') 
    store = Storage(credential_path) 
    credentials = store.get() 
    if not credentials or credentials.invalid: 
     flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES) 
     flow.user_agent = APPLICATION_NAME 
     credentials = tools.run_flow(flow, store) 
     print('Storing credentials to ' + credential_path) 
    return credentials 

그럼 I의 서버없는을 사용 AWS 람다로 people-api-secret.json 전송 API가 OAuth2를 요구하므로 I는 AWS (λ)로 전송 한 후 토큰 국부적으로 OAuth2을 페치하려 뼈대. 하지만 람다에 비밀을로드하려고하면 store.get()None을 반환합니다. 이 파일은 실제로 AWS에 있습니다 ( os.path.isfile(credential_path)True을 반환합니다).

다른 컴퓨터/IP 주소에서 이러한 비밀을 복사 할 수 있습니까? 그렇지 않은 경우 : 예를 들어 "완전한 방법"을 수행하지 않고 이것이 작동하도록하는 "최소한의 방법"은 무엇입니까? here

업데이트는 간단한 람다에 오류를 "권한이 거부"의 것을 찾았 print(open(credential_path).read())[Errno 13] Permission denied: '/var/task/people-api-secret.json'를 얻을 수 있습니다. 그 변수는 파일에서 읽는 대신 환경에 넣어야한다고 생각하십니까?

+1

람다 함수를 배포하기 전에 json 파일에 올바른 권한 (chmod)을 지정했는지 확인하십시오. 런타임에이 파일을 다운로드하는 경우/tmp 디렉토리에 자격 증명을 저장하려고하면 '/tmp/people-api-secret.json'이 표시됩니다.이 파일은 람다 컨테이너에 파일을 쓸 수있는 유일한 디렉토리입니다. –

답변

0

올바른 권한을 부여하는 것이 Tom Melos 주석과 this github issue에 따르면 효과가 있었을 가능성이 있지만 환경 변수에 암호를 넣으려는 것이 가장 좋은 방법으로 설명 되었기 때문에이 변수를 환경 변수에 넣고 싶습니다. 내가 넣어

from oauth2client import client, tools 

class MyStorage(client.Storage): 
    def locked_put(self, credentials): 
    print("="*70) 
    print("client_id: {}\nclient_secret: {}\nrefresh_token: {}".format(
     credentials.client_id, credentials.client_secret, credentials.refresh_token)) 
    print("="*70) 

flow = client.flow_from_clientsecrets('client_secret.json', 'https://www.googleapis.com/auth/contacts.readonly') 
flow.user_agent = 'my-user-agent' 
storage = MyStorage() 
tools.run_flow(flow, storage) 

결과 세 개의 문자열 : (당신이 this guide을 다음과 구글 API를 콘솔에서 다운로드 할 수있는 파일 client_secret.json을 필요로하는) ​​

먼저 내가 토큰을 얻을 수있는 방법이 필요했습니다, 그래서 나는이 실행

자세한 내용은
import os 
from oauth2client import client 
from apiclient import discovery 

client_id = os.environ['GOOGLE_PEOPLE_CLIENT_ID'] 
client_secret = os.environ['GOOGLE_PEOPLE_CLIENT_SECRET'] 
refresh_token = os.environ['GOOGLE_PEOPLE_REFRESH_TOKEN'] 
credentials = client.GoogleCredentials(None, 
    client_id, 
    client_secret, 
    refresh_token, 
    None, 
    "https://accounts.google.com/o/oauth2/token", 
    'my-user-agent') 
http = credentials.authorize(httplib2.Http()) 
service = discovery.build('people', 'v1', http=http, 
    discoveryServiceUrl='https://people.googleapis.com/$discovery/rest', 
    cache_discovery=False) 

이 reque에 무슨 일 나는 documented here했습니다 (개인적으로 난 그냥 OAuth2를의 기초를 배웠) : 환경에 다음이 작업을 수행 할 수 있었다 this guide을 다음과 왜 여기에 refresh_token이 필요한지

2

당신이하고있는 것처럼 비밀 키와 JSON을 업로드 한 후이 작업을 수행 : 환경 변수 키와 같은 람다 구성 설정 GOOGLE_APPLICATION_CREDENTIALS 및 자격 증명 값으로 JSON 파일 이름에

#import GoogleCredentials 
from oauth2client.client import GoogleCredentials 

credentials = GoogleCredentials.get_application_default() 
service = discovery.build('people', 'v1', credentials=credentials,cache_discovery=False) 

합니다.

google api를 사용하는 모든 람다에서 작동합니다.