OAuth 和 YouTube API

发布于 2024-11-25 02:26:01 字数 2074 浏览 3 评论 0原文

我正在尝试通过 OAuth 使用 YouTube 服务。我已经能够获取请求令牌、对其进行授权并将其转换为访问令牌。

现在我正在尝试使用这些令牌来实际向 YouTube 服务发出请求。例如,我正在尝试将视频添加到播放列表中。 发出 POST 请求

因此,我向https://gdata.youtube.com/feeds/api /playlists/XXXXXXXXXXXX

发送主体

<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
    xmlns:yt="http://gdata.youtube.com/schemas/2007">
  <id>XXXXXXXXX</id>
</entry>

和 以及标头

Gdata-version: 2
Content-type: application/atom+xml
Authorization: OAuth oauth_consumer_key="www.xxxxx.xx",
                                oauth_nonce="xxxxxxxxxxxxxxxxxxxxxxxxx",
                                oauth_signature="XXXXXXXXXXXXXXXXXXX",
                                oauth_signature_method="HMAC-SHA1",
                                oauth_timestamp="1310985770",
                                oauth_token="1%2FXXXXXXXXXXXXXXXXXXXX",
                                oauth_version="1.0"
X-gdata-key: key="XXXXXXXXXXXXXXXXXXXXXXXXX"

以及一些标准标头(主机和内容长度),这些标头是在请求时由 urllib2 (我使用 Python)添加的。

不幸的是,我收到错误 401:未知授权标头,并且响应的标头

X-GData-User-Country: IT
WWW-Authenticate: GoogleLogin service="youtube",realm="https://www.google.com/youtube/accounts/ClientLogin"
Content-Type: text/html; charset=UTF-8
Content-Length: 179
Date: Mon, 18 Jul 2011 10:42:50 GMT
Expires: Mon, 18 Jul 2011 10:42:50 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Connection: close

特别是我不知道如何解释 WWW-Authenticate 标头,其领域提示 ClientLogin。

我还尝试使用 OAuth Playground ,该网站发送的授权标头看起来完全像我的,除了字段的顺序。尽管如此,在游乐场上一切正常。好吧,差不多了:我收到一条错误,提示缺少开发者密钥,但这是合理的,因为无法在操场上添加开发者密钥。尽管如此,我还是跳过了错误 401。

我还尝试从那里手动复制授权标头,但我收到了错误 400:错误请求

我做错了什么?

I am trying to use the YouTube services with OAuth. I have been able to obtain request tokens, authorize them and transform them into access tokens.

Now I am trying to use those tokens to actually do requests to the YouTube services. For instance I am trying to add a video to a playlist. Hence I am making a POST request to

https://gdata.youtube.com/feeds/api/playlists/XXXXXXXXXXXX

sending a body of

<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
    xmlns:yt="http://gdata.youtube.com/schemas/2007">
  <id>XXXXXXXXX</id>
</entry>

and with the headers

Gdata-version: 2
Content-type: application/atom+xml
Authorization: OAuth oauth_consumer_key="www.xxxxx.xx",
                                oauth_nonce="xxxxxxxxxxxxxxxxxxxxxxxxx",
                                oauth_signature="XXXXXXXXXXXXXXXXXXX",
                                oauth_signature_method="HMAC-SHA1",
                                oauth_timestamp="1310985770",
                                oauth_token="1%2FXXXXXXXXXXXXXXXXXXXX",
                                oauth_version="1.0"
X-gdata-key: key="XXXXXXXXXXXXXXXXXXXXXXXXX"

plus some standard headers (Host and Content-Length) which are added by urllib2 (I am using Python) at the moment of the request.

Unfortunately, I get an Error 401: Unknown authorization header, and the headers of the response are

X-GData-User-Country: IT
WWW-Authenticate: GoogleLogin service="youtube",realm="https://www.google.com/youtube/accounts/ClientLogin"
Content-Type: text/html; charset=UTF-8
Content-Length: 179
Date: Mon, 18 Jul 2011 10:42:50 GMT
Expires: Mon, 18 Jul 2011 10:42:50 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Connection: close

In particular I do not know how to interpret the WWW-Authenticate header, whose realm hints to ClientLogin.

I have also tried to play with the OAuth Playground and the Authorization header sent by that site looks exactly like mine, except for the order of the fields. Still, on the plyaground everything works. Well, almost: I get an error telling that a Developer key is missing, but that is reasonable since there is no way to add one on the playground. Still, I go past the Error 401.

I have also tried to manually copy the Authorization header from there, and I got an Error 400: Bad request.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往昔成烟 2024-12-02 02:26:01

结果发现问题出在 xmlns:yt 之前的换行符。我能够按照此处的建议使用 ncat 进行调试,并检查完整响应。

Turns out the problem was the newline before xmlns:yt. I was able to debug this using ncat, as suggeested here, and inspecting the full response.

差↓一点笑了 2024-12-02 02:26:01

我建议使用 oauth python 模块,因为它更简单并且可以处理 auth 标头:) https: //github.com/simplegeo/python-oauth2,作为一个解决方案,我建议你用 'utf-8' 编码你的参数,我有一个类似的问题,解决方案是谷歌期待utf-8编码的字符串

i would suggest using the oauth python module, because it much more simple and takes care of the auth headers :) https://github.com/simplegeo/python-oauth2, as a solution i suggest you encode your parameters with 'utf-8' , i had a similar problem, and the solution was that google was expecting utf-8 encoded strings

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文