oauth2_session
OAuth2Session
Bases: Session
Versatile OAuth 2 extension to :class:requests.Session
.
Supports any grant type adhering to :class:oauthlib.oauth2.Client
spec including the four core OAuth 2 grants.
Can be used to create authorization urls, fetch tokens and access protected resources using the :class:requests.Session
interface you are used to.
- :class:
oauthlib.oauth2.WebApplicationClient
(default): Authorization Code Grant - :class:
oauthlib.oauth2.MobileApplicationClient
: Implicit Grant - :class:
oauthlib.oauth2.LegacyApplicationClient
: Password Credentials Grant - :class:
oauthlib.oauth2.BackendApplicationClient
: Client Credentials Grant
Note that the only time you will be using Implicit Grant from python is if you are driving a user agent able to obtain URL fragments.
Source code in server/vendor/requests_oauthlib/oauth2_session.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 |
|
authorized
property
Boolean that indicates whether this session has an OAuth token or not. If self.authorized
is True, you can reasonably expect OAuth-protected requests to the resource to succeed. If self.authorized
is False, you need the user to go through the OAuth authentication dance before OAuth-protected requests to the resource will succeed.
scope
property
writable
By default the scope from the client is used, except if overridden
__init__(client_id=None, client=None, auto_refresh_url=None, auto_refresh_kwargs=None, scope=None, redirect_uri=None, token=None, state=None, token_updater=None, pkce=None, **kwargs)
Construct a new OAuth 2 client session.
:param client_id: Client id obtained during registration :param client: :class:oauthlib.oauth2.Client
to be used. Default is WebApplicationClient which is useful for any hosted application but not mobile or desktop. :param scope: List of scopes you wish to request access to :param redirect_uri: Redirect URI you registered as callback :param token: Token dictionary, must include access_token and token_type. :param state: State string used to prevent CSRF. This will be given when creating the authorization url and must be supplied when parsing the authorization response. Can be either a string or a no argument callable. :auto_refresh_url: Refresh token endpoint URL, must be HTTPS. Supply this if you wish the client to automatically refresh your access tokens. :auto_refresh_kwargs: Extra arguments to pass to the refresh token endpoint. :token_updater: Method with one argument, token, to be used to update your token database on automatic token refresh. If not set a TokenUpdated warning will be raised when a token has been refreshed. This warning will carry the token in its token argument. :param pkce: Set "S256" or "plain" to enable PKCE. Default is disabled. :param kwargs: Arguments to pass to the Session constructor.
Source code in server/vendor/requests_oauthlib/oauth2_session.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
|
authorization_url(url, state=None, **kwargs)
Form an authorization URL.
:param url: Authorization endpoint url, must be HTTPS. :param state: An optional state string for CSRF protection. If not given it will be generated for you. :param kwargs: Extra parameters to include. :return: authorization_url, state
Source code in server/vendor/requests_oauthlib/oauth2_session.py
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
|
fetch_token(token_url, code=None, authorization_response=None, body='', auth=None, username=None, password=None, method='POST', force_querystring=False, timeout=None, headers=None, verify=None, proxies=None, include_client_id=None, client_secret=None, cert=None, **kwargs)
Generic method for fetching an access token from the token endpoint.
If you are using the MobileApplicationClient you will want to use token_from_fragment
instead of fetch_token
.
The current implementation enforces the RFC guidelines.
:param token_url: Token endpoint URL, must use HTTPS. :param code: Authorization code (used by WebApplicationClients). :param authorization_response: Authorization response URL, the callback URL of the request back to you. Used by WebApplicationClients instead of code. :param body: Optional application/x-www-form-urlencoded body to add the include in the token request. Prefer kwargs over body. :param auth: An auth tuple or method as accepted by requests
. :param username: Username required by LegacyApplicationClients to appear in the request body. :param password: Password required by LegacyApplicationClients to appear in the request body. :param method: The HTTP method used to make the request. Defaults to POST, but may also be GET. Other methods should be added as needed. :param force_querystring: If True, force the request body to be sent in the querystring instead. :param timeout: Timeout of the request in seconds. :param headers: Dict to default request headers with. :param verify: Verify SSL certificate. :param proxies: The proxies
argument is passed onto requests
. :param include_client_id: Should the request body include the client_id
parameter. Default is None
, which will attempt to autodetect. This can be forced to always include (True) or never include (False). :param client_secret: The client_secret
paired to the client_id
. This is generally required unless provided in the auth
tuple. If the value is None
, it will be omitted from the request, however if the value is an empty string, an empty string will be sent. :param cert: Client certificate to send for OAuth 2.0 Mutual-TLS Client Authentication (draft-ietf-oauth-mtls). Can either be the path of a file containing the private key and certificate or a tuple of two filenames for certificate and key. :param kwargs: Extra parameters to include in the token request. :return: A token dict
Source code in server/vendor/requests_oauthlib/oauth2_session.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
|
new_state()
Generates a state string to be used in authorizations.
Source code in server/vendor/requests_oauthlib/oauth2_session.py
120 121 122 123 124 125 126 127 128 |
|
refresh_token(token_url, refresh_token=None, body='', auth=None, timeout=None, headers=None, verify=None, proxies=None, **kwargs)
Fetch a new access token using a refresh token.
:param token_url: The token endpoint, must be HTTPS. :param refresh_token: The refresh_token to use. :param body: Optional application/x-www-form-urlencoded body to add the include in the token request. Prefer kwargs over body. :param auth: An auth tuple or method as accepted by requests
. :param timeout: Timeout of the request in seconds. :param headers: A dict of headers to be used by requests
. :param verify: Verify SSL certificate. :param proxies: The proxies
argument will be passed to requests
. :param kwargs: Extra parameters to include in the token request. :return: A token dict
Source code in server/vendor/requests_oauthlib/oauth2_session.py
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 |
|
register_compliance_hook(hook_type, hook)
Register a hook for request/response tweaking.
Available hooks are
access_token_response invoked before token parsing. refresh_token_response invoked before refresh token parsing. protected_request invoked before making a request. access_token_request invoked before making a token fetch request. refresh_token_request invoked before making a refresh request.
If you find a new hook is needed please send a GitHub PR request or open an issue.
Source code in server/vendor/requests_oauthlib/oauth2_session.py
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 |
|
request(method, url, data=None, headers=None, withhold_token=False, client_id=None, client_secret=None, files=None, **kwargs)
Intercept all requests and add the OAuth 2 token if present.
Source code in server/vendor/requests_oauthlib/oauth2_session.py
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 |
|
token_from_fragment(authorization_response)
Parse token from the URI fragment, used by MobileApplicationClients.
:param authorization_response: The full URL of the redirect back to you :return: A token dict
Source code in server/vendor/requests_oauthlib/oauth2_session.py
411 412 413 414 415 416 417 418 419 420 421 |
|