<?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 Error when trying to create access token in Spotify for Developers</title>
    <link>https://community.spotify.com/t5/Spotify-for-Developers/Error-when-trying-to-create-access-token/m-p/5680299#M11479</link>
    <description>&lt;P&gt;&lt;STRONG&gt;Plan:&amp;nbsp;&lt;/STRONG&gt;Free&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Country: &lt;/STRONG&gt;United States&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Operating System:&amp;nbsp;&lt;/STRONG&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;&lt;SPAN&gt;For my music app, I'm trying to create an access token and use it to fetch my online profile using the Spotify API but I keep getting an error message saying &lt;FONT color="#993300"&gt;'Failed to load resource: the server responded with a status of 400 ()'.&lt;/FONT&gt; I get this error when trying to create the access token. Could anyone tell me what could be going on?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Here's a TypeScript code snippet that shows what I'm trying to do: &lt;/SPAN&gt;&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="javascript"&gt;export async function getAccessToken(clientId: string, code: string): Promise&amp;lt;string | void&amp;gt; {
  const verifier = localStorage.getItem("verifier");

  const params = new URLSearchParams();
  params.append("client_id", clientId);
  params.append("grant_type", "authorization_code");
  params.append("code", code);
  params.append("redirect_uri", "http://localhost:5173/profile");
  params.append("code_verifier", verifier!);

  try {
    const result = await fetch("https://accounts.spotify.com/api/token", {
      method: "POST",
      headers: { "Content-Type": "application/x-www-form-urlencoded" },
      body: params
    });

    const { access_token } = await result.json();
    return access_token;

  } catch (error) {
    console.log("No access token created: " + error)
    throw error
  }  
}

export async function fetchProfile(token: string | void): Promise&amp;lt;UserProfileObject | string&amp;gt; {
  if (token) {
    const result = await fetch("https://api.spotify.com/v1/me", {
      method: "GET", headers: { Authorization: `Bearer ${token}` }
    });
    return await result.json();
  } else {
    return "No profile fetched"
  }
}&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;Also, here's a link to my app project:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://drive.google.com/drive/folders/1z11P4kmI_D3dhWijQGTCl2oV9B7B9XpD?usp=drive_link" target="_blank"&gt;https://drive.google.com/drive/folders/1z11P4kmI_D3dhWijQGTCl2oV9B7B9XpD?usp=drive_link&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 16 Nov 2023 21:46:09 GMT</pubDate>
    <dc:creator>gijutaku23</dc:creator>
    <dc:date>2023-11-16T21:46:09Z</dc:date>
    <item>
      <title>Error when trying to create access token</title>
      <link>https://community.spotify.com/t5/Spotify-for-Developers/Error-when-trying-to-create-access-token/m-p/5680299#M11479</link>
      <description>&lt;P&gt;&lt;STRONG&gt;Plan:&amp;nbsp;&lt;/STRONG&gt;Free&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Country: &lt;/STRONG&gt;United States&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Operating System:&amp;nbsp;&lt;/STRONG&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;&lt;SPAN&gt;For my music app, I'm trying to create an access token and use it to fetch my online profile using the Spotify API but I keep getting an error message saying &lt;FONT color="#993300"&gt;'Failed to load resource: the server responded with a status of 400 ()'.&lt;/FONT&gt; I get this error when trying to create the access token. Could anyone tell me what could be going on?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Here's a TypeScript code snippet that shows what I'm trying to do: &lt;/SPAN&gt;&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="javascript"&gt;export async function getAccessToken(clientId: string, code: string): Promise&amp;lt;string | void&amp;gt; {
  const verifier = localStorage.getItem("verifier");

  const params = new URLSearchParams();
  params.append("client_id", clientId);
  params.append("grant_type", "authorization_code");
  params.append("code", code);
  params.append("redirect_uri", "http://localhost:5173/profile");
  params.append("code_verifier", verifier!);

  try {
    const result = await fetch("https://accounts.spotify.com/api/token", {
      method: "POST",
      headers: { "Content-Type": "application/x-www-form-urlencoded" },
      body: params
    });

    const { access_token } = await result.json();
    return access_token;

  } catch (error) {
    console.log("No access token created: " + error)
    throw error
  }  
}

