<?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 Re: Unable to obtain a refresh token in Spotify for Developers</title>
    <link>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-obtain-a-refresh-token/m-p/5805621#M12207</link>
    <description>&lt;P&gt;Edit: Ignore me. I didn't scroll down enough to see that line of code, and the search function couldn't find it also for some reason.&lt;/P&gt;</description>
    <pubDate>Tue, 09 Jan 2024 17:48:28 GMT</pubDate>
    <dc:creator>Ximzend</dc:creator>
    <dc:date>2024-01-09T17:48:28Z</dc:date>
    <item>
      <title>Unable to obtain a refresh token</title>
      <link>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-obtain-a-refresh-token/m-p/5797788#M12151</link>
      <description>&lt;P&gt;I would like to get a refresh_token in Next.js, but no matter what I do, I cannot get one.&lt;/P&gt;&lt;P&gt;I would like to know how to do this.&lt;/P&gt;&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;Japan&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;The code is as follows&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;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;import { redirect_uri, client_id, client_secret, encode } from "@/module/env";

export default async function handle(req, res) {
  if (req.method === "POST") {
    const { code } = req.body;
    const url = "https://accounts.spotify.com/api/token";
    const params = new URLSearchParams();
    params.append("grant_type", "authorization_code");
    params.append("code", code);
    params.append("client_id", client_id || "");
    params.append("client_secret", client_secret || "");
    params.append("redirect_uri", redirect_uri || "/callback");
    const payload = {
      method: "POST",
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      body: params
    };
    const body = await fetch(url, payload);
    const response = await body.json();
    res.status(200).json(response);
  } else {
    res.status(405).json({ message: "We only support POST" }).end;
  }
}

