Google API OAuth - 尝试上传到 YouTube 时收到 401(未经授权)
最近几天,我一直在努力尝试使用官方的 Python Wrapper 库将电影上传到 YouTube。
到目前为止我已经成功:
- 构建请求访问 URL
- 保存返回的 OAuth Secret 以供以后使用(获取实际的访问令牌)
- 获取访问令牌及其耦合的 Secret。将它们存储在数据库中。
我正在尝试但没有成功:
- 准备并上传视频 - 这总是给我带来:
{'status': 401, 'body': '令牌无效 - 无效的 AuthSub 令牌。', 'reason': '未经授权'}
下面是上述相关代码块的编译:
< strong>获取访问请求 URL
def GetOAuthToken():
callback_url = 'http://www.mysite.com/media_sites/oauth_access_token_callback/youtube'
scope = 'http://gdata.youtube.com'
secure = False
session = True
yt_service.developer_key = YOUTUBE_DEVELOPER_KEY
yt_service.client_id = YOUTUBE_KEY
yt_service.SetOAuthInputParameters(gdata.auth.OAuthSignatureMethod.HMAC_SHA1, YOUTUBE_KEY, consumer_secret=YOUTUBE_SECRET)
req_token = yt_service.FetchOAuthRequestToken(scopes=scope)
print 'secret: %s' % req_token.secret
yt_service.SetOAuthToken(req_token)
yt_service.SetOAuthToken(req_token)
return yt_service.GenerateOAuthAuthorizationURL(callback_url=callback_url, )
获取访问令牌
def handle_token():
scope = 'http://gdata.youtube.com'
yt_service = gdata.youtube.service.YouTubeService()
yt_service.developer_key = YOUTUBE_DEVELOPER_KEY
yt_service.client_id = YOUTUBE_KEY
yt_service.SetOAuthInputParameters(gdata.auth.OAuthSignatureMethod.HMAC_SHA1, YOUTUBE_KEY, consumer_secret=YOUTUBE_SECRET)
oauth_token = gdata.auth.OAuthTokenFromUrl('http://www.mysite.com/media_sites/oauth_access_token_callback/youtube?oauth_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
oauth_token.secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
if oauth_token:
oauth_token.oauth_input_params = gdata.auth.OAuthInputParams(gdata.auth.OAuthSignatureMethod.HMAC_SHA1, YOUTUBE_KEY, consumer_secret=YOUTUBE_SECRET)
yt_service.SetOAuthToken(oauth_token)
access_token = yt_service.UpgradeToOAuthAccessToken()
上传视频
def upload_vid(access_token, access_token_secret):
my_media_group = gdata.media.Group(
title=gdata.media.Title(text='My Test Movie'),
description=gdata.media.Description(description_type='plain',
text='My description'),
keywords=gdata.media.Keywords(text='cars, funny'),
category=[gdata.media.Category(
text='Autos',
scheme='http://gdata.youtube.com/schemas/2007/categories.cat',
label='Autos')],
player=None
)
yt_service = gdata.youtube.service.YouTubeService()
yt_service.developer_key = YOUTUBE_DEVELOPER_KEY
yt_service.client_id = YOUTUBE_KEY
yt_service.SetOAuthInputParameters(gdata.auth.OAuthSignatureMethod.HMAC_SHA1, YOUTUBE_KEY, consumer_secret=YOUTUBE_SECRET)
oauth_token = gdata.auth.OAuthToken(key=access_token, secret=access_token_secret, scopes='http://gdata.youtube.com')
oauth_token.oauth_input_params = gdata.auth.OAuthInputParams(gdata.auth.OAuthSignatureMethod.HMAC_SHA1, YOUTUBE_KEY, consumer_secret=YOUTUBE_SECRET)
yt_service.SetOAuthToken(oauth_token)
where = gdata.geo.Where()
where.set_location((37.0,-122.0))
video_entry = gdata.youtube.YouTubeVideoEntry(media=my_media_group, geo=where)
video_file_location = '/home/path/of/myfile.flv'
new_entry = yt_service.InsertVideoEntry(video_entry, video_file_location)
所以基本上每次我尝试上传电影时都会出现 401 错误。 任何帮助都会很棒!
The last days I've been banging my head against the wall trying to upload a movie to YouTube using its official Python Wrapper lib.
What I've succeeded till now:
- Building an Request Access URL
- Saving the returned OAuth Secret for a later use (getting the actual Access Token)
- Getting an Access Token and its coupled Secret. Stored them in a DB.
What I'm trying without success:
- Preparing and uploading a video - this always get me a:
{'status': 401, 'body': 'Token invalid - Invalid AuthSub token.', 'reason': 'Unauthorized'}
Below is a compilation of the relevant code blocks for the above:
Getting the Access Request URL
def GetOAuthToken():
callback_url = 'http://www.mysite.com/media_sites/oauth_access_token_callback/youtube'
scope = 'http://gdata.youtube.com'
secure = False
session = True
yt_service.developer_key = YOUTUBE_DEVELOPER_KEY
yt_service.client_id = YOUTUBE_KEY
yt_service.SetOAuthInputParameters(gdata.auth.OAuthSignatureMethod.HMAC_SHA1, YOUTUBE_KEY, consumer_secret=YOUTUBE_SECRET)
req_token = yt_service.FetchOAuthRequestToken(scopes=scope)
print 'secret: %s' % req_token.secret
yt_service.SetOAuthToken(req_token)
yt_service.SetOAuthToken(req_token)
return yt_service.GenerateOAuthAuthorizationURL(callback_url=callback_url, )
Getting the Access Token
def handle_token():
scope = 'http://gdata.youtube.com'
yt_service = gdata.youtube.service.YouTubeService()
yt_service.developer_key = YOUTUBE_DEVELOPER_KEY
yt_service.client_id = YOUTUBE_KEY
yt_service.SetOAuthInputParameters(gdata.auth.OAuthSignatureMethod.HMAC_SHA1, YOUTUBE_KEY, consumer_secret=YOUTUBE_SECRET)
oauth_token = gdata.auth.OAuthTokenFromUrl('http://www.mysite.com/media_sites/oauth_access_token_callback/youtube?oauth_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
oauth_token.secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
if oauth_token:
oauth_token.oauth_input_params = gdata.auth.OAuthInputParams(gdata.auth.OAuthSignatureMethod.HMAC_SHA1, YOUTUBE_KEY, consumer_secret=YOUTUBE_SECRET)
yt_service.SetOAuthToken(oauth_token)
access_token = yt_service.UpgradeToOAuthAccessToken()
Uploading The Video
def upload_vid(access_token, access_token_secret):
my_media_group = gdata.media.Group(
title=gdata.media.Title(text='My Test Movie'),
description=gdata.media.Description(description_type='plain',
text='My description'),
keywords=gdata.media.Keywords(text='cars, funny'),
category=[gdata.media.Category(
text='Autos',
scheme='http://gdata.youtube.com/schemas/2007/categories.cat',
label='Autos')],
player=None
)
yt_service = gdata.youtube.service.YouTubeService()
yt_service.developer_key = YOUTUBE_DEVELOPER_KEY
yt_service.client_id = YOUTUBE_KEY
yt_service.SetOAuthInputParameters(gdata.auth.OAuthSignatureMethod.HMAC_SHA1, YOUTUBE_KEY, consumer_secret=YOUTUBE_SECRET)
oauth_token = gdata.auth.OAuthToken(key=access_token, secret=access_token_secret, scopes='http://gdata.youtube.com')
oauth_token.oauth_input_params = gdata.auth.OAuthInputParams(gdata.auth.OAuthSignatureMethod.HMAC_SHA1, YOUTUBE_KEY, consumer_secret=YOUTUBE_SECRET)
yt_service.SetOAuthToken(oauth_token)
where = gdata.geo.Where()
where.set_location((37.0,-122.0))
video_entry = gdata.youtube.YouTubeVideoEntry(media=my_media_group, geo=where)
video_file_location = '/home/path/of/myfile.flv'
new_entry = yt_service.InsertVideoEntry(video_entry, video_file_location)
So basically this gives me a 401 every time I try to upload a movie.
Any help will be great!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最后解决了。
就我而言,它正在将转义令牌发送到需要原始字符串的方法。
只需确保在将令牌作为参数发送之前“未转义”即可。
googles OAuth 令牌可能包含转义为 %2F 的斜线
发送这样的令牌将带您进入 401
I solved it at the end.
On my case it was sending an escaped token to a method which expect a raw string.
Just make sure you have your token "not escaped" prior to sending them as parameters.
googles OAuth tokens may include a slash which escapes as %2F
sending such a token will bring you to a 401