def apply_auth(self, url, method, headers, parameters): request = oauth.OAuthRequest.from_consumer_and_token( self._consumer, http_url=url, http_method=method, token=self.access_token, parameters=parameters ) request.sign_request(self._sigmethod, self._consumer, self.access_token) headers.update(request.to_header())
def getAccessToken(self): consumer = oauth.Consumer(self.public_key, self.private_key) token = oauth.Token(None, None) request = oauth.Request.from_consumer_and_token( consumer, token=token, http_method='GET', http_url='http://api.telldus.com/oauth/accessToken') request.sign_request(oauth.SignatureMethod_HMAC_SHA1(), consumer, token) conn = http.client.HTTPConnection('api.telldus.com:80') conn.request(request.http_method, request.to_url(), headers=request.to_header()) resp = conn.getresponse() if resp.status != 200: print(('Error retrieving access token, the server replied:\n%s' % resp.read())) return token = oauth.Token.from_string(resp.read()) self.token = str(token.key) self.token_secret = str(token.secret) print('Authentication successful, you can now use tdtool') return str(token.key), str(token.secret)
def _makeOAuthRequest(self, url, token=None, params=None, http_method="GET"): '''Make a OAuth request from url and parameters Args: url: The Url to use for creating OAuth Request parameters: The URL parameters http_method: The HTTP method to use Returns: A OAauthRequest object ''' oauth_base_params = { 'oauth_version': "1.0", 'oauth_nonce': oauth.generate_nonce(), 'oauth_timestamp': int(time.time()) } if params: params.update(oauth_base_params) else: params = oauth_base_params if not token: token = self._access_token request = oauth.Request(method=http_method,url=url,parameters=params) request.sign_request(self._signature_method, self._Consumer, token) return request
def _makeOAuthRequest(self, url, token=None, params=None, http_method="GET"): '''Make a OAuth request from url and parameters Args: url: The Url to use for creating OAuth Request parameters: The URL parameters http_method: The HTTP method to use Returns: A OAauthRequest object ''' oauth_base_params = { 'oauth_version': "1.0", 'oauth_nonce': oauth.generate_nonce(), 'oauth_timestamp': int(time.time()) } if params: params.update(oauth_base_params) else: params = oauth_base_params if not token: token = self._access_token request = oauth.Request(method=http_method, url=url, parameters=params) request.sign_request(self._signature_method, self._Consumer, token) return request
def oauth_post_request(self, token, url, params): """Generate OAuth request, setups callback url""" if 'oauth_verifier' in self.data: params['oauth_verifier'] = self.data['oauth_verifier'] request = OAuthRequest.from_consumer_and_token(self.consumer, token=token, http_url=url, parameters=params, http_method='POST') request.sign_request(SignatureMethod_HMAC_SHA1(), self.consumer, token) return request
def build_xoauth_string(url, consumer, token=None): """Build an XOAUTH string for use in SMTP/IMPA authentication.""" request = Request.from_consumer_and_token(consumer, token, "GET", url) signing_method = SignatureMethod_HMAC_SHA1() request.sign_request(signing_method, consumer, token) params = [] for k, v in sorted(request.items()): if v is not None: params.append('%s="%s"' % (k, escape(v))) return "%s %s %s" % ("GET", url, ','.join(params))
def get_oauth_params(self, url, key, secret, data=None, method='GET', to_header=False, to_dict=False): """ Converts a mapping object to signed url query. *Parameters:* :url: Target url :key: Public API key :secret: Public API key secret :data: Dictionary with data parameters :method: Mehtod to be called, default is ``GET`` :to_header: If ``True``, data will be encoded as auth headers """ # Temporary not use incoming data, just generate headers if data is None: data = {} else: data = data.copy() token = oauth.Token(key, secret) consumer = self.get_oauth_consumer() data.update({ 'oauth_token': token.key, 'oauth_consumer_key': consumer.key, 'oauth_version': '1.0', 'oauth_nonce': oauth.generate_nonce(), 'oauth_timestamp': int(time.time()), }) request = oauth.Request(method=method, url=url, parameters=data) signature_method = oauth.SignatureMethod_HMAC_SHA1() request.sign_request(signature_method, consumer, token) if to_header: return request.to_header() return request.to_postdata()
def _makeOAuthRequest(self, url, token=None, params=None): """Make a OAuth request from url and parameters. Returns oAuth object.""" oauth_base_params = { 'oauth_version': "1.0", 'oauth_nonce': oauth.generate_nonce(), 'oauth_timestamp': int(time.time()) } if params: params.update(oauth_base_params) else: params = oauth_base_params if not token: token = self._access_token request = oauth.Request(method="GET", url=url, parameters=params) request.sign_request(self._signature_method, self._Consumer, token) return request
def requestToken(self): consumer = oauth.Consumer(self.public_key, self.private_key) request = oauth.Request.from_consumer_and_token( consumer, http_url='http://api.telldus.com/oauth/requestToken') request.sign_request(oauth.SignatureMethod_HMAC_SHA1(), consumer, None) conn = http.client.HTTPConnection('api.telldus.com:80') conn.request(request.http_method, '/oauth/requestToken', headers=request.to_header()) resp = conn.getresponse().read() token = oauth.Token.from_string(resp) print(( 'Open the following url in your webbrowser:\nhttp://api.telldus.com/oauth/authorize?oauth_token=%s\n' % token.key)) print(( 'After logging in and accepting to use this application run:\n%s --authenticate' % (sys.argv[0]))) self.token = str(token.key) self.token_secret = str(token.secret) return str(token.key), str(token.secret)
def augment(self, url, parameters): ''' Construct, sign, and open a twitter request using the hard-coded credentials above. ''' consumer = oauth.Consumer( key=self.credencialesTwitter['consumer_key'], secret=self.credencialesTwitter['consumer_secret']) token = oauth.Token(key=self.credencialesTwitter['token_key'], secret=self.credencialesTwitter['token_secret']) signature_method_hmac_sha1 = oauth.SignatureMethod_HMAC_SHA1() request = oauth.Request.from_consumer_and_token(consumer, token, http_method='GET', http_url=url, parameters=parameters) request.sign_request(signature_method_hmac_sha1, consumer, token) headers = request.to_header() return request.to_url()