export const config = {
  api: {
    externalResolver: true,
  },
};&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;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Jan 2024 19:08:07 GMT</pubDate>
      <guid>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-obtain-a-refresh-token/m-p/5797788#M12151</guid>
      <dc:creator>nitadori</dc:creator>
      <dc:date>2024-01-05T19:08:07Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to obtain a refresh token</title>
      <link>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-obtain-a-refresh-token/m-p/5798393#M12154</link>
      <description>&lt;P&gt;I mean based off of just that code, it doesn't seem like you are including all the necessary information, like the redirect_url, client secret, etc&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;import { redirect_uri, client_id, client_secret } from "@/module/env";&lt;/P&gt;&lt;P&gt;export default async function handle(req, res) {&lt;BR /&gt;if (req.method === "POST") {&lt;BR /&gt;try {&lt;BR /&gt;const { code } = req.body;&lt;BR /&gt;const url = "&lt;A href="https://accounts.spotify.com/api/token" target="_blank"&gt;https://accounts.spotify.com/api/token&lt;/A&gt;";&lt;BR /&gt;const params = new URLSearchParams();&lt;BR /&gt;params.append("grant_type", "authorization_code");&lt;BR /&gt;params.append("code", code);&lt;BR /&gt;params.append("client_id", client_id);&lt;BR /&gt;params.append("client_secret", client_secret);&lt;BR /&gt;params.append("redirect_uri", redirect_uri);&lt;/P&gt;&lt;P&gt;const payload = {&lt;BR /&gt;method: "POST",&lt;BR /&gt;headers: {&lt;BR /&gt;"Content-Type": "application/x-www-form-urlencoded",&lt;BR /&gt;},&lt;BR /&gt;body: params&lt;BR /&gt;};&lt;/P&gt;&lt;P&gt;const response = await fetch(url, payload);&lt;BR /&gt;const data = await response.json();&lt;/P&gt;&lt;P&gt;if (data.refresh_token) {&lt;BR /&gt;// Handle successful response&lt;BR /&gt;res.status(200).json(data);&lt;BR /&gt;} else {&lt;BR /&gt;// Handle missing refresh token&lt;BR /&gt;res.status(500).json({ message: "Refresh token not received", data });&lt;BR /&gt;}&lt;BR /&gt;} catch (error) {&lt;BR /&gt;// Handle any errors that occurred during the request&lt;BR /&gt;res.status(500).json({ message: "Error occurred", error: error.message });&lt;BR /&gt;}&lt;BR /&gt;} else {&lt;BR /&gt;res.status(405).json({ message: "We only support POST" });&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;export const config = {&lt;BR /&gt;api: {&lt;BR /&gt;externalResolver: true,&lt;BR /&gt;},&lt;BR /&gt;};&lt;/P&gt;</description>
      <pubDate>Sat, 06 Jan 2024 01:25:42 GMT</pubDate>
      <guid>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-obtain-a-refresh-token/m-p/5798393#M12154</guid>
      <dc:creator>NathanG2</dc:creator>
      <dc:date>2024-01-06T01:25:42Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to obtain a refresh token</title>
      <link>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-obtain-a-refresh-token/m-p/5798418#M12156</link>
      <description>&lt;P&gt;I ran your code after making sure that client_id, client_secret, redirect_uri, code all exist, but I get the following error.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;{
"error":"invalid_grant",
"error_description":"Invalid authorization code"
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 06 Jan 2024 01:49:59 GMT</pubDate>
      <guid>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-obtain-a-refresh-token/m-p/5798418#M12156</guid>
      <dc:creator>nitadori</dc:creator>
      <dc:date>2024-01-06T01:49:59Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to obtain a refresh token</title>
      <link>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-obtain-a-refresh-token/m-p/5798422#M12157</link>
      <description>&lt;P&gt;Double check everything in the Spotify Developer Dashboard matches up. Auth codes only last like a minute, so you need to make sure you're getting the access token correctly.&lt;BR /&gt;&lt;BR /&gt;Also I hope you didn't run that code as is lol&lt;BR /&gt;&lt;BR /&gt;'&lt;SPAN&gt;params.append("grant_type", "authorization_code");' wont work&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 06 Jan 2024 01:53:17 GMT</pubDate>
      <guid>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-obtain-a-refresh-token/m-p/5798422#M12157</guid>
      <dc:creator>NathanG2</dc:creator>
      <dc:date>2024-01-06T01:53:17Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to obtain a refresh token</title>
      <link>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-obtain-a-refresh-token/m-p/5798562#M12158</link>
      <description>&lt;P&gt;I apologize for my ignorance, but how do I get a refresh token?&lt;BR /&gt;I ran the code immediately after getting it and got the latest client_id and client_secret from the dashboard&lt;/P&gt;</description>
      <pubDate>Sat, 06 Jan 2024 02:43:10 GMT</pubDate>
      <guid>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-obtain-a-refresh-token/m-p/5798562#M12158</guid>
      <dc:creator>nitadori</dc:creator>
      <dc:date>2024-01-06T02:43:10Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to obtain a refresh token</title>
      <link>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-obtain-a-refresh-token/m-p/5798650#M12159</link>
      <description>&lt;P&gt;&lt;FONT&gt;Do I have to rebuild the app.....？&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jan 2024 13:16:22 GMT</pubDate>
      <guid>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-obtain-a-refresh-token/m-p/5798650#M12159</guid>
      <dc:creator>nitadori</dc:creator>
      <dc:date>2024-01-09T13:16:22Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to obtain a refresh token</title>
      <link>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-obtain-a-refresh-token/m-p/5805247#M12202</link>
      <description>&lt;P&gt;I have no idea. .....&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jan 2024 13:17:16 GMT</pubDate>
      <guid>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-obtain-a-refresh-token/m-p/5805247#M12202</guid>
      <dc:creator>nitadori</dc:creator>
      <dc:date>2024-01-09T13:17:16Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to obtain a refresh token</title>
      <link>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-obtain-a-refresh-token/m-p/5805274#M12204</link>
      <description>&lt;P&gt;You have to remove the following line:&lt;/P&gt;&lt;P&gt;&lt;EM&gt;params.append("grant_type", "authorization_code");&lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jan 2024 13:43:46 GMT</pubDate>
      <guid>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-obtain-a-refresh-token/m-p/5805274#M12204</guid>
      <dc:creator>Ximzend</dc:creator>
      <dc:date>2024-01-09T13:43:46Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to obtain a refresh token</title>
      <link>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-obtain-a-refresh-token/m-p/5805284#M12205</link>
      <description>&lt;P&gt;Thanks for the reply.&lt;/P&gt;&lt;P&gt;I removed &lt;EM&gt;params.append("grant_type", "authorization_code"); and ran the following code I get the same error..... &lt;/EM&gt;&lt;/P&gt;&lt;P&gt;I tried to recreate the app 3 times to try, but no luck.....&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;    let code = req.body.code;
    const params = new URLSearchParams();
    params.append("redirect_uri", redirect_uri || "");
    params.append("code", code);
    params.append("grant_type", "authorization_code");
    params.append("client_id", public_client_id || "");
    const url = "https://accounts.spotify.com/api/token";
    const response = await fetch(url, {
      method: "POST",
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
        "Authorization": "Basic " + Buffer.from(public_client_id + ":" + client_secret).toString("base64"),
      },
      body: params
    });&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 09 Jan 2024 13:50:34 GMT</pubDate>
      <guid>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-obtain-a-refresh-token/m-p/5805284#M12205</guid>
      <dc:creator>nitadori</dc:creator>
      <dc:date>2024-01-09T13:50:34Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to obtain a refresh token</title>
      <link>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-obtain-a-refresh-token/m-p/5805621#M12207</link>
      <description>&lt;P&gt;Edit: Ignore me. I didn't scroll down enough to see that line of code, and the search function couldn't find it also for some reason.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jan 2024 17:48:28 GMT</pubDate>
      <guid>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-obtain-a-refresh-token/m-p/5805621#M12207</guid>
      <dc:creator>Ximzend</dc:creator>
      <dc:date>2024-01-09T17:48:28Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to obtain a refresh token</title>
      <link>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-obtain-a-refresh-token/m-p/5814461#M12257</link>
      <description>&lt;P&gt;didn't resolve&lt;/P&gt;</description>
      <pubDate>Sun, 14 Jan 2024 00:36:06 GMT</pubDate>
      <guid>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-obtain-a-refresh-token/m-p/5814461#M12257</guid>
      <dc:creator>nitadori</dc:creator>
      <dc:date>2024-01-14T00:36:06Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to obtain a refresh token</title>
      <link>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-obtain-a-refresh-token/m-p/5844855#M12464</link>
      <description>&lt;P&gt;I'm new to this so apologies if this is obvious but what tripped me up is that the /refresh_token route expects&amp;nbsp;&lt;SPAN&gt;a refresh_token query as a parameter. I tried manually copying and pasting the refresh token in the URL like this to make sure the code works:&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&lt;A href="http://localhost:3000/refresh_token?refresh_token=YOUR_REFRESH_TOKEN" target="_blank"&gt;http://localhost:3000/refresh_token?refresh_token=YOUR_REFRESH_TOKEN&lt;/A&gt;&lt;BR /&gt;Hope it helps.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 28 Jan 2024 10:42:16 GMT</pubDate>
      <guid>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-obtain-a-refresh-token/m-p/5844855#M12464</guid>
      <dc:creator>cdusfim</dc:creator>
      <dc:date>2024-01-28T10:42:16Z</dc:date>
    </item>
  </channel>
</rss>

