def fetch_token(<EOL>self,<EOL>token_url,<EOL>code=None,<EOL>authorization_response=None,<EOL>body="<STR_LIT>",<EOL>auth=None,<EOL>username=None,<EOL>password=None,<EOL>method="<STR_LIT:POST>",<EOL>force_querystring=False,<EOL>timeout=None,<EOL>headers=None,<EOL>verify=True,<EOL>proxies=None,<EOL>include_client_id=None,<EOL>client_secret=None,<EOL>**kwargs<EOL>):
if not is_secure_transport(token_url):<EOL><INDENT>raise InsecureTransportError()<EOL><DEDENT>if not code and authorization_response:<EOL><INDENT>self._client.parse_request_uri_response(<EOL>authorization_response, state=self._state<EOL>)<EOL>code = self._client.code<EOL><DEDENT>elif not code and isinstance(self._client, WebApplicationClient):<EOL><INDENT>code = self._client.code<EOL>if not code:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>" "<STR_LIT>"<EOL>)<EOL><DEDENT><DEDENT>if isinstance(self._client, LegacyApplicationClient):<EOL><INDENT>if username is None:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>if password is None:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT><DEDENT>if username is not None:<EOL><INDENT>kwargs["<STR_LIT:username>"] = username<EOL><DEDENT>if password is not None:<EOL><INDENT>kwargs["<STR_LIT:password>"] = password<EOL><DEDENT>if auth is not None:<EOL><INDENT>if include_client_id is None:<EOL><INDENT>include_client_id = False<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if include_client_id is not True:<EOL><INDENT>client_id = self.client_id<EOL>if client_id:<EOL><INDENT>log.debug(<EOL>'<STR_LIT>'<EOL>"<STR_LIT>",<EOL>client_id,<EOL>)<EOL>client_secret = client_secret if client_secret is not None else "<STR_LIT>"<EOL>auth = requests.auth.HTTPBasicAuth(client_id, client_secret)<EOL><DEDENT><DEDENT><DEDENT>if include_client_id:<EOL><INDENT>if client_secret is not None:<EOL><INDENT>kwargs["<STR_LIT>"] = client_secret<EOL><DEDENT><DEDENT>body = self._client.prepare_request_body(<EOL>code=code,<EOL>body=body,<EOL>redirect_uri=self.redirect_uri,<EOL>include_client_id=include_client_id,<EOL>**kwargs<EOL>)<EOL>headers = headers or {<EOL>"<STR_LIT>": "<STR_LIT:application/json>",<EOL>"<STR_LIT:Content-Type>": "<STR_LIT>",<EOL>}<EOL>self.token = {}<EOL>request_kwargs = {}<EOL>if method.upper() == "<STR_LIT:POST>":<EOL><INDENT>request_kwargs["<STR_LIT>" if force_querystring else "<STR_LIT:data>"] = dict(<EOL>urldecode(body)<EOL>)<EOL><DEDENT>elif method.upper() == "<STR_LIT:GET>":<EOL><INDENT>request_kwargs["<STR_LIT>"] = dict(urldecode(body))<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>r = self.request(<EOL>method=method,<EOL>url=token_url,<EOL>timeout=timeout,<EOL>headers=headers,<EOL>auth=auth,<EOL>verify=verify,<EOL>proxies=proxies,<EOL>**request_kwargs<EOL>)<EOL>log.debug("<STR_LIT>", r.status_code)<EOL>log.debug("<STR_LIT>", r.request.url)<EOL>log.debug("<STR_LIT>", r.request.headers)<EOL>log.debug("<STR_LIT>", r.request.body)<EOL>log.debug("<STR_LIT>", r.headers, r.text)<EOL>log.debug(<EOL>"<STR_LIT>",<EOL>len(self.compliance_hook["<STR_LIT>"]),<EOL>)<EOL>for hook in self.compliance_hook["<STR_LIT>"]:<EOL><INDENT>log.debug("<STR_LIT>", hook)<EOL>r = hook(r)<EOL><DEDENT>self._client.parse_request_body_response(r.text, scope=self.scope)<EOL>self.token = self._client.token<EOL>log.debug("<STR_LIT>", self.token)<EOL>return self.token<EOL>
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 kwargs: Extra parameters to include in the token request.
        :return: A token dict