def __init__(self, key=None, password=None, **kwargs): netrcfile = kwargs.pop('netrcfile', None) super(BasicClient, self).__init__(**kwargs) if key is None: key = os.environ.get('SMARTFILE_API_KEY') if password is None: password = os.environ.get('SMARTFILE_API_PASSWORD') if key is None or password is None: try: rc = netrc(netrcfile) except: pass else: urlp = urlparse.urlparse(self.url) auth = rc.authenticators(urlp.netloc) if auth is not None: if key is None: key = auth[0] if key is None: key = auth[1] if password is None: password = auth[2] try: self.key, self.password = clean_tokens(key, password) except ValueError: raise APIError('Please provide an API key and password. Use ' 'arguments or environment variables.')
def get_authorization_url(self, request=None): "The second step of the OAuth workflow." if request is None: if not self.__request.is_valid(): raise APIError('You must obtain a request token to request ' 'and access token. Use get_request_token() ' 'first.') request = self.__request url = urlparse.urljoin(self.url, 'oauth/authorize/') return url + '?' + urllib.urlencode(dict(oauth_token=request.token))
def _do_request(self, *args, **kwargs): if not self._access.is_valid(): raise APIError('You must obtain an access token' 'before making API calls.') # Add the OAuth parameters. kwargs['auth'] = OAuth1(self._client.token, client_secret=self._client.secret, resource_owner_key=self._access.token, resource_owner_secret=self._access.secret, signature_method=SIGNATURE_PLAINTEXT) return super(OAuthClient, self)._do_request(*args, **kwargs)
def __init__(self, client_token=None, client_secret=None, access_token=None, access_secret=None, **kwargs): if client_token is None: client_token = os.environ.get('SMARTFILE_CLIENT_TOKEN') if client_secret is None: client_secret = os.environ.get('SMARTFILE_CLIENT_SECRET') if access_token is None: access_token = os.environ.get('SMARTFILE_ACCESS_TOKEN') if access_secret is None: access_secret = os.environ.get('SMARTFILE_ACCESS_SECRET') self._client = OAuthToken(client_token, client_secret) if not self._client.is_valid(): raise APIError('You must provide a client_token' 'and client_secret for OAuth.') self._access = OAuthToken(access_token, access_secret) super(OAuthClient, self).__init__(**kwargs)
def get_access_token(self, request=None, verifier=None): """The final step of the OAuth workflow. After this the client can make API calls.""" if request is None: if not self.__request.is_valid(): raise APIError('You must obtain a request token to request ' 'and access token. Use get_request_token() ' 'first.') request = self.__request oauth = OAuth1(self._client.token, client_secret=self._client.secret, resource_owner_key=request.token, resource_owner_secret=request.secret, verifier=verifier, signature_method=SIGNATURE_PLAINTEXT) r = requests.post(urlparse.urljoin( self.url, 'oauth/access_token/'), auth=oauth) credentials = urlparse.parse_qs(r.text) self._access = OAuthToken(credentials.get('oauth_token')[0], credentials.get('oauth_token_secret')[0]) return self._access