Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload videos to my Youtube channel without user authentication using YoutubeApi v3 and ouath2

The goal of my task is to create a console script, which will insert recently uploaded videos on my own site to my own Youtube channel. I want to use server-to-server authentication but YoutubeApi does not support this way of authentication now.

So my question is: How could I upload video to youtube channel, using oauth2 authentication with console script without any help of a user? Is there any way to do this without using deprecated ClientLogin authentication protocol?

like image 349
Yehor Herasymchuk Avatar asked Oct 18 '13 11:10

Yehor Herasymchuk


2 Answers

Yes this segment explains how to: https://developers.google.com/youtube/v3/guides/moving_to_oauth#standalone

Basically, you go through once and save the token from there.

If you even want to skip that one time as well, you can get a refresh token in OAuth2 Playground with respected scopes and plug it in directly in your code, with client secret and id. That way your script won't need a web browser.

Here's the video explaining this workflow step-by-step.

like image 66
Ibrahim Ulukaya Avatar answered Sep 28 '22 04:09

Ibrahim Ulukaya


here is a script to upload a video via curl

# WARNING, this works only with GNU grep, if you run this on a mac replace grep with ggrep after 'brew install grep'
# Store our credentials in our home directory with a file called .<script name>
my_creds=.`basename $0`
client_id='YOURCLIENTID'
client_secret='YOURCLIENTSECRET' # really a secret
if [ -s $my_creds ]; then
  # if we already have a token stored, use it
  . $my_creds
  time_now=`date +%s`
else
  scope='https://www.googleapis.com/auth/youtube'

  # Form the request URL
  auth_url="https://accounts.google.com/o/oauth2/auth?client_id=$client_id&scope=$scope&response_type=code&redirect_uri=urn:ietf:wg:oauth:2.0:oob"

  echo "Please go to:"
  echo
  echo "$auth_url"
  echo
  echo "after accepting, enter the code you are given:"
  read auth_code

  # swap authorization code for access and refresh tokens
  auth_result=$(curl -s https://accounts.google.com/o/oauth2/token \
    -H "Content-Type: application/x-www-form-urlencoded" \
    -d code=$auth_code \
    -d client_id=$client_id \
    -d client_secret=$client_secret \
    -d redirect_uri=urn:ietf:wg:oauth:2.0:oob \
    -d grant_type=authorization_code)

  echo COMPLETE ANSWER WAS:
  echo $auth_result

  access_token=$(echo "$auth_result" | \
                 ggrep -Po '"access_token" *: *.*?[^\\]",' | \
                 awk -F'"' '{ print $4 }')
  refresh_token=$(echo "$auth_result" | \
                 ggrep -Po '"refresh_token" *: *.*?[^\\]",*' | \
                 awk -F'"' '{ print $4 }')
  expires_in=$(echo "$auth_result" | \
               ggrep -Po '"expires_in" *: *.*' | \
               awk -F' ' '{ print $3 }' | awk -F',' '{ print $1}')
  time_now=`date +%s`
  expires_at=$((time_now + expires_in - 60))
  echo "access_token=$access_token\nrefresh_token=$refresh_token\nexpires_at=$expires_at" > $my_creds
fi

# if our access token is expired, use the refresh token to get a new one
if [ $time_now -gt $expires_at ]; then
  refresh_result=$(curl -s https://accounts.google.com/o/oauth2/token \
   -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)
  access_token=$(echo "$refresh_result" | \
                 ggrep -Po '"access_token" *: *.*?[^\\]",' | \
                 awk -F'"' '{ print $4 }')
  expires_in=$(echo "$refresh_result" | \
               ggrep -Po '"expires_in" *: *.*' | \
               awk -F' ' '{ print $3 }' | awk -F',' '{ print $1 }')
  time_now=`date +%s`
  expires_at=$(($time_now + $expires_in - 60))
  echo "access_token=$access_token\nrefresh_token=$refresh_token\nexpires_at=$expires_at" > $my_creds
fi


# finally this is the call to upload the video (but I haven't managed to set title and description, you might want to make another call for that)
curl https://www.googleapis.com/upload/youtube/v3/videos?part=snippet \
    -d part='snippet' \
    -d snippet.title='test of a title' \
    -d snippet.description='test of video description' \
    --data-binary "@./small.mp4" \
    -H "Content-Type: application/octet-stream" \
    -H "Authorization: Bearer $access_token"

to make this work you will need to

  • go to https://console.developers.google.com/projectselector/apis/library and click 'credentials' and the 'create credentials' to get your client_id and client_secret make sure it is a 'oauth Client ID' for native application (select 'Other' as the type)
  • enable 'YouTube Data API v3'

this script is based on this other question

Moreover there is this github project which address the problem with python...

like image 39
Edoardo Avatar answered Sep 28 '22 03:09

Edoardo