2013-10-18 12 views
12

L'obiettivo del mio compito è quello di creare uno script console, che inserirà i video caricati di recente sul mio sito sul mio canale Youtube. Voglio usare l'autenticazione server-to-server ma YoutubeApi non supporta questo modo di autenticazione ora.Carica video sul mio canale Youtube senza autenticazione utente utilizzando YoutubeApi v3 e ouath2

Quindi la mia domanda è: come posso caricare video sul canale youtube, utilizzando l'autenticazione oauth2 con lo script della console senza l'aiuto di un utente? C'è un modo per farlo senza usare il protocollo di autenticazione ClientLogin deprecato?

+0

Aggiunto video alla mia risposta qui sotto. –

risposta

9

Sì questo segmento spiega come: https://developers.google.com/youtube/v3/guides/moving_to_oauth#standalone

In sostanza, si passare attraverso una volta e salvare il token da lì.

Se si desidera saltare anche quella volta, è possibile ottenere un token di aggiornamento in OAuth2 Playground con gli ambiti rispettati e collegarlo direttamente nel codice, con client secret e id. In questo modo il tuo script non avrà bisogno di un browser web.

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

+0

Grazie per la tua risposta, cercherò di utilizzare OAuth2 Playground e spero che ti sia d'aiuto. Ti dico piuttosto che la tua soluzione mi aiuta o no. –

+0

Grazie per la soluzione. –

+0

Aggiunto video alla mia risposta. –

1

Dopo disco con YoutubeAPI sviluppatore, abbiamo ottenuto tale soluzione: NON SI PUO FARE la propria applicazione server-to-server senza utilizzare deprecato ** ** ClientLogin Auth protocollo Sarà completamente deprecato su aprile 2014 (ma fino ad aprile è possibile utilizzarlo).

Quindi, se si desidera che gli utenti di caricare i video nel tuo canale di YouTube dal tuo sito, si dovrebbe lavorare in programma in questo modo: - I suoi utenti di caricare video al tuo sito - Tu (o qualcun altro che ha il tuo YT credenziali dell'account) importare video sul canale YT Per risolvere questo problema è possibile utilizzare facilmente il protocollo OAuth2.

+1

sebbene accettato come risposta corretta. Questo è sbagliato! – pinoyyid

+0

@pinoyyid puoi provarlo con i link? –

+0

sì. la risposta sopra con 4 upvotes – pinoyyid

1

qui è uno script per caricare un video tramite ricciolo

# 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" 

Per fare questo lavoro è necessario

questo script si basa su questo altro question

Inoltre c'è this github project che affrontano il problema con pitone ...

0

sono stato in grado di caricare un video sul mio canale su YouTube utilizzando il seguendo lo script di shell.

#!/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" 

Fare riferimento alla risposta precedente per come ottenere i valori delle variabili personalizzate.

Problemi correlati