export async function fetchProfile(token: string | void): Promise&amp;lt;UserProfileObject | string&amp;gt; {
  if (token) {
    const result = await fetch("https://api.spotify.com/v1/me", {
      method: "GET", headers: { Authorization: `Bearer ${token}` }
    });
    return await result.json();
  } else {
    return "No profile fetched"
  }
}&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;Also, here's a link to my app project:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://drive.google.com/drive/folders/1z11P4kmI_D3dhWijQGTCl2oV9B7B9XpD?usp=drive_link" target="_blank"&gt;https://drive.google.com/drive/folders/1z11P4kmI_D3dhWijQGTCl2oV9B7B9XpD?usp=drive_link&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Nov 2023 21:46:09 GMT</pubDate>
      <guid>https://community.spotify.com/t5/Spotify-for-Developers/Error-when-trying-to-create-access-token/m-p/5680299#M11479</guid>
      <dc:creator>gijutaku23</dc:creator>
      <dc:date>2023-11-16T21:46:09Z</dc:date>
    </item>
    <item>
      <title>Re: Error when trying to create access token</title>
      <link>https://community.spotify.com/t5/Spotify-for-Developers/Error-when-trying-to-create-access-token/m-p/5684629#M11506</link>
      <description>&lt;P&gt;Hello @gijutaku23,&lt;/P&gt;&lt;P&gt;the issue you are experiencing is maybe linked to the authorized redirect URIs you added for your app (you can change that in your app's settings &amp;gt; Redirect URIs). Maybe make sure you added the correct one.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am assuming you are trying to use authentification by authorization code with PCKE, but I am not quite sure you are providing all the required parameters (I feel there should be a code_challenge parameter somewhere), maybe consider reading or re-reading this page&amp;nbsp;&lt;A href="https://developer.spotify.com/documentation/web-api/tutorials/code-pkce-flow" target="_blank"&gt;https://developer.spotify.com/documentation/web-api/tutorials/code-pkce-flow&lt;/A&gt;&amp;nbsp;it could help it is quite clear.&lt;/P&gt;&lt;P&gt;Hope this helps&lt;/P&gt;</description>
      <pubDate>Sat, 18 Nov 2023 22:09:16 GMT</pubDate>
      <guid>https://community.spotify.com/t5/Spotify-for-Developers/Error-when-trying-to-create-access-token/m-p/5684629#M11506</guid>
      <dc:creator>the-cellist</dc:creator>
      <dc:date>2023-11-18T22:09:16Z</dc:date>
    </item>
    <item>
      <title>Re: Error when trying to create access token</title>
      <link>https://community.spotify.com/t5/Spotify-for-Developers/Error-when-trying-to-create-access-token/m-p/5688173#M11521</link>
      <description>&lt;P&gt;@the-cellist&amp;nbsp; Thank you for responding.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the same redirect URI added across all of my project files and within my app's settings, and yes I am using the PCKE method for authentication. I have provided the link to my project folder in the original post. All of the functions, specifically the ones used for authentication, are in the &lt;FONT face="courier new,courier"&gt;`utilities.ts`&amp;nbsp;&lt;FONT face="arial,helvetica,sans-serif"&gt;file.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Nov 2023 18:07:04 GMT</pubDate>
      <guid>https://community.spotify.com/t5/Spotify-for-Developers/Error-when-trying-to-create-access-token/m-p/5688173#M11521</guid>
      <dc:creator>gijutaku23</dc:creator>
      <dc:date>2023-11-20T18:07:04Z</dc:date>
    </item>
  </channel>
</rss>

