Python에서 Google G Suite API (Sheets/Slides 등) 호출
개요
이 기사에서는 Python에서 Google Slides/Spread Sheets/Documentation/Gmail을 API에서 조작할 때 필요한 자격 증명 설정 및 소통 확인을 설명합니다. (소요시간 20분 정도)
Google Cloud Platform 자체의 아키텍처는 항상 업데이트되므로 본 문서는 실제 GUI 및 UX와 다를 수 있습니다. (2019/11/4 시점 최신)
절차 (자격 증명 다운로드)
본 기사에서는 G Suite계 API의 대표로서 Spread Sheets를 Call하는 Sheets API를 예로 실시합니다.
본 기사에서는 G Suite계 API의 대표로서 Spread Sheets를 Call하는 Sheets API를 예로 실시합니다.
Sheets API Python Quick Start
![](https://s1.md5.ltd/image/9c36cc8a92d59befdd126fe66b1a8fbb.png)
![](https://s1.md5.ltd/image/9a2a0f07ceffc753ff647e6dc75c6a95.png)
project name은 기본적으로 "Quickstart"로 되어 있지만 어떠한 것도 괜찮습니다.
![](https://s1.md5.ltd/image/a75952069db6bc2af3e31e66ce4c422f.png)
![](https://s1.md5.ltd/image/5e72bf8627e1e42f1a21d1b3044aa523.png)
이것으로 자격 증명 설정이 완료됩니다.
인증의 소통 확인을 하고 싶은 분은 이하의 순서를 실시합시다.
절차 (인증 소통)
a. pip install에서 필요한 라이브러리를 설치합니다.pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
b. 모든 디렉토리에 quickstart.py라는 py 파일을 작성하고 다음 코드를 복사하십시오.
quickstart.py
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
# The ID and range of a sample spreadsheet.
SAMPLE_SPREADSHEET_ID = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms'
SAMPLE_RANGE_NAME = 'Class Data!A2:E'
def main():
"""Shows basic usage of the Sheets API.
Prints values from a sample spreadsheet.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('sheets', 'v4', credentials=creds)
# Call the Sheets API
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID,
range=SAMPLE_RANGE_NAME).execute()
values = result.get('values', [])
if not values:
print('No data found.')
else:
print('Name, Major:')
for row in values:
# Print columns A and E, which correspond to indices 0 and 4.
print(row)
if __name__ == '__main__':
main()
c. 작성한 quickstart.py와 동일한 디렉토리에 다운로드한 credentials.json을 배치하십시오.
d. quickstart.py가 있는 디렉토리로 변경하고 다음 명령으로 실행하십시오.
python quickstart.py
e. 웹 브라우저에 액세스 권한 부여 화면이 나타나면 인증을 허용합니다.
※이 화면의 앞에 보안 경고 화면이 나왔을 경우는 이동하는 링크를 클릭해 주세요
![](https://s1.md5.ltd/image/db825fc18109070a1dc4e965da4755e1.png)
![](https://s1.md5.ltd/image/5390ec5503c083d22a911ebfb08d6af1.png)
f. py 파일의 실행 화면에 결과가 출력되면 성공입니다.
추가
2019/11/08 : 제목 수정
Reference
이 문제에 관하여(Python에서 Google G Suite API (Sheets/Slides 등) 호출), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/ken0078/items/ece6fe2a871446383481
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
Reference
이 문제에 관하여(Python에서 Google G Suite API (Sheets/Slides 등) 호출), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/ken0078/items/ece6fe2a871446383481텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)