đây là một kịch bản để tải lên một đoạn video qua 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"
để làm cho công việc này, bạn sẽ cần phải
kịch bản này được dựa trên khác question
này Hơn nữa có this github project mà giải quyết vấn đề với trăn ...
Đã thêm video vào câu trả lời của tôi bên dưới. –