Skip to content

Instantly share code, notes, and snippets.

@leohmoraes
Forked from seanbehan/youtube.bash
Created May 12, 2020 04:09
Show Gist options
  • Save leohmoraes/06148bdbb4d305967b6f0d4ed5d5c9d1 to your computer and use it in GitHub Desktop.
Save leohmoraes/06148bdbb4d305967b6f0d4ed5d5c9d1 to your computer and use it in GitHub Desktop.
Upload videos to Youtube.com with bash
#!/bin/sh
#
# Bash script for uploading videos to Youtube.com
#
# Usage:
# ./youtube.bash path-to-movie-movie.mov
#
# Setup
# Goto https://console.developers.google.com
# Create app, enable Youtube Data API and create OAuth credentials, client id and client secret.
# Goto https://developers.google.com/oauthplayground/ to authorize yourself and get a refresh token
# Plug in client id, client secret and refresh token here
client_id="[your client id goes here]"
client_secret="[your client secret goes here]"
refresh_token="[your refresh token obtained from oauthplayground goes here]"
cid_base_url="apps.googleusercontent.com"
client_id="$client_id.$cid_base_url"
token_url="https://accounts.google.com/o/oauth2/token"
api_base_url="https://www.googleapis.com/upload/youtube/v3"
api_url="$api_base_url/videos?part=snippet"
access_token=$(curl -H "Content-Type: application/x-www-form-urlencoded" -d refresh_token="$refresh_token" -d client_id="$client_id" -d client_secret="$client_secret" -d grant_type="refresh_token" $token_url| awk -F '"' '/access/{print $4}')
auth_header="Authorization: Bearer $access_token"
curl -v --data-binary "@$1" -H "Content-Type: application/octet-stream" -H "$auth_header" "$api_url"
#!/bin/sh
#
# ./youtube.bash '{"json goes here as explained below"}'
#
# Slightly more complicated
#
# To update a Youtube video you have to PUT a JSON document with the following structure..
#
# {"id": "id of youtube video", "snippet": { "title": "goes here", "description": "goes here", "categoryId": "goes here"}, "status":{"privacyStatus":"unlisted"}}
json_data="$1"
client_id="goes here"
client_secret="goes here"
refresh_token="goes here"
cid_base_url="apps.googleusercontent.com"
client_id="$client_id.$cid_base_url"
token_url="https://accounts.google.com/o/oauth2/token"
api_base_url="https://www.googleapis.com/upload/youtube/v3"
api_url="$api_base_url/videos?part=snippet"
access_token=$(curl -H "Content-Type: application/x-www-form-urlencoded" -d refresh_token="$refresh_token" -d client_id="$client_id" -d client_secret="$client_secret" -d grant_type="refresh_token" $token_url| awk -F '"' '/access/{print $4}')
auth_header="Authorization: Bearer $access_token"
curl -v -i -X PUT -d "$json_data" -H "$auth_header" -H "Content-Type: application/json; charset=UTF-8" "https://www.googleapis.com/youtube/v3/videos?part=snippet,status&fields=snippet,status"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment