- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
From what I am reading it is a bad OAuth request, but I cannot identify what I am doing wrong. TIA!
import { useEffect, useState } from "react";
import "./App.css";
import axios from "axios";
function App() {
const CLIENT_ID = "501594440ad341babe6b49b9db06b908";
const REDIRECT_URI = "http://localhost:3002";
const AUTH_ENDPOINT = "https://accounts.spotify.com/authorize";
const RESPONSE_TYPE = "token";
const [token, setToken] = useState("");
const [data, setData] = useState([]);
// on login
useEffect(() => {
const hash = window.location.hash;
let token = window.localStorage.getItem("token");
if (!token && hash) {
token = hash
.substring(1)
.split("&")
.find((elem) => elem.startsWith("access_token"))
.split("=")[1];
window.location.hash = "";
window.localStorage.setItem("token", token);
}
setToken(token);
// getTopArtists();
}, []);
const getTopArtists = async () => {
console.log(token);
const { data } = await axios.get(
"https://api.spotify.com/v1/me/top/artists",
{
headers: {
Authorization: `Bearer ${token}`,
},
}
);
console.log(token);
console.log(data);
// setData(data);
};
return (
<div className="App">
<header className="App-header">
<h1>Spotify React</h1>
{!token && (
<a
href={`${AUTH_ENDPOINT}?client_id=${CLIENT_ID}&redirect_uri=${REDIRECT_URI}&response_type=${RESPONSE_TYPE}`}
>
Login to Spotify
</a>
)}
<button onClick={getTopArtists}>Get Top Artists</button>
</header>
</div>
);
}
export default App;
Solved! Go to Solution.
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page