OAuth 2.0 API 절차 정리 (google 구글 로그인 연동 후 youtube 유튜브 동영상 검색을 통해서 가져오기)
절차
- 유저 code 가져오기 - (구글 로그인창에서 유저가 권한을 부여하면)
- code를 통해서 (중요)refresh_token 및 (임시)access_token 가져오기
- access_token이 1시간(3600ms)마다 만료되는데 refresh_token을 통해서 갱신하여 로그인 유지
- 3번에서 실패의 경우: refresh_token이 유효하지 않은 경우일 가능성이 큼. refresh_token은 첫 연동 시에만 발급 되기 때문에 새로 발급받기 위해서는 token을 취소하고 유저로부터 재연동을 유도해야함
- access_token 을 통해서 각종 데이터 가져오기
- 유저의 기본정보 얻기
- 검색하고자 하는 태그나 단어를 통해 동영상ID 얻기
- 동영상ID로 동영상 정보 얻기
https://developers.google.com/youtube/v3/guides/auth/server-side-web-apps
상세
(클라이언트사이드)
get 요청
1. code 얻기
유저가 권한을 부여하면 code를 얻음
https://accounts.google.com/o/oauth2/v2/auth?
redirect_uri={callbackUrl} &
response_type=code &
client_id={client_id} &
scope=https://www.googleapis.com/auth/youtube.readonly &
access_type=offline
<client_id 및 client_secret>
- Google Developers Console - OAuth 2.0 클라이언트 ID 상단에 있음.
response
http://example.com/example_directory/?
code=4%2FjD21vE3nIwsCjBDH-DbXRxugvLHgaQC-rBJ1oNi8sHk#
(서버사이드)
post 요청
2. code를 통해서 access_token 얻기
첫 연동 시에만 하면 됨
https://accounts.google.com/o/oauth2/token?
code={code} &
client_id={client_id} &
client_secret={client_secret} &
redirect_uri={redirect_uri} &
grant_type="authorization_code"
<redirect_uri>
- Google Developers Console - OAuth 2.0 클라이언트 ID - 승인된 리디렉션 URI 에 등록이 되어있어야 한다.
- query string을 허용하지 않음.
response
- 최초 연동 (정상적인 경우)
{
"access_token" : "ya29.GltZBSkIf23G21usdyOab5i83wuDDhg5eisiY--9tl_x2AHjm-9wX-aHMh6xbhRjhiGwqLjwROSqkouHmgWtKivWj35iGob_fjCjdLSs9IjM-IfP_T9Xb6UP-tHQ",
"expires_in" : 3600,
"refresh_token" : "1/BvBCGUfg-fdkse6dR-cdGiaR_V-SLiDQaYM3omHkS4o",
"token_type" : "Bearer"
}
- 최초 연동이 아닌 경우 (refresh_token을 주지 않음)
{
"access_token" : "ya29.GltZBVsB8M9UVg_4l7JzCjtUFNCxcEKzIj2c4QsVAzoC_-CCIqypak3NJ95iNAtUy2sOM2gY9h_68xi2SBG7cPb3Ygi9al2E8chtDWfVaOGL_-1v5jizn4Rk3o4T",
"expires_in" : 3598,
"token_type" : "Bearer"
}
3. access_token 만료 상태 확인하기
https://www.googleapis.com/oauth2/v1/tokeninfo?
access_token={access_token}
response
- 만료
{
"error": "invalid_token",
"error_description": "Invalid Value"
}
- 만료 안됨
{
"issued_to": "245-cgqtpdgcnt5n6o8.apps.googleusercontent.com",
"audience": "245-cgqtpdgcnt5n6o8.apps.googleusercontent.com",
"scope": "https://www.googleapis.com/auth/youtube.readonly",
"expires_in": 3553,
"access_type": "offline"
}
4. 만료된 access_token 갱신하기
https://accounts.google.com/o/oauth2/token?
client_id={client_id} &
client_secret={client_secret} &
refresh_token={refresh_token} &
grant_type="refresh_token"
response
- 비정상 refresh_token
{
"error" : "invalid_request",
"error_description" : "Missing required parameter: refresh_token"
}
- 정상 갱신
{
"access_token" : "ya29.GltZBcG0ko1w91KFzP8_W4PV1vYUWTCBGG0FhGdIxTHSlP7yyhUPG6b8f3djbJOTGwfSMaf_WyMpPDRG6u3QR4i20JpFAYGfJxeNATuO39EqjOU_sM2vSDqWZrnt",
"expires_in" : 3600,
"token_type" : "Bearer"
}
5. 유튜브 - 유저의 기본 정보 얻기
https://www.googleapis.com/youtube/v3/channels?
access_token={access_token}&
part="snippet,statistics"&
mine=true
response



6. 유튜브 - 검색하고자 하는 태그나 단어를 통해 동영상ID 얻기
https://www.googleapis.com/youtube/v3/search?
access_token={access_token} &
part="id" &
q="검색할 단어나 태그" &
forMine="true" &
type="video"
response
{
"kind": "youtube#searchListResponse",
"etag": "",
"nextPageToken": "Cib3-nqn8_____9rVDdjNnlyc3dXcwD_Af_-a1Q3YzZ5cnN3MAARABIa5I5Tvd3y-QOQAAAAAMWIUFSAFQAloLCV6DRi3vNm3vEAJg_eGT3AE=",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 5
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "\"Wu2llbfqCdx/_dujpjJb23VH\"",
"id": {
"kind": "youtube#video",
"videoId": "kT7rsw"
}
}
]
}
7. 유튜브 - 동영상ID로 동영상 정보 얻기
https://www.googleapis.com/youtube/v3/videos?
access_token={access_token} &
part="id,snippet,contentDetails,fileDetails,liveStreamingDetails,player,processingDetails,recordingDetails,statistics,status,suggestions,topicDetails" &
id={videoId}
response




- 더 자세한 사항 : https://developers.google.com/youtube/v3/guides/auth/server-side-web-apps
- google developer console