def refresh(self, refresh_token=None): """Get a new access token from the supplied refresh token https://svn.tools.ietf.org/html/draft-hammer-oauth2-00#section-4 """ endpoint = 'access_token' refresh_token = refresh_token or self.refresh_token if not refresh_token: raise ValueError("refresh_token can't be empty") args = { 'grant_type': 'refresh_token', 'client_id': self._api_key, 'refresh_token': refresh_token, } uri = urlparse.urljoin(self.BASE_URL, endpoint) body = urlencode(args) headers = { 'Content-Type': 'application/x-www-form-urlencoded', } req = Request(uri, body, headers) resp = urlopen(req) content = resp.read() if not resp.code == 200: raise Error(content) response_args = dict(parse_qsl(content)) self.access_token = response_args.get("access_token", None) self.refresh_token = response_args.get("refresh_token", None) return response_args
def get_access_token(self, code): """user code to access token Get an access token from the supplied code https://svn.tools.ietf.org/html/draft-hammer-oauth2-00#section-3.5.2.2 """ if code is None: raise ValueError("Code must be set.") endpoint='access_token' params = {} if 'state' in self.params: params['state'] = self.params['state'] args = { 'grant_type': 'authorization_code', 'client_id': self._api_key, 'client_secret': self._api_secret, 'code': code, 'redirect_uri': self.callback, } args.update(params or {}) uri = urlparse.urljoin(self.BASE_URL, endpoint) body = urlencode(args) headers = { 'Content-Type': 'application/x-www-form-urlencoded', } req = Request(uri, body, headers) resp = urlopen(req) content = resp.read() if not resp.code == 200: print (resp, resp.code, content) raise Error(content) response_args = dict(parse_qsl(content)) error = response_args.get('error', None) if error is not None: msg = "%s:%s" % (error, response_args.get('error_description', '')) raise Error(msg) refresh_token = response_args.get('refresh_token', None) access_token = response_args.get('access_token', None) openid = response_args.get('openid', None) self.refresh_token = refresh_token self.access_token = access_token self.openid = openid if refresh_token is not None: response_args = self.refresh(refresh_token) return response_args
def get_access_token(self, code): """user code to access token Get an access token from the supplied code https://svn.tools.ietf.org/html/draft-hammer-oauth2-00#section-3.5.2.2 """ if code is None: raise ValueError("Code must be set.") endpoint = 'access_token' params = {} if 'state' in self.params: params['state'] = self.params['state'] args = { 'grant_type': 'authorization_code', 'client_id': self._api_key, 'client_secret': self._api_secret, 'code': code, 'redirect_uri': self.callback, } args.update(params or {}) uri = urlparse.urljoin(self.BASE_URL, endpoint) body = urlencode(args) headers = { 'Content-Type': 'application/x-www-form-urlencoded', } req = Request(uri, body, headers) resp = urlopen(req) content = resp.read() if not resp.code == 200: print(resp, resp.code, content) raise Error(content) response_args = dict(parse_qsl(content)) error = response_args.get('error', None) if error is not None: msg = "%s:%s" % (error, response_args.get('error_description', '')) raise Error(msg) refresh_token = response_args.get('refresh_token', None) access_token = response_args.get('access_token', None) openid = response_args.get('openid', None) self.refresh_token = refresh_token self.access_token = access_token self.openid = openid if refresh_token is not None: response_args = self.refresh(refresh_token) return response_args
def get_authorization_url(self): """return a url for user to open Get the URL to redirect the user for client authorization https://svn.tools.ietf.org/html/draft-hammer-oauth2-00#section-3.5.2.1 """ endpoint = 'authorize' redirect_uri = self.callback params = self.params args = {'response_type': 'code', 'client_id': self._api_key} args['redirect_uri'] = self.callback args.update(params or {}) return '%s?%s' % (urlparse.urljoin(self.BASE_URL, endpoint), urlencode(args))
def get_authorization_url(self): """return a url for user to open Get the URL to redirect the user for client authorization https://svn.tools.ietf.org/html/draft-hammer-oauth2-00#section-3.5.2.1 """ endpoint = 'authorize' redirect_uri = self.callback params = self.params args = { 'response_type': 'code', 'client_id': self._api_key } args['redirect_uri'] = self.callback args.update(params or {}) return '%s?%s' % (urlparse.urljoin(self.BASE_URL, endpoint), urlencode(args))