<?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: 401 Error in API Call. in Spotify for Developers</title>
    <link>https://community.spotify.com/t5/Spotify-for-Developers/401-Error-in-API-Call/m-p/5655229#M11183</link>
    <description>&lt;P&gt;You have helped me so much!!! I had a very similar problem, and reading this clarified the issue for me, Thank you!!&lt;/P&gt;</description>
    <pubDate>Fri, 20 Oct 2023 18:42:06 GMT</pubDate>
    <dc:creator>Liz994</dc:creator>
    <dc:date>2023-10-20T18:42:06Z</dc:date>
    <item>
      <title>401 Error in API Call.</title>
      <link>https://community.spotify.com/t5/Spotify-for-Developers/401-Error-in-API-Call/m-p/4973202#M428</link>
      <description>&lt;P&gt;I'm a design trying to learn to code through the Spotify API. I'm having a heck of a time getting my access_tokens saved to state and wondering if anyone has any advice. The code below will return a 401 but when I console.log(accessToken) it appears to be correct. Am I missing a syntax error here or something obvious?&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;const [user, setUser] = useState();
const [accessToken, setaccessToken] = useState();
const [refreshToken, setrefreshToken] = useState();
  

useEffect(() =&amp;gt; {
   extractTokens();
   spotifyAuth(); 
  }, []);
    
    //Parse URL sring to get the access token from Spotify
    const hashparam = window.location.hash
    .substring(1)
    .split("&amp;amp;")
    .reduce(function(initial, item) {
      if (item) {
        var parts = item.split("=");
        initial[parts[0]] = decodeURIComponent(parts[1]);
      }
      return initial;
    }, {});

    //Extract access_token from hashparam
    const extractTokens = () =&amp;gt; {
        setaccessToken(hashparam.access_token);
        setrefreshToken(hashparam.refresh_token);
    }


    console.log(accessToken);

    //Fetch data from the Spotify Endpoint
    const spotifyAuth = async () =&amp;gt; {
        const settings = {
            headers: {'Authorization': 'Bearer ' + accessToken}
        }
        const response = await fetch('https://api.spotify.com/v1/me', settings);
        const data = await response.json();
        setUser(data);
    }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jun 2020 03:59:31 GMT</pubDate>
      <guid>https://community.spotify.com/t5/Spotify-for-Developers/401-Error-in-API-Call/m-p/4973202#M428</guid>
      <dc:creator>sickleandsycthe</dc:creator>
      <dc:date>2020-06-09T03:59:31Z</dc:date>
    </item>
    <item>
      <title>Re: 401 Error in API Call.</title>
      <link>https://community.spotify.com/t5/Spotify-for-Developers/401-Error-in-API-Call/m-p/4974471#M455</link>
      <description>&lt;P&gt;If any other novices else end up here, I solved the problem like this. The API request was being made before the accessToken had been set, so I separated the useEffect (among other things) to ensure the API call wouldn't trigger before accessToken was set. I'll probably consolidate some but the code below works:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;//Fetch data from the Spotify Endpoint
const getUserData = async (token) =&amp;gt; {
const settings = {
headers: {'Authorization': 'Bearer ' + token}
}
const response = await fetch('https://api.spotify.com/v1/me', settings);
const data = await response.json()
return data;
}

const App = () =&amp;gt; {
const [showNavPanel, setShowNavPanel] = useState(false)
const closeNavPanel = () =&amp;gt; {setShowNavPanel(false)}

const [user, setUser] = useState();
const [accessToken, setaccessToken] = useState(null);
const [refreshToken, setrefreshToken] = useState(null);

//Parse URL sring to get the access token from Spotify
const hashparam = window.location.hash
.substring(1)
.split("&amp;amp;")
.reduce(function(initial, item) {
if (item) {
var parts = item.split("=");
initial[parts[0]] = decodeURIComponent(parts[1]);
}
return initial;
}, {});
//Set accessToken and refreshToken from URL returned from Server Auth
useEffect(() =&amp;gt; {
setaccessToken(hashparam.access_token);
setrefreshToken(hashparam.refresh_token);
}, []);

// Get User data from Spotify endpoint once accessToken is set.
useEffect(() =&amp;gt; {
async function fetchData() {
if(accessToken != null) {
setUser(await getUserData(accessToken));
}
}
fetchData();
}, [accessToken]);&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jun 2020 22:24:38 GMT</pubDate>
      <guid>https://community.spotify.com/t5/Spotify-for-Developers/401-Error-in-API-Call/m-p/4974471#M455</guid>
      <dc:creator>sickleandsycthe</dc:creator>
      <dc:date>2020-06-10T22:24:38Z</dc:date>
    </item>
    <item>
      <title>Re: 401 Error in API Call.</title>
      <link>https://community.spotify.com/t5/Spotify-for-Developers/401-Error-in-API-Call/m-p/5655229#M11183</link>
      <description>&lt;P&gt;You have helped me so much!!! I had a very similar problem, and reading this clarified the issue for me, Thank you!!&lt;/P&gt;</description>
      <pubDate>Fri, 20 Oct 2023 18:42:06 GMT</pubDate>
      <guid>https://community.spotify.com/t5/Spotify-for-Developers/401-Error-in-API-Call/m-p/5655229#M11183</guid>
      <dc:creator>Liz994</dc:creator>
      <dc:date>2023-10-20T18:42:06Z</dc:date>
    </item>
  </channel>
</rss>

