65 lines
1.2 KiB
Bash
65 lines
1.2 KiB
Bash
#!/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 .
|
|
}
|