python - 如何在任何版本的 youtube python API 中使用 OAuth2 token

标签 python youtube oauth-2.0

我正在获取一个 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()

但我不确定如何正确接听电话 GetFormUploadTokenUpdateVideoEntry .以前我只是使用 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/

相关文章:

Python 将 2d 数据绘制到 3d 轴上

python - 将 pandas 数据框保存为不带 NaN 的单独 json

python - 使用标题词作为键从 .CSV 文件创建词典列表

python - 在 Django/python 中,如何将内存缓存设置为无限时间?

iphone - 在 iPhone 上嵌入 YouTube 视频时出现问题

oauth-2.0 - 从 google calendar api v2 迁移到 v3 - 客户端授权

spring - 在 Spring Security OAuth2(提供者)中使用作用域作为角色

youtube - 删除视频后可以重新发布YouTube ID吗?

api - 在YouTube API v3中无法获取视频时长

ios - YouTube OAuth2授权在重新授权时失败