구글 Drive API 사용법

pyk0844·2023년 1월 26일
0

Google Cloud > Google Workspace > Google Drive(Google One에 통합)


1. 클라우드에 api 발급을 위한 프로젝트 생성(구글 계정 필수)


2. API 키, OAuth 2.0 클라이언트 ID 발급


3. 라이브러리 사용 설정

API 및 서비스 > 라이브러리 > 라이브러리 선택 > 사용 버튼 클릭

사용 설정 안해 줄 경우 에러 발생
-> GaxiosError: Access Not Configured. Drive API has not been used in project <프로젝트 번호> before or it is disabled.
-> 라이브러리 > 해당 api > 사용 버튼 클릭 해주면 됌

Develop/Etc
Access Not Configured. Drive API has not been used in project {프로젝트번호} before or it is disabled


4. 상세 사용법 참고

4-1. 공식 깃헙 샘플 이용

googleapis/google-api-nodejs-client

구글에서 제공하는 샘플 프로그램 실행시(node quickstart.js) 에러
-> Error: Cannot find module '...\google-api-nodejs-client\samples\oauth2.keys.json'
--> 해당 경로에 'oauth2.keys.json' 이 파일이 없어서 발생
--> 클라우드 콘솔에서 client id를 받고 다운로드 버튼 클릭시 json 파일 생성해주는데 해당 경로에 복붙해주고 이름 변경해주면 됌

Where is sample oauth2.keys.json #2196

{"web": 
	{"client_id":"<client_id>",
	"project_id":"<project_id>",
"auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"<client_secret>","redirect_uris":["http://localhost:8080/login/oauth2/code/google"]
	}
}

4-2. Reference > API > 하단 Try this method 부분 이용

workspace


5. 기타 참고

method별로 scope를 다르게 줄 수 있는데 해당 부분 참고하면 좋다.(Authorization 부분)

사용 예시

const auth = await authenticate({
    keyfilePath: path.join(__dirname, '../oauth2.keys.json'),
    scopes: 'https://www.googleapis.com/auth/drive.metadata.readonly',
  });
  google.options({auth});

Google Drive scopes


순서 참고
Google Workspace에서 개발
Api 키 인증 참고
API 키를 사용하여 인증

0개의 댓글