我正在获取一个 Oauth2 token ,其中包含我剥离的一些功能:
https://code.google.com/p/google-api-python-client/source/browse/#hg%2Foauth2client
我试过:
yt_service = gdata.youtube.service.YouTubeService()
yt_service.developer_key = YOUTUBE_DEV_KEY
yt_service.access_token = FRESH_OAUTH2_ACCESS_TOKEN
yt_service.client_id = YOUTUBE_OAUTH2_CLIENT_ID
yt_service.email = YOUTUBE_USER_EMAIL
yt_service.password = YOUTUBE_USER_PASSWORD
yt_service.source = YOUTUBE_DEV_SRC
yt_service.ProgrammaticLogin()
但我不确定如何正确接听电话
GetFormUploadToken
或 UpdateVideoEntry
.以前我只是使用 developer_key
它正在工作(使用 gdata.youtube.service.YouTubeService()
)。我也尝试使用这个例子,但它没有得到很好的评论,文档也没有任何好转:
https://code.google.com/p/youtube-api-samples/source/browse/samples/python/update_video.py
我试着简单地改变
build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
http=credentials.authorize(httplib2.Http()))
到
build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
ACCESS_TOKEN=current_access_token)
但它只是提示它不知道
ACCESS_TOKEN
是。
最佳答案
我就是这样做的。
def _yt_oauth_flow_hack(self, secret_json_str, scope, redirect_uri=None):
"""
A hacked version of: oauth2client.clientflow_from_clientsecrets
"""
client_type, client_info = clientsecrets.loads(secret_json_str)
if client_type in [clientsecrets.TYPE_WEB, clientsecrets.TYPE_INSTALLED]:
return OAuth2WebServerFlow(
client_info['client_id'],
client_info['client_secret'],
scope,
redirect_uri=redirect_uri,
user_agent=None,
auth_uri=client_info['auth_uri'],
token_uri=client_info['token_uri']
)
def begin_authentication(self):
'''Starts the authentication process and returns the URL that we have to use to authorize the content
if we need to authorize it, otherwise will generate the service and return None'''
credentials = self._storage.get()
if credentials is None:
print 'Credentials Not Stored - We Require Fresh Authentication'
flow = self._yt_oauth_flow_hack(AUTH_JSON, self.scope)
flow.redirect_uri = OOB_CALLBACK_URN
return flow.step1_get_authorize_url()
elif (credentials.invalid or credentials.token_expiry <= (datetime.now() + timedelta(seconds=time.timezone))):
print 'Credentials are Expired - Going to attempt to refresh them'
try:
http = httplib2.Http()
credentials.refresh(http)
print 'Success!'
except AccessTokenRefreshError:
print 'Unable to refresh credentials - requesting new ones.'
flow = self._yt_oauth_flow_hack(AUTH_JSON, self.scope)
flow.redirect_uri = OOB_CALLBACK_URN
return flow.step1_get_authorize_url()
print 'Credentials ->', credentials
http = httplib2.Http()
http = credentials.authorize(http)
self.service = build('youtube', 'v3', http=http)
return None
AUTH_JSON 是我在设置 API 帐户时下载的。存储是其存储的定制版本,可与 postgres 数据库一起使用,但原理仍然相同。
关于python - 如何在任何版本的 youtube python API 中使用 OAuth2 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15651985/