Google Drive API Python 서비스 계정 예
Oct 28 2020
OAuth 흐름이 아닌 Google 서비스 계정을 사용하여 Google Drive API에 대해 인증 할 수 있습니까?
OAuth를 사용하는 Google 드라이브 용 Python 예제-Google Drive Python 빠른 시작
그러나 서비스 계정 예를 찾을 수 없습니다.
그러나 내가 사용하는 대부분의 다른 Google API (Translate, Cloud Vision) 는 서비스 계정을 사용하므로 일관성을 위해 Google 드라이브 OAuth 코드를 지원 중단하고 싶습니다.
답변
2 DaImTo Oct 28 2020 at 17:19
The best service account python example that i know of is the one for Google analytics
It should be something like this.
from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
SCOPES = ['https://www.googleapis.com/auth/drive.readonly']
KEY_FILE_LOCATION = '<REPLACE_WITH_JSON_FILE>'
VIEW_ID = '<REPLACE_WITH_VIEW_ID>'
def initialize_drive():
"""Initializes an service object.
Returns:
An authorized service object.
"""
creds = ServiceAccountCredentials.from_json_keyfile_name(
KEY_FILE_LOCATION, SCOPES)
# Build the service object.
service = build('drive', 'v3', credentials=creds)
return service
Once you have the drive service you should be able to use the rest of the code you have from the other tutorial. Its just the auth method that is diffrent.
Just remember to create service account credentials and not Oauth credentials.