Exchange ``code`` for access token. Args: code: OAuth 2.0 authorization code to be exchanged for access token. state: Authorization state; should match the one generated when creating the authorize URL.
(self, *, code: str, **kwargs)
| 204 | return url |
| 205 | |
| 206 | def fetch_token(self, *, code: str, **kwargs) -> dict: |
| 207 | """Exchange ``code`` for access token. |
| 208 | |
| 209 | Args: |
| 210 | code: |
| 211 | OAuth 2.0 authorization code to be exchanged for access token. |
| 212 | state: |
| 213 | Authorization state; should match the one generated when creating |
| 214 | the authorize URL. |
| 215 | |
| 216 | Note: except for the out-of-band flow, be sure to pass in the |
| 217 | ``state`` parameter received on the ``redirect_uri`` for added |
| 218 | security. |
| 219 | """ |
| 220 | # make sure states match before proceeding, we shouldn't be tricked |
| 221 | # into exchanging arbitrary auth codes. |
| 222 | state = kwargs.get('state', None) |
| 223 | if state is not None: |
| 224 | if state != self.state: |
| 225 | raise ValueError('State mismatch') |
| 226 | |
| 227 | token = self.session.fetch_token( |
| 228 | url=self.token_endpoint, |
| 229 | grant_type='authorization_code', |
| 230 | code=code, |
| 231 | code_verifier=self.code_verifier, |
| 232 | **kwargs) |
| 233 | |
| 234 | logger.debug(f"{type(self).__name__}.fetch_token() = {token!r}") |
| 235 | self._save_token_to_creds(token) |
| 236 | |
| 237 | return token |
| 238 | |
| 239 | def refresh_token(self): |
| 240 | token = self.session.refresh_token(url=self.token_endpoint) |