如何在任何版本的youtube python API中使用OAuth2令牌

2024-04-19 15:16:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在获取一个Oauth2令牌,其中包含我从中剥离的一些函数: 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()

{{cdm>如何正确调用。以前我只使用了developer_key,它正在工作(使用gdata.youtube.service.YouTubeService())。在

我也试过用这个例子,但是它的注释不是很好,文档也没有任何改进: https://code.google.com/p/youtube-api-samples/source/browse/samples/python/update_video.py

我试着换衣服 ^{cd5}

build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, ACCESS_TOKEN=current_access_token)

但它只是抱怨它不知道ACCESS_TOKEN是什么。在


Tags: httpscomclienttokenapisourceaccessyoutube
1条回答
网友
1楼 · 发布于 2024-04-19 15:16:21

我就是这样做的。在

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数据库一起工作,但是原理仍然是一样的。在

相关问题 更多 >