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

时间:2013-10-18 11:55:21

标签: php youtube-api google-api oauth-2.0 console-application

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

所以我的问题是:如何在没有用户帮助的情况下使用oauth2身份验证和控制台脚本将视频上传到youtube频道?有没有办法在不使用已弃用的ClientLogin身份验证协议的情况下执行此操作?

5 个答案:

答案 0 :(得分:10)

是的,此细分说明如何:https://developers.google.com/youtube/v3/guides/moving_to_oauth#standalone

基本上,你经历过一次并从那里保存令牌。

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

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

答案 1 :(得分:1)

在使用YoutubeAPI开发者的光盘后,我们得到了这样的解决方案: 在不使用弃用的情况下自行完成服务器到服务器应用程序是不可能的** ** ClientLogin Auth Protocol 它将在 2014年4月完全弃用(但在4月之前您可以使用它)。

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

答案 2 :(得分:1)

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

要完成这项工作,您需要

此脚本基于此其他question

此外还有this github project解决了python ...

的问题

答案 3 :(得分:0)

我可以使用以下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"

有关如何获取自定义变量值的信息,请参阅上一个答案。

答案 4 :(得分:0)

如果要上载而不登录,则必须先将登录数据保存到pickle文件中,下次再调用该pickle文件。它将包含您的频道身份验证数据。 请勿公开公开泡菜文件

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

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