0

我正在使用我剥离的一些功能获取 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()

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

4

1 回答 1

0

我就是这样做的。

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 数据库一起使用,但原理仍然相同。

于 2013-03-27T05:09:11.660 回答