class ApiClient: API_HOST = 'https://api.douban.com' AUTH_HOST = 'https://www.douban.com' TOKEN_URL = AUTH_HOST + '/service/auth2/token' AUTHORIZE_URL = AUTH_HOST + '/service/auth2/auth' ACCESS_TOKEN_FILE = "data/access_token.txt" DEFAULT_START = 0 DEFAULT_COUNT = 20 def __init__(self, key, secret, redirect='', scope=''): self.redirect_uri = redirect self.scope = scope self.client = Client(key, secret, site=self.API_HOST, authorize_url=self.AUTHORIZE_URL, token_url=self.TOKEN_URL) self.access_token = AccessToken(self.client, '') self.load_token() def __repr__(self): return '<ApiClient OAuth2>' def auth_with_code(self, code): self.access_token = self.client.auth_code.get_token(code, redirect_uri=self.redirect_uri) def auth_with_token(self, token): self.access_token = AccessToken(self.client, token) def auth_with_password(self, username, password, **opt): self.access_token = self.client.password.get_token(username=username, password=password, redirect_uri=self.redirect_uri, **opt) def refresh_token(self, refresh_token): access_token = AccessToken(self.client, token='', refresh_token=refresh_token) self.access_token = access_token.refresh() def me(self): return self.access_token.get("/v2/lifestream/user/~me") def user(self, id): return self.access_token.get("/v2/lifestream/user/%d" % id) def followings(self, id, count=DEFAULT_COUNT, start=DEFAULT_START): return self.access_token.get("/v2/lifestream/user/%d/followings" % id, count=count, start=start) def followers(self, id, count=DEFAULT_COUNT, start=DEFAULT_START): return self.access_token.get("/v2/lifestream/user/%d/followers" % id, count=count, start=start) @property def authorize_url(self): return self.client.auth_code.authorize_url(redirect_uri=self.redirect_uri, scope=self.scope) @property def token_code(self): return self.access_token and self.access_token.token @property def refresh_token_code(self): return getattr(self.access_token, 'refresh_token', None) def save_token(self): print "save_token:", self.token_code with open(self.ACCESS_TOKEN_FILE, "w") as f: f.write(self.token_code) f.flush() def load_token(self): if os.path.isfile(self.ACCESS_TOKEN_FILE): f = open(self.ACCESS_TOKEN_FILE, "r") self.access_token.token = f.read() print "load_token:", self.token_code
from pyoauth2 import Client from pyoauth2 import AccessToken KEY = '' SECRET = '' CALLBACK = '' token = '' client = Client(KEY, SECRET, site='https://api.douban.com', authorize_url='https://www.douban.com/service/auth2/auth', token_url='https://www.douban.com/service/auth2/token') access_token = AccessToken(client, token) print '-' * 80 ret = access_token.get('/people/%40me', alt='json') print ret.parsed
KEY = '' SECRET = '' CALLBACK = '' token = '' client = Client(KEY, SECRET, site='https://api.douban.com', authorize_url='https://www.douban.com/service/auth2/auth', token_url='https://www.douban.com/service/auth2/token') access_token = AccessToken(client, token) print '-' * 80 ret = access_token.get('/people/%40me', alt='json') print ret.parsed ########NEW FILE######## __FILENAME__ = github from pyoauth2 import Client KEY = '' SECRET = '' CALLBACK = '' client = Client(KEY, SECRET, site='https://api.github.com', authorize_url='https://github.com/login/oauth/authorize', token_url='https://github.com/login/oauth/access_token')
from pyoauth2 import Client from pyoauth2 import AccessToken KEY = '' SECRET = '' CALLBACK = '' client = Client(KEY, SECRET, site='https://api.weibo.com', authorize_url='/oauth2/authorize', token_url='/oauth2/access_token') code = raw_input( 'Enter the verification code and hit ENTER when you\'re done:') client.auth_code.get_token(code, redirect_uri=CALLBACK) access_token = AccessToken(client, code) print access_token.get('/2/statuses/public_timeline.json', access_token=access_token.token).parsed
from pyoauth2 import Client from pyoauth2 import AccessToken KEY = '' SECRET = '' CALLBACK = '' client = Client(KEY, SECRET, site='https://api.weibo.com', authorize_url='/oauth2/authorize', token_url='/oauth2/access_token') code = raw_input('Enter the verification code and hit ENTER when you\'re done:') client.auth_code.get_token(code, redirect_uri=CALLBACK) access_token = AccessToken(client, code) print access_token.get('/2/statuses/public_timeline.json', access_token=access_token.token).parsed
class ApiClient: API_HOST = 'https://api.douban.com' AUTH_HOST = 'https://www.douban.com' TOKEN_URL = AUTH_HOST + '/service/auth2/token' AUTHORIZE_URL = AUTH_HOST + '/service/auth2/auth' ACCESS_TOKEN_FILE = "data/access_token.txt" DEFAULT_START = 0 DEFAULT_COUNT = 20 def __init__(self, key, secret, redirect='', scope=''): self.redirect_uri = redirect self.scope = scope self.client = Client(key, secret, site=self.API_HOST, authorize_url=self.AUTHORIZE_URL, token_url=self.TOKEN_URL) self.access_token = AccessToken(self.client, '') self.load_token() def __repr__(self): return '<ApiClient OAuth2>' def auth_with_code(self, code): self.access_token = self.client.auth_code.get_token( code, redirect_uri=self.redirect_uri) def auth_with_token(self, token): self.access_token = AccessToken(self.client, token) def auth_with_password(self, username, password, **opt): self.access_token = self.client.password.get_token( username=username, password=password, redirect_uri=self.redirect_uri, **opt) def refresh_token(self, refresh_token): access_token = AccessToken(self.client, token='', refresh_token=refresh_token) self.access_token = access_token.refresh() def me(self): return self.access_token.get("/v2/lifestream/user/~me") def user(self, id): return self.access_token.get("/v2/lifestream/user/%d" % id) def followings(self, id, count=DEFAULT_COUNT, start=DEFAULT_START): return self.access_token.get("/v2/lifestream/user/%d/followings" % id, count=count, start=start) def followers(self, id, count=DEFAULT_COUNT, start=DEFAULT_START): return self.access_token.get("/v2/lifestream/user/%d/followers" % id, count=count, start=start) @property def authorize_url(self): return self.client.auth_code.authorize_url( redirect_uri=self.redirect_uri, scope=self.scope) @property def token_code(self): return self.access_token and self.access_token.token @property def refresh_token_code(self): return getattr(self.access_token, 'refresh_token', None) def save_token(self): print "save_token:", self.token_code with open(self.ACCESS_TOKEN_FILE, "w") as f: f.write(self.token_code) f.flush() def load_token(self): if os.path.isfile(self.ACCESS_TOKEN_FILE): f = open(self.ACCESS_TOKEN_FILE, "r") self.access_token.token = f.read() print "load_token:", self.token_code
class EverydoApiClient: def __init__(self, key, secret, api_host, redirect=''): self.key = key self.secret = secret self.api_host = api_host self.redirect_uri = redirect self.client = Client(key, secret, site=self.api_host, authorize_url=self.api_host + '/@@authorize', token_url= self.api_host + '/@@access_token') self.access_token = None def __repr__(self): return '<EverydoClient OAuth2>' @property def authorize_url(self): return self.client.auth_code.authorize_url(redirect_uri=self.redirect_uri) def auth_with_code(self, code): self.access_token = self.client.auth_code.get_token(code, redirect_uri=self.redirect_uri, header_format="Oauth2 %s") def auth_with_token(self, token): self.access_token = AccessToken(self.client, token, header_format="Oauth2 %s") def auth_with_password(self, username, password, **opt): self.access_token = self.client.password.get_token(username=username, password=password, redirect_uri=self.redirect_uri, **opt) @property def token_code(self): return self.access_token and self.access_token.token @property def refresh_token_code(self): return getattr(self.access_token, 'refresh_token', None) def refresh_token(self, refresh_token): access_token = AccessToken(self.client, token='', refresh_token=refresh_token, header_format="Oauth2 %s") self.access_token = access_token.refresh() def get_account(self): client = OCApiClient(self.key, self.secret, self.api_host, self.redirect_uri) client.auth_with_token(self.token_code) return client @check_execption def _get(self, url, **opts): return self.access_token.get(url, **opts) @property def list_sites(self): return self._get('/list_sites') def get_site(self, site_name): site = self.list_sites.get(site_name, {}) if not site: return None client = WOApiClient(self.key, self.secret, site['site_url'], self.redirect_uri) client.auth_with_token(self.token_code) return client