<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Invalid Authorization Code - Authorization Code Flow in Spotify for Developers</title>
    <link>https://community.spotify.com/t5/Spotify-for-Developers/Invalid-Authorization-Code-Authorization-Code-Flow/m-p/5159627#M2087</link>
    <description>&lt;P&gt;&lt;STRONG&gt;Plan&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Premium&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Country&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;US&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Device&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;PC&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Operating System&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Windows 10&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;My Question or Issue&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;I'm trying to request an access token using the /token endpoint with python requests library but keep receiving this error: {'error': 'invalid_grant', 'error_description': 'Invalid authorization code'}. Here's a snippet of my code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import os
import base64
import requests

client_id = os.environ.get('CLIENT_ID')
client_secret = os.environ.get('CLIENT_SECRET')

# taking spotify application variables and base64 encoding
credentials = client_id + ':' + client_secret
credentials_bytes = credentials.encode('utf-8')
credentials_encoded = base64.b64encode(credentials_bytes)
authorization = credentials_encoded.decode('utf-8')

def token_request(code,redirect_uri):
    headers = {
        "Content-Type":"application/x-www-form-urlencoded",
        "Authorization": f'Basic {authorization}'
        }
    body = {
        "grant_type":"authorization_code",
        "code":code,
        "redirect_uri":f'{redirect_uri}'
        }

    response = requests.post(
    'https://accounts.spotify.com/api/token',
    data=body,
    headers=headers
    )

    token = response.json()
    return token&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm not sure what issue might to give me this error because I sent the same data through cURL and received an expected response.&lt;/P&gt;&lt;P&gt;Example cURL:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;curl -H "Authorization: Basic *&amp;lt;base64 encoded client_id:client_secret&amp;gt;*" -d grant_type=authorization_code -d code=code_from_user_authorization -d redirect_uri=http://my_redirect_uri https://accounts.spotify.com/api/token&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 24 Feb 2021 18:06:19 GMT</pubDate>
    <dc:creator>a_camarillo</dc:creator>
    <dc:date>2021-02-24T18:06:19Z</dc:date>
    <item>
      <title>Invalid Authorization Code - Authorization Code Flow</title>
      <link>https://community.spotify.com/t5/Spotify-for-Developers/Invalid-Authorization-Code-Authorization-Code-Flow/m-p/5159627#M2087</link>
      <description>&lt;P&gt;&lt;STRONG&gt;Plan&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Premium&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Country&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;US&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Device&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;PC&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Operating System&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Windows 10&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;My Question or Issue&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;I'm trying to request an access token using the /token endpoint with python requests library but keep receiving this error: {'error': 'invalid_grant', 'error_description': 'Invalid authorization code'}. Here's a snippet of my code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import os
import base64
import requests

client_id = os.environ.get('CLIENT_ID')
client_secret = os.environ.get('CLIENT_SECRET')

# taking spotify application variables and base64 encoding
credentials = client_id + ':' + client_secret
credentials_bytes = credentials.encode('utf-8')
credentials_encoded = base64.b64encode(credentials_bytes)
authorization = credentials_encoded.decode('utf-8')

def token_request(code,redirect_uri):
    headers = {
        "Content-Type":"application/x-www-form-urlencoded",
        "Authorization": f'Basic {authorization}'
        }
    body = {
        "grant_type":"authorization_code",
        "code":code,
        "redirect_uri":f'{redirect_uri}'
        }

    response = requests.post(
    'https://accounts.spotify.com/api/token',
    data=body,
    headers=headers
    )

    token = response.json()
    return token&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm not sure what issue might to give me this error because I sent the same data through cURL and received an expected response.&lt;/P&gt;&lt;P&gt;Example cURL:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;curl -H "Authorization: Basic *&amp;lt;base64 encoded client_id:client_secret&amp;gt;*" -d grant_type=authorization_code -d code=code_from_user_authorization -d redirect_uri=http://my_redirect_uri https://accounts.spotify.com/api/token&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Feb 2021 18:06:19 GMT</pubDate>
      <guid>https://community.spotify.com/t5/Spotify-for-Developers/Invalid-Authorization-Code-Authorization-Code-Flow/m-p/5159627#M2087</guid>
      <dc:creator>a_camarillo</dc:creator>
      <dc:date>2021-02-24T18:06:19Z</dc:date>
    </item>
  </channel>
</rss>

