Poking at the YouTube v3 API a bit
This commit is contained in:
parent
3b7bbd5ff1
commit
f0bbf3fa8e
4
youtube-api-poking/.gitignore
vendored
Normal file
4
youtube-api-poking/.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
client_id.json
|
||||
.env
|
||||
subscriptions.json
|
||||
exchange_auth.json
|
20
youtube-api-poking/create-grant-url
Executable file
20
youtube-api-poking/create-grant-url
Executable file
@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env bash
|
||||
set -o errexit
|
||||
|
||||
main() {
|
||||
[[ "${OOM_CLIENT_ID}" ]] || {
|
||||
echo "Missing \$OOM_CLIENT_ID"
|
||||
exit 1
|
||||
}
|
||||
|
||||
local url='https://accounts.google.com/o/oauth2/auth'
|
||||
|
||||
url="${url}?client_id=${OOM_CLIENT_ID}"
|
||||
url="${url}&redirect_uri=urn:ietf:wg:oauth:2.0:oob"
|
||||
url="${url}&scope=https://www.googleapis.com/auth/youtube"
|
||||
url="${url}&response_type=code&access_type=offline"
|
||||
|
||||
echo "${url}"
|
||||
}
|
||||
|
||||
main "$@"
|
33
youtube-api-poking/exchange-auth-code
Executable file
33
youtube-api-poking/exchange-auth-code
Executable file
@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env bash
|
||||
set -o errexit
|
||||
|
||||
main() {
|
||||
local single_use_token="${1}"
|
||||
|
||||
[[ "${single_use_token}" ]] || {
|
||||
echo "Missing single use token arg"
|
||||
exit 1
|
||||
}
|
||||
|
||||
[[ "${OOM_CLIENT_ID}" ]] || {
|
||||
echo "Missing \$OOM_CLIENT_ID"
|
||||
exit 1
|
||||
}
|
||||
|
||||
[[ "${OOM_CLIENT_SECRET}" ]] || {
|
||||
echo "Missing \$OOM_CLIENT_SECRET"
|
||||
exit 1
|
||||
}
|
||||
|
||||
curl \
|
||||
-X POST \
|
||||
-sSL \
|
||||
-d "client_id=${OOM_CLIENT_ID}" \
|
||||
-d "code=${single_use_token}" \
|
||||
-d "client_secret=${OOM_CLIENT_SECRET}" \
|
||||
-d 'redirect_uri=urn:ietf:wg:oauth:2.0:oob' \
|
||||
-d 'grant_type=authorization_code' \
|
||||
'https://accounts.google.com/o/oauth2/token'
|
||||
}
|
||||
|
||||
main "$@"
|
64
youtube-api-poking/functions.bash
Normal file
64
youtube-api-poking/functions.bash
Normal file
@ -0,0 +1,64 @@
|
||||
#!/usr/bin/env bash
|
||||
set -o errexit
|
||||
|
||||
__fetch_access_token() {
|
||||
local client_id="${1}"
|
||||
local client_secret="${2}"
|
||||
local refresh_token="${3}"
|
||||
local oauth2_token_url="${4}"
|
||||
: "${oauth2_token_url:=https://accounts.google.com/o/oauth2/token}"
|
||||
|
||||
curl \
|
||||
-sSL \
|
||||
-X POST \
|
||||
-d "client_id=${client_id}" \
|
||||
-d "client_secret=${client_secret}" \
|
||||
-d "refresh_token=${refresh_token}" \
|
||||
-d 'grant_type=refresh_token' \
|
||||
"${oauth2_token_url}" \
|
||||
| jq -r '.access_token'
|
||||
}
|
||||
|
||||
__googleapis_http() {
|
||||
local client_id="${1}"
|
||||
shift
|
||||
local client_secret="${1}"
|
||||
shift
|
||||
local refresh_token="${1}"
|
||||
shift
|
||||
local verb="${1}"
|
||||
shift
|
||||
local path="${1}"
|
||||
shift
|
||||
|
||||
local qs=''
|
||||
local first=1
|
||||
|
||||
for param in "${@}"; do
|
||||
if [[ "${first}" = '1' ]]; then
|
||||
qs="${param}"
|
||||
first=0
|
||||
continue
|
||||
fi
|
||||
qs="${qs}&${param}"
|
||||
done
|
||||
|
||||
local access_token
|
||||
access_token="$(
|
||||
__fetch_access_token \
|
||||
"${client_id}" \
|
||||
"${client_secret}" \
|
||||
"${refresh_token}"
|
||||
)"
|
||||
|
||||
local curl_opts
|
||||
if [[ "${DEBUG}" ]]; then
|
||||
curl_opts=-vvvv
|
||||
fi
|
||||
curl \
|
||||
-sSL ${curl_opts} \
|
||||
-X "${verb}" \
|
||||
-H "Authorization: Bearer ${access_token}" \
|
||||
"https://www.googleapis.com${path}?${qs}" \
|
||||
| jq .
|
||||
}
|
15
youtube-api-poking/list-channels
Executable file
15
youtube-api-poking/list-channels
Executable file
@ -0,0 +1,15 @@
|
||||
#!/usr/bin/env bash
|
||||
set -o errexit
|
||||
|
||||
main() {
|
||||
source "$(dirname "${BASH_SOURCE[0]}")/functions.bash"
|
||||
|
||||
__googleapis_http \
|
||||
"${OOM_CLIENT_ID}" \
|
||||
"${OOM_CLIENT_SECRET}" \
|
||||
"${OOM_YOUTUBE_REFRESH_TOKEN}" \
|
||||
GET /youtube/v3/channels \
|
||||
part=contentDetails mine=true maxResults=50
|
||||
}
|
||||
|
||||
main "$@"
|
17
youtube-api-poking/list-playlistitems
Executable file
17
youtube-api-poking/list-playlistitems
Executable file
@ -0,0 +1,17 @@
|
||||
#!/usr/bin/env bash
|
||||
set -o errexit
|
||||
|
||||
main() {
|
||||
local playlist_id="${1}"
|
||||
|
||||
source "$(dirname "${BASH_SOURCE[0]}")/functions.bash"
|
||||
|
||||
__googleapis_http \
|
||||
"${OOM_CLIENT_ID}" \
|
||||
"${OOM_CLIENT_SECRET}" \
|
||||
"${OOM_YOUTUBE_REFRESH_TOKEN}" \
|
||||
GET /youtube/v3/playlistItems \
|
||||
part=snippet mine=true maxResults=50 id=${playlist_id}
|
||||
}
|
||||
|
||||
main "$@"
|
15
youtube-api-poking/list-playlists
Executable file
15
youtube-api-poking/list-playlists
Executable file
@ -0,0 +1,15 @@
|
||||
#!/usr/bin/env bash
|
||||
set -o errexit
|
||||
|
||||
main() {
|
||||
source "$(dirname "${BASH_SOURCE[0]}")/functions.bash"
|
||||
|
||||
__googleapis_http \
|
||||
"${OOM_CLIENT_ID}" \
|
||||
"${OOM_CLIENT_SECRET}" \
|
||||
"${OOM_YOUTUBE_REFRESH_TOKEN}" \
|
||||
GET /youtube/v3/playlists \
|
||||
part=contentDetails mine=true maxResults=50
|
||||
}
|
||||
|
||||
main "$@"
|
15
youtube-api-poking/list-subs
Executable file
15
youtube-api-poking/list-subs
Executable file
@ -0,0 +1,15 @@
|
||||
#!/usr/bin/env bash
|
||||
set -o errexit
|
||||
|
||||
main() {
|
||||
source "$(dirname "${BASH_SOURCE[0]}")/functions.bash"
|
||||
|
||||
__googleapis_http \
|
||||
"${OOM_CLIENT_ID}" \
|
||||
"${OOM_CLIENT_SECRET}" \
|
||||
"${OOM_YOUTUBE_REFRESH_TOKEN}" \
|
||||
GET /youtube/v3/subscriptions \
|
||||
part=snippet mine=true maxResults=50
|
||||
}
|
||||
|
||||
main "$@"
|
31
youtube-api-poking/refresh-token
Executable file
31
youtube-api-poking/refresh-token
Executable file
@ -0,0 +1,31 @@
|
||||
#!/usr/bin/env bash
|
||||
set -o errexit
|
||||
|
||||
main() {
|
||||
[[ "${OOM_YOUTUBE_ACCESS_TOKEN}" ]] || {
|
||||
echo "Missing \$OOM_YOUTUBE_ACCESS_TOKEN"
|
||||
exit 1
|
||||
}
|
||||
|
||||
[[ "${OOM_CLIENT_SECRET}" ]] || {
|
||||
echo "Missing \$OOM_CLIENT_SECRET"
|
||||
exit 1
|
||||
}
|
||||
|
||||
[[ "${OOM_CLIENT_ID}" ]] || {
|
||||
echo "Missing \$OOM_CLIENT_ID"
|
||||
exit 1
|
||||
}
|
||||
|
||||
curl \
|
||||
-vvvv \
|
||||
-X POST \
|
||||
-sSL \
|
||||
-d client_id="${OOM_CLIENT_ID}" \
|
||||
-d client_secret="${OOM_CLIENT_SECRET}" \
|
||||
-d refresh_token="${OOM_YOUTUBE_ACCESS_TOKEN}" \
|
||||
-d grant_type=refresh_token \
|
||||
'https://accounts.google.com/o/oauth2/token'
|
||||
}
|
||||
|
||||
main "$@"
|
Loading…
Reference in New Issue
Block a user