0

I'm trying to get a grant code and every time I make a request with my respective clientID and clientSecret I get a 400 response error and the catch statement executes:

Details: invalid_grant Invalid redirect URI

From my perspective its as if

Some posts say that the redirect URI has to be the exact same in the developer dashboard and in the request header but it gives the exact same result. Here's my current node.js file:

const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
const SpotifyWebApi = require("spotify-web-api-node");

const app = express();
app.use(cors());
app.use(bodyParser.json());

app.post("/login", (req, res) => {
    const code = req.body.code;
    const spotifyApi = new SpotifyWebApi({
        redirectUri: "http://localhost:3000/",
        clientId: "someCode",
        clientSecret: "someCode",
    });

    spotifyApi
        .authorizationCodeGrant(code)
        .then((data) => {
            res.json({
                accessToken: data.body.access_token,
                refreshToken: data.body.refresh_token,
                expiresIn: data.body.expires_in,
            });
        })
        .catch((err) => {
            console.log(err)
            res.sendStatus(400);
        });
});

app.listen(3001);

This is the custom hook making the request:

import { useEffect, useState } from "react";
import axios from "axios";

export default function useAuth(code) {
    const [accessToken, setAccessToken] = useState();
    const [refreshToken, setRefreshToken] = useState();
    const [expiresInToken, setExpiresIn] = useState();

    useEffect(() => {
        axios
            .post("http://localhost:3001/login", {
                code,
            })
            .then((res) => {
                window.history.pushState({}, null, "/");
                console.log(res.data);
            })
            .catch(() => {
                window.location = "/";
            });
    }, [code]);
}

yonatan goldin
  • 91
  • 1
  • 10
  • Does [this question](https://stackoverflow.com/questions/32956443/invalid-redirect-uri-on-spotify-auth) help at all? – Alan Friedman Jul 16 '22 at 18:13
  • Saw this and also referred to it in the question, maybe I didn't get it but I tried what was said there. Thanks for the suggestion tho. – yonatan goldin Jul 16 '22 at 19:01
  • i also know every time i log in with Spotify i get a different code, so maybe the info given to the SpotifyWebApi class is incorrect? – yonatan goldin Jul 17 '22 at 14:39

0 Answers0