def get_access_token(self): """ Obtain the access token to access private resources at the API endpoint. """ if self.access_token is None: request_token = self._get_rt_from_session() oauth = OAuth1( self.consumer_key, client_secret=self.consumer_secret, resource_owner_key=request_token['oauth_token'], resource_owner_secret=request_token['oauth_token_secret']) at_url = self.access_token_url # Passing along oauth_verifier is required according to: # http://groups.google.com/group/twitter-development-talk/browse_frm/thread/472500cfe9e7cdb9# # Though, the custom oauth_callback seems to work without it? oauth_verifier = get_request_param(self.request, 'oauth_verifier') if oauth_verifier: at_url = at_url + '?' + urlencode( {'oauth_verifier': oauth_verifier}) response = requests.post(url=at_url, auth=oauth) if response.status_code not in [200, 201]: raise OAuthError( _('Invalid response while obtaining access token' ' from "%s".') % get_token_prefix(self.request_token_url)) self.access_token = dict(parse_qsl(response.text)) self.request.session['oauth_%s_access_token' % get_token_prefix( self.request_token_url)] = self.access_token return self.access_token
def get_auth_params(self, request, action): settings = self.get_settings() ret = dict(settings.get('AUTH_PARAMS', {})) dynamic_auth_params = request.GET.get('auth_params', None) if dynamic_auth_params: ret.update(dict(parse_qsl(dynamic_auth_params))) return ret
def _get_request_token(self): """ Obtain a temporary request token to authorize an access token and to sign the request to obtain the access token """ if self.request_token is None: get_params = {} if self.parameters: get_params.update(self.parameters) get_params['oauth_callback'] \ = self.request.build_absolute_uri(self.callback_url) rt_url = self.request_token_url + '?' + urlencode(get_params) oauth = OAuth1(self.consumer_key, client_secret=self.consumer_secret) response = requests.post(url=rt_url, auth=oauth) if response.status_code not in [200, 201]: raise OAuthError( _('Invalid response while obtaining request token' ' from "%s".') % get_token_prefix(self.request_token_url) + ' Response content: %s' % response.content.decode("utf-8")) self.request_token = dict(parse_qsl(response.text)) self.request.session['oauth_%s_request_token' % get_token_prefix( self.request_token_url)] = self.request_token return self.request_token
def get_access_token(self): """ Obtain the access token to access private resources at the API endpoint. """ if self.access_token is None: request_token = self._get_rt_from_session() oauth = OAuth1( self.consumer_key, client_secret=self.consumer_secret, resource_owner_key=request_token['oauth_token'], resource_owner_secret=request_token['oauth_token_secret']) at_url = self.access_token_url # Passing along oauth_verifier is required according to: # http://groups.google.com/group/twitter-development-talk/browse_frm/thread/472500cfe9e7cdb9# # Though, the custom oauth_callback seems to work without it? oauth_verifier = get_request_param(self.request, 'oauth_verifier') if oauth_verifier: at_url = at_url + '?' + urlencode( {'oauth_verifier': oauth_verifier}) response = requests.post(url=at_url, auth=oauth) if response.status_code not in [200, 201]: raise OAuthError( _('Invalid response while obtaining access token' ' from "%s".') % get_token_prefix( self.request_token_url)) self.access_token = dict(parse_qsl(response.text)) self.request.session['oauth_%s_access_token' % get_token_prefix( self.request_token_url)] = self.access_token return self.access_token
def get_access_token(self, code): data = { 'redirect_uri': self.callback_url, 'grant_type': 'authorization_code', 'code': code} if self.basic_auth: auth = requests.auth.HTTPBasicAuth( self.consumer_key, self.consumer_secret) else: auth = None data.update({ 'client_id': self.consumer_key, 'client_secret': self.consumer_secret }) params = None self._strip_empty_keys(data) url = self.access_token_url if self.access_token_method == 'GET': params = data data = None # TODO: Proper exception handling resp = requests.request( self.access_token_method, url, params=params, data=data, headers=self.headers, auth=auth) access_token = None if resp.status_code in [200, 201]: # Weibo sends json via 'text/plain;charset=UTF-8' if (resp.headers['content-type'].split( ';')[0] == 'application/json' or resp.text[:2] == '{"'): access_token = resp.json() else: access_token = dict(parse_qsl(resp.text)) if not access_token or 'access_token' not in access_token: raise OAuth2Error('Error retrieving access token: %s' % resp.content) return access_token
def get_access_token(self, code): data = { 'redirect_uri': self.callback_url, 'grant_type': 'authorization_code', 'code': code} if self.basic_auth: auth = requests.auth.HTTPBasicAuth( self.consumer_key, self.consumer_secret) else: auth = None data.update({ 'client_id': self.consumer_key, 'client_secret': self.consumer_secret }) params = None self._strip_empty_keys(data) url = self.access_token_url if self.access_token_method == 'GET': params = data data = None # TODO: Proper exception handling resp = requests.request( self.access_token_method, url, params=params, data=data, headers=self.headers, auth=auth) access_token = None if resp.status_code == 200: # Weibo sends json via 'text/plain;charset=UTF-8' if (resp.headers['content-type'].split( ';')[0] == 'application/json' or resp.text[:2] == '{"'): access_token = resp.json() else: access_token = dict(parse_qsl(resp.text)) if not access_token or 'access_token' not in access_token: raise OAuth2Error('Error retrieving access token: %s' % resp.content) return access_token
def _get_request_token(self): """ Obtain a temporary request token to authorize an access token and to sign the request to obtain the access token """ if self.request_token is None: get_params = {} if self.parameters: get_params.update(self.parameters) get_params['oauth_callback'] \ = self.request.build_absolute_uri(self.callback_url) rt_url = self.request_token_url + '?' + urlencode(get_params) oauth = OAuth1(self.consumer_key, client_secret=self.consumer_secret) response = requests.post(url=rt_url, auth=oauth) if response.status_code not in [200, 201]: raise OAuthError( _('Invalid response while obtaining request token from "%s".') % get_token_prefix(self.request_token_url)) self.request_token = dict(parse_qsl(response.text)) self.request.session['oauth_%s_request_token' % get_token_prefix(self.request_token_url)] = self.request_token return self.request_token