Yehor Herasymchuk
Yehor Herasymchuk

Reputation: 439

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?

Upvotes: 15

Views: 9178

Answers (5)

Nitish Srivastava
Nitish Srivastava

Reputation: 1

If you want to upload without login then you have to first save login data into pickle file and next time just call that pickle file. It will contain your channel authentication data. DO NOT LET pickle file public.

youtube = googleapiclient.discovery.buil(...)

Just save this youtube variable into a pickle file. You can do it locally while testing. Just save this pickle file into the server and call its deserialize object.

Upvotes: 0

dardo82
dardo82

Reputation: 77

I have been able to upload a video to my channel on YouTube using the following shell script.

#!/bin/sh

# Upload the given video file to your YouTube channel.

cid_base_url="apps.googleusercontent.com"
client_id="<YOUR_CLIENT_ID>.$cid_base_url"
client_secret="<YOUR_CLIENT_SECRET>"
refresh_token="<YOUR_REFRESH_TOKEN>"
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?uploadType=resumable&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"
upload_url=$(curl -I -X POST -H "$auth_header" "$api_url"|awk -F ' |\r'  '/loc/{print $2}'); curl -v -X POST --data-binary "@$1" -H "$auth_header" "$upload_url"

Refer to the previous answer for how to get your custom variable values.

Upvotes: 0

Edoardo
Edoardo

Reputation: 5492

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

this script is based on this other question

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

Upvotes: 3

Yehor Herasymchuk
Yehor Herasymchuk

Reputation: 439

After disc with YoutubeAPI developer, we got such solution: IT IS IMPOSSIBLE TO DO YOUR OWN SERVER-TO-SERVER APPLICATION WITHOUT USING DEPRECATED ** **ClientLogin Auth Protocol It will be fully deprecated on April, 2014 (but until April you can use it).

So, if you want your users to upload videos into your YT channel from your site, you should work in scheme like this: - Your users upload videos to your site - You (or somebody else who has your YT account credentials) import video to your YT channel To resolve this you can easily use OAuth2 Protocol.

Upvotes: 1

Ibrahim Ulukaya
Ibrahim Ulukaya

Reputation: 12877

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.

Upvotes: 11

Related Questions