6 examples of 'python requests bearer token' in Python

Every line of 'python requests bearer token' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
22def session(proxies,headers,cookie):
23 r=requests.Session()
24 r.proxies=proxies
25 r.headers=headers
26 r.cookies.update(json.loads(cookie))
27 return r
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

225def request_access_token(self, authorization, url):
226 """ Request an access token. """
227 request = six.moves.urllib.request.Request(
228 url=url,
229 headers={'Authorization': authorization,
230 'Accept': 'application/json'},
231 data={}
232 )
233
234 opener = six.moves.urllib.request.build_opener()
235
236 try:
237 result = opener.open(request)
238 self.parse_token_response(result.read())
239 except six.moves.urllib.error.HTTPError as e:
240 self.parse_error_response(e)
108def get_request_token(self,):
109 """Get request token
110 """
111 oauth = OAuth1(self.consumer_key, client_secret=self.consumer_secret, callback_uri=CALLBACK_URI)
112 response = requests.post(url=REQUEST_TOKEN_URL, auth=oauth)
113 tokens = self.fetch_tokens(response.content)
114 logging.debug(tokens)
115 self.request_token, self.request_token_secret = tokens.get('oauth_token'), tokens.get('oauth_token_secret')
116 logging.debug("{0}, {1}".format(self.request_token, self.request_token_secret))
117 return tokens
221def set_token(self, access_token):
222 """Set the OAuth token for this requester"""
223 self.oauth_token = access_token
224 self.userless = not bool(access_token) # Userless if no access_token
77def refresh_token(refresh_token, client_id):
78 resp = requests.post('https://api.tdameritrade.com/v1/oauth2/token',
79 headers={'Content-Type': 'application/x-www-form-urlencoded'},
80 data={'grant_type': 'refresh_token',
81 'refresh_token': refresh_token,
82 'client_id': client_id})
83 if resp.status_code != 200:
84 raise Exception('Could not authenticate!')
85 return resp.json()
174def refresh_access_token(self):
175 headers = {
176 "Accept": "application/json",
177 "Content-Type": "application/x-www-form-urlencoded"
178 }
179
180 params = {
181 "refresh_token": self.oauth_user.refresh_token,
182 "grant_type": "refresh_token",
183 }
184 rst = self._session.request(method='POST', url=self.oauth_service.access_token_url,
185 headers=headers, params=params)
186 data = rst.json()
187 if rst.status_code == 200:
188 self.oauth_user.refresh_token = data.get("refresh_token")
189 self.oauth_user.access_token = data.get("access_token")
190 self.access_token = data.get("access_token")
191 self.refresh_token = data.get("refresh_token")
192 self.set_api(self.oauth_service.home_url, self.oauth_user.access_token)
193 self.oauth_user = self.oauth_user.save()
194 return

Related snippets