使用YoutubeApi v3和ouath2将视频上传到我的Youtube频道,而无需用户身份验证


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

我的任务目标是创建一个控制台脚本,该脚本会将我自己网站上最近上传的视频插入我自己的Youtube频道。我想使用服务器到服务器身份验证,但 YoutubeAPI 现在不支持这种身份验证方式。

所以我的问题是:如何在没有任何用户帮助的情况下使用带有控制台脚本的 oauth2 身份验证将视频上传到 youtube 频道?有没有办法在不使用已弃用的客户端登录身份验证协议的情况下做到这一点?

是的,

本部分解释了如何: https://developers.google.com/youtube/v3/guides/moving_to_oauth#standalone

基本上,您通过一次并从那里保存令牌。

如果你甚至还想跳过一次,可以在 OAuth2 Playground 中获取具有受尊重作用域的刷新令牌,并使用客户端密码和 ID 将其直接插入到代码中。这样,您的脚本就不需要 Web 浏览器。

以下是逐步解释此工作流程的视频。

这是一个通过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"

要完成这项工作,您需要

  • 转到 https://console.developers.google.com/projectselector/apis/library 并单击"凭据"和"创建凭据"以获取client_id,client_secret并确保它是本机应用程序的"OAuth Client ID"(选择"其他"作为类型)
  • 启用"YouTube 数据 API v3"

此脚本基于另一个问题

此外,还有这个 github 项目解决了 python 的问题......

在与YoutubeAPI开发人员光盘之后,我们得到了这样的解决方案:如果不使用已弃用的 ** **客户端登录身份验证协议,就不可能创建自己的服务器到服务器应用程序它将在 2014 年 4 月完全弃用(但在 4 月之前您可以使用它)。

因此,如果您希望用户从您的网站将视频上传到您的 YT 频道,您应该采用这样的方案: - 您的用户将视频上传到您的网站 - 您(或拥有您的 YT 帐户凭据的其他人)将视频导入您的 YT 频道要解决此问题,您可以轻松使用 OAuth2 协议。

我已经能够使用以下shell脚本将视频上传到YouTube上的频道。

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

请参阅前面的答案,了解如何获取自定义变量值。

如果您想在没有登录的情况下上传,那么您必须首先将登录数据保存到 pickle 文件中,下次只需调用该 pickle 文件即可。它将包含您的频道身份验证数据。不要让泡菜文件公开

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

只需将此 youtube 变量保存到泡菜文件中即可。您可以在测试时在本地执行此操作。只需将此 pickle 文件保存到服务器中并调用其反序列化对象即可。