3
最近幾天,我一直在用我的頭撞牆,試圖使用官方的Python Wrapper lib將影片上傳到YouTube。Google API OAuth - 試圖上傳到YouTube時獲得401(未授權)
我已經成功了什麼至今:
- 構建一個請求訪問URL
- 保存返回的OAuth的祕訣以後使用(獲得實際的訪問令牌)
- 獲得一個訪問令牌和其耦合的祕密。將它們存儲在數據庫中。
我想什麼都沒有成功:
- 準備和上傳視頻的 - 這總是讓我:
{'status': 401, 'body': 'Token invalid - Invalid AuthSub token.', 'reason': 'Unauthorized'}
下面是相關的代碼塊的彙編以上:
獲取訪問請求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。 任何幫助將是偉大的!