了解为什么通过 curl 上传视频会返回身份验证错误 [英] Understand why uploading video via curl returns an authentication error

查看:46
本文介绍了了解为什么通过 curl 上传视频会返回身份验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 API 上传到 YouTube.我使用 curl 执行此操作,当我运行以下命令时,我收到一条错误消息,指出并非未经身份验证,即使我已填写了身份验证详细信息.

我的代码是:

curl -XPOST -H 'client_id: CLIENT_ID' -H 'client_secret: CLIENT_SECRET' -H 'auth_uri: https://accounts.google.com/o/oauth2/auth' -H 'token_uri: https://oauth2.googleapis.com/token' -H "Content-type: application/json";-d'{片段":{标题":测试标题",描述":测试描述",标签":[测试",标签"],类别 ID":20},状态":{隐私状态":未公开"}}' 'https://www.googleapis.com/upload/youtube/v3/videos'

错误信息是401:

<块引用>

请求缺少必需的身份验证凭据.预期的 OAuth 2 访问令牌、登录 cookie 或其他有效的身份验证凭据.请参阅 https://developers.google.com/identity/sign-in/web/devconsole-project.

我 100% 正确输入了 client_idclient_secret.

解决方案

不幸的是,@Mac Hooper,您的代码受到了很多问题的影响.请耐心等待:

  1. 您必须承认视频上传(即使用 Videos.insert API 端点)必须经过适当授权:

<块引用>

授权
此请求需要至少具有以下范围之一的授权(阅读有关身份验证和授权的更多信息).

范围
https://www.googleapis.com/auth/youtube.upload
https://www.googleapis.com/auth/youtube
https://www.googleapis.com/auth/youtubepartner
https://www.googleapis.com/auth/youtube.force-ssl

  1. 使用 YouTube 数据 API 上传视频并不能像您尝试的那样简单.虽然没有正式记录,但 API 支持两种上传过程:

    一个.可恢复上传,以及
    湾一次性上传.

可恢复上传是通过官方记录的可恢复上传协议.

无论实现语言/环境如何,可恢复上传协议都很难正确实现.Google 提供了为大量语言/环境实现协议的开源代码.虽然它是可以实现的,但我建议不要使用 curl 在 shell 脚本中实现这个协议.

另一方面,如果希望每次上传的视频只发出一个 curl 调用(不包括获得访问令牌).但是那个 curl 调用需要事先准备一些东西.

我的这个答案 指出了在使用 curl 时要采用的路径一次性上传.

基本上,您的 curl 调用将如下所示:

$ curl \--data-binary "@$MULTIPART_RELATED";\--header "Content-Type: $CONTENT_TYPE";\--header "Authorization: Bearer $ACCESS_TOKEN";\'https://www.googleapis.com/upload/youtube/v3/videos?uploadType=multipart&part=snippet,status'

(请注意,上面我假设在 bash 命令行提示符下发出 curl.在 Windows 命令行提示符下,事情类似,但上面的 curl 命令应该适当调整.)

上面的 curl 命令有三个参数:$MULTIPART_RELATED$CONTENT_TYPE$ACCESS_TOKEN.后者是您的凭据数据,它会传递到 YouTube 的服务器,这样 您的 API 调用已获得授权.

请阅读获取 OAuth 2.0 访问令牌 官方文档 OAuth 2.0 for Mobile &桌面应用,了解如何在成功完成 OAuth 2 身份验证/授权流程后获取有效(未过期)的访问令牌.

参数 $MULTIPART_RELATED 是您应该在发出实际 curl 调用之前(以编程方式)编写的文件的名称.该文件的格式并未正式记录在案,而是通过检查 Google 自己的开源代码得出的.

参数 $CONTENT_TYPE 是一个字符串,它指定了 Content-通过curl 发出的HTTP POST 方法所需的类型 标头.

再次,我的回答 SO 问题 如何使用 google api 将视频上传到 youtube.没有库提供了能够正确生成文件$MULTIPART_RELATED和字符串$CONTENT_TYPE的所有细节.

I am trying to upload to YouTube via the API. I am doing so with curl and when am running following command I receive an error saying that am not unauthenticated, even though I have filled out the authentication details.

My code is:

curl -XPOST -H 'client_id: CLIENT_ID' -H 'client_secret: CLIENT_SECRET' -H 'auth_uri: https://accounts.google.com/o/oauth2/auth' -H 'token_uri: https://oauth2.googleapis.com/token' -H "Content-type: application/json" -d '{
    "snippet": {
        "title": "Test Title",
        "description": "Test description",
        "tags": ["test", "tags"],
        "categoryId": 20
    },
    "status": {
        "privacyStatus": "unlisted"
    }
}' 'https://www.googleapis.com/upload/youtube/v3/videos'

Error message is 401:

Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.

I have 100% put in the client_id and client_secret correctly.

解决方案

Unfortunately, @Mac Hooper, your code suffers very badly from a whole lot of issues. Please bear with me for a while:

  1. You have to acknowledge that video uploading (that is using the Videos.insert API endpoint) has to be properly authorized:

Authorization
This request requires authorization with at least one of the following scopes (read more about authentication and authorization).

Scope
https://www.googleapis.com/auth/youtube.upload
https://www.googleapis.com/auth/youtube
https://www.googleapis.com/auth/youtubepartner
https://www.googleapis.com/auth/youtube.force-ssl

  1. Uploading videos using the YouTube Data API cannot be done as simple as you're trying to do it. Although not officially documented as such, the API supports two kinds of uploading procedures:

    a. Resumable uploads, and
    b. One-go uploads.

The resumable uploads are achieved by means of the officially documented Resumable Uploading Protocol.

The resumable uploading protocol is quite tricky to correctly be implemented regardless of the implementation language/environment. Google provides open-source code that implements the protocol for a plethora of languages/environments. Although it is achievable, I'd recommend against implementing this protocol in shell scripts using curl.

On the other hand, uploading videos in one go is what you'll have to do if expecting to issue only one curl call per video upload (excluding the calls implied by the procedure of obtaining an access token). But that curl call requires certain things to be prepared beforehand.

There's this answer of mine that indicated the path to be taken when employing curl in one-go uploads.

Basically, your curl call would look like the one below:

$ curl \
--data-binary "@$MULTIPART_RELATED" \
--header "Content-Type: $CONTENT_TYPE" \
--header "Authorization: Bearer $ACCESS_TOKEN" \
'https://www.googleapis.com/upload/youtube/v3/videos?uploadType=multipart&part=snippet,status'

(Do note that above I presume issuing curl at a bash command line prompt. When being at a Windows command line prompt things work similarly, but the above curl command should properly be adapted.)

The curl command above has three parameters: $MULTIPART_RELATED, $CONTENT_TYPE and $ACCESS_TOKEN. The latter is your credentials data that gets passed on to YouTube's server such that your API call be authorized.

Do read the section Obtaining OAuth 2.0 access tokens of the official document OAuth 2.0 for Mobile & Desktop Apps for to see how to obtain a valid (non-expired) access token upon completing successfully an OAuth 2 authentication/authorization flow.

The parameter $MULTIPART_RELATED is the name of the file you should have composed (programmatically) before issuing the actual curl call. The format of this file is not documented officially as such, but is derived upon inspecting Google's own open-source code.

The parameter $CONTENT_TYPE is a string that specifies the Content-Type header needed by the HTTP POST method to be issued via curl.

Again, my answer to the SO question How to upload video to youtube using google api. Without libraries provides all the details for one to be able to generate properly the file $MULTIPART_RELATED and the string $CONTENT_TYPE.

这篇关于了解为什么通过 curl 上传视频会返回身份验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