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,
)
| 183 | return uri, state |
| 184 | |
| 185 | def fetch_token( |
| 186 | self, |
| 187 | url=None, |
| 188 | body="", |
| 189 | method="POST", |
| 190 | headers=None, |
| 191 | auth=None, |
| 192 | grant_type=None, |
| 193 | state=None, |
| 194 | **kwargs, |
| 195 | ): |
| 196 | """Generic method for fetching an access token from the token endpoint. |
| 197 | |
| 198 | :param url: Access Token endpoint URL, if not configured, |
| 199 | ``authorization_response`` is used to extract token from |
| 200 | its fragment (implicit way). |
| 201 | :param body: Optional application/x-www-form-urlencoded body to add the |
| 202 | include in the token request. Prefer kwargs over body. |
| 203 | :param method: The HTTP method used to make the request. Defaults |
| 204 | to POST, but may also be GET. Other methods should |
| 205 | be added as needed. |
| 206 | :param headers: Dict to default request headers with. |
| 207 | :param auth: An auth tuple or method as accepted by requests. |
| 208 | :param grant_type: Use specified grant_type to fetch token. |
| 209 | :param state: Optional "state" value to fetch token. |
| 210 | :return: A :class:`OAuth2Token` object (a dict too). |
| 211 | """ |
| 212 | state = state or self.state |
| 213 | # implicit grant_type |
| 214 | authorization_response = kwargs.pop("authorization_response", None) |
| 215 | if authorization_response and "#" in authorization_response: |
| 216 | return self.token_from_fragment(authorization_response, state) |
| 217 | |
| 218 | session_kwargs = self._extract_session_request_params(kwargs) |
| 219 | |
| 220 | if authorization_response and "code=" in authorization_response: |
| 221 | grant_type = "authorization_code" |
| 222 | params = parse_authorization_code_response( |
| 223 | authorization_response, |
| 224 | state=state, |
| 225 | ) |
| 226 | kwargs["code"] = params["code"] |
| 227 | |
| 228 | if grant_type is None: |
| 229 | grant_type = self.metadata.get("grant_type") |
| 230 | |
| 231 | if grant_type is None: |
| 232 | grant_type = _guess_grant_type(kwargs) |
| 233 | self.metadata["grant_type"] = grant_type |
| 234 | |
| 235 | body = self._prepare_token_endpoint_body(body, grant_type, **kwargs) |
| 236 | |
| 237 | if auth is None: |
| 238 | auth = self.client_auth(self.token_endpoint_auth_method) |
| 239 | |
| 240 | if headers is None: |
| 241 | headers = DEFAULT_HEADERS |
| 242 |