Generic method for fetching an access token from the token endpoint. :param url: Access Token endpoint URL, if not configured, ``authorization_response`` is used to extract token from its fragment (implicit way). :param body: Optional applicat
(
self,
url=None,
body="",
method="POST",
headers=None,
auth=None,
grant_type=None,
state=None,
**kwargs,
)
| 205 | return uri, state |
| 206 | |
| 207 | def fetch_token( |
| 208 | self, |
| 209 | url=None, |
| 210 | body="", |
| 211 | method="POST", |
| 212 | headers=None, |
| 213 | auth=None, |
| 214 | grant_type=None, |
| 215 | state=None, |
| 216 | **kwargs, |
| 217 | ): |
| 218 | """Generic method for fetching an access token from the token endpoint. |
| 219 | |
| 220 | :param url: Access Token endpoint URL, if not configured, |
| 221 | ``authorization_response`` is used to extract token from |
| 222 | its fragment (implicit way). |
| 223 | :param body: Optional application/x-www-form-urlencoded body to add the |
| 224 | include in the token request. Prefer kwargs over body. |
| 225 | :param method: The HTTP method used to make the request. Defaults |
| 226 | to POST, but may also be GET. Other methods should |
| 227 | be added as needed. |
| 228 | :param headers: Dict to default request headers with. |
| 229 | :param auth: An auth tuple or method as accepted by requests. |
| 230 | :param grant_type: Use specified grant_type to fetch token. |
| 231 | :param state: Optional "state" value to fetch token. |
| 232 | :return: A :class:`OAuth2Token` object (a dict too). |
| 233 | """ |
| 234 | state = state or self.state |
| 235 | # implicit grant_type |
| 236 | authorization_response = kwargs.pop("authorization_response", None) |
| 237 | if authorization_response and "#" in authorization_response: |
| 238 | return self.token_from_fragment(authorization_response, state) |
| 239 | |
| 240 | session_kwargs = self._extract_session_request_params(kwargs) |
| 241 | |
| 242 | if authorization_response: |
| 243 | grant_type = "authorization_code" |
| 244 | params = parse_authorization_code_response( |
| 245 | authorization_response, |
| 246 | state=state, |
| 247 | ) |
| 248 | kwargs["code"] = params["code"] |
| 249 | |
| 250 | if grant_type is None: |
| 251 | grant_type = self.metadata.get("grant_type") |
| 252 | |
| 253 | if grant_type is None: |
| 254 | grant_type = _guess_grant_type(kwargs) |
| 255 | self.metadata["grant_type"] = grant_type |
| 256 | |
| 257 | body = self._prepare_token_endpoint_body(body, grant_type, **kwargs) |
| 258 | |
| 259 | if auth is None: |
| 260 | auth = self.client_auth(self.token_endpoint_auth_method) |
| 261 | |
| 262 | if headers is None: |
| 263 | headers = DEFAULT_HEADERS |
| 264 |