1

我目前正在将 Youtube API 用于 C++ 中的桌面应用程序。我正在尝试实现需要身份验证的直接上传。我自然选择了 OAuth 2.0,我按照Google 的示例进行操作,显然一切正常,我有一个访问令牌和一个刷新令牌,没有返回任何错误。

但是,当我尝试使用访问令牌上传视频(我把它放在Authorization : Bearer标题中)时,我收到了一个错误401 : Unauthorized的描述Token Invalid

然后我尝试在请求上传之前刷新访问令牌(这意味着我在检索访问令牌后尝试刷新,因为这两个操作在我的应用程序流程中是连续的)。访问令牌保持不变:我从刷新请求和交换请求中收到了相同的访问令牌

我首先认为这意味着访问令牌仅在过期时才应刷新,但这显然不是真的:使用OAuth 2.0 Playground,很明显刷新尚未过期的令牌可以正常工作并提供不同的访问令牌。

关于问题可能是什么的任何想法?401 错误是否与我无法刷新令牌有关?

编辑:这是 Fiddler 中所示的请求和响应

要求 :

POST http://uploads.gdata.youtube.com/feeds/api/users/default/uploads HTTP/1.1
Accept: */*
Accept-Language: xx
Authorization: Bearer MY_ACCESS_TOKEN
GData-Version: 2
X-GData-Key: key=MY_DEV_KEY
Slug: test.avi
Content-Type: multipart/related; boundary="f93dcbA3"
Pragma: no-cache
User-Agent: SOME_STUFF
Host: uploads.gdata.youtube.com
Content-Length: 23686
Connection: Keep-Alive

--f93dcbA3
Content-Type: application/atom+xml; charset=UTF-8

<?xml version="1.0"?><entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007"><media:group><media:title type="plain">Bad Wedding Toast</media:title><media:description type="plain">I gave a bad toast at my friend's wedding.</media:description><media:category scheme="http://gdata.youtube.com/schemas/2007/categories.cat">People</media:category><media:keywords>toast, wedding</media:keywords></media:group></entry>
--f93dcbA3
Content-Type: video/avi
Content-Transfer-Encoding: binary

<My file binary data>
--f93dcbA3--

回复

HTTP/1.1 401 Unauthorized
X-GData-User-Country: FR
WWW-Authenticate: Bearer realm="https://accounts.google.com/o/oauth2/auth",service="youtube"
Content-Type: text/html; charset=UTF-8
Content-Length: 13
X-GUploader-UploadID: AEnB2UrVDA94Fk5VFn1ng-2q9VFOo2KifLvIEHFOxQ4m66IUSC8sRf3mo5S8UH94mLyupbfANeLQvxMPhPLo6L0wlcaguQW9CQ
Date: Wed, 17 Jul 2013 09:51:23 GMT
Server: HTTP Upload Server Built on Jul 8 2013 15:32:26 (1373322746)

Token invalid

编辑 2:使用 Youtube API v3 请求和响应

要求 :

POST /upload/youtube/v3/videos?part="snippet" HTTP/1.1
Host: www.googleapis.com
X-gdata-key: DEV_KEY
Content-length: 42190
Content-type: multipart/related; boundary="===============1679429526=="
Authorization: ACCESS_TOKEN

--===============1679429526==
Content-type: application/json

{
    "snippet": 
    {
        "title": "test"
    }
}

--===============1679429526==
Content-type: video/avi

<BINARY DATA - 41984B>
--===============1679429526==--

回复 :

HTTP/1.1 400 Bad Request
Content-length: 229
Via: HTTP/1.1 GWA
X-google-cache-control: remote-fetch
Server: HTTP Upload Server Built on Jul 8 2013 15:32:26 (1373322746)
Date: Wed, 17 Jul 2013 22:14:03 GMT
Content-type: application/json

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "badContent",
    "message": "Unsupported content with type: video/avi"
   }
  ],
  "code": 400,
  "message": "Unsupported content with type: video/avi"
 }
}
4

0 回答 0