class OauthClient(object): def __init__(self, client_id, client_secret, redirect_uri = 'oob'): self.__client_id = client_id self.__client_secret = client_secret self.__redirect_uri = redirect_uri self.__domain = 'https://openapi.baidu.com/oauth/2.0/' self.__http_object = HttpObject() def get_authorize_url(self, state = '', scope = '', display = 'page'): ''' Get baidu oayuth2's authorization granting url. ''' return '%s%s?%s' % (self.__domain, 'authorize', self.__http_object.params_encode( response_type = 'code', client_id = self.__client_id, redirect_uri = self.__redirect_uri, scope = scope, display = display, state = state)) def get_access_token_by_authorization_code(self, code): ''' Get access token ifno by authorization code. ''' try: return self.__http_object.get('%s%s' % (self.__domain, 'token'), client_id = self.__client_id, client_secret = self.__client_secret, redirect_uri = self.__redirect_uri, code = code, grant_type = 'authorization_code') except OpenApiError as e: return e def get_access_token_by_client_credentials(self, scope = ''): ''' Get access token info by client credentials. ''' return self.__http_object.get('%s%s' % (self.__domain, 'token'), client_id = self.__client_id, client_secret = self.__client_secret, scope = scope, grant_type = 'client_credentials') def get_access_token_by_refresh_token(self, refresh_token, scope = ''): ''' Get access token info by authorization code. ''' return self.__http_object.get('%s%s' % (self.__domain, 'token'), client_id = self.__client_id, client_secret = self.__client_secret, refresh_token = refresh_token, scope = scope, grant_type = 'refresh_token')
class BaiduApiClient(object): def __init__(self): self.__http_object = HttpObject() def __call(self, url, http_type, **params): method = http_type.lower() try: if method == 'get': return self.__http_object.get(url, **params) else: return self.__http_object.post(url, ** params) except OpenApiError, e: return e
class BaiduNetdisk(object): def __init__(self, access_token): ''' Initialize by access token. ''' self.__access_token = str(access_token) self.__domain = 'https://pcs.baidu.com/rest/2.0/pcs/' self.__http_object = HttpObject() def quota(self): ''' Get user quota info. ''' try: return self.__http_object.get('%s%s' % (self.__domain, 'quota'), method = 'info', access_token = self.__access_token ) except OpenApiError, e: return e