簡體   English   中英

SpotifyWebApi - nodeJS 中的身份驗證(授權碼)問題

[英]SpotifyWebApi - Trouble with authentication (Authorization Code) in nodeJS

我正在嘗試使用 spotify API 開發一個簡單的應用程序,但我無法進行身份驗證。 在前端,我有一個按鈕向 /authorize 發送調用,如下所示:

var CLIENT_ID = 'blabla'; // Your client id
var CLIENT_SECRET = 'blablabla'; // Your secret
var REDIRECT_URI = 'http://localhost:4001/logincallback';
const STATE_KEY = "spotify_auth_state";
const scopes = ['user-read-private', 'user-read-email'];

const spotifyApi = new Spotify({
  clientId: CLIENT_ID,
  clientSecret: CLIENT_SECRET,
  redirectUri: REDIRECT_URI
});

/**
 * The /login endpoint
 * Redirect the client to the spotify authorize url, but first set that user's
 * state in the cookie.
 */
const generateRandomString = N => (Math.random().toString(36)+Array(N).join('0')).slice(2, N+2);

router.get('/', (_, res) => {
  const state = generateRandomString(16);
  res.cookie(STATE_KEY, state);
  var authorized_url = spotifyApi.createAuthorizeURL(scopes, state);
  res.redirect(authorized_url);
});

module.exports = router;

然后在 /logincallback 我有

var CLIENT_ID = 'blabla'; // Your client id
var CLIENT_SECRET = 'blabla'; // Your secret


router.get('/', (req, res) => {
  const { code, state } = req.query;
  const storedState = req.cookies ? req.cookies[STATE_KEY] : null;
  // first do state validation
  if (state === null || state !== storedState) {
    res.redirect('/#/error/state mismatch');
    console.log("Incorrect state");
  // if the state is valid, get the authorization code and pass it on to the client
  } else {
    res.clearCookie(STATE_KEY);
    // Retrieve an access token and a refresh token
    spotifyApi.authorizationCodeGrant(code).then(data => {
      const { expires_in, access_token, refresh_token } = data.body;
      // Set the access token on the API object to use it in later calls
      spotifyApi.setAccessToken(access_token);
      spotifyApi.setRefreshToken(refresh_token);

      // use the access token to access the Spotify Web API
      spotifyApi.getMe().then(({ body }) => {
        console.log(body);
      });

      // we can also pass the token to the browser to make requests from there
      res.redirect(`/#/user/${access_token}/${refresh_token}`);
    }).catch(err => {
      console.log(err);
      res.redirect('/#/error/invalid token');
    });
  }
});

這都是直接從 Spotify API 示例中復制的。 但是,對 res.redirect(authorized_url) 的調用似乎沒有觸發 - 例如,我收到了對 /authorize 的請求,但沒有對 /logincallback 的后續請求。 如果我執行 res.redirect('/logincallback'),API 將響應錯誤請求。 我使用 Postman 將沒有任何參數的請求直接發送到 /logincallback,但這顯然失敗了,因為它缺少第一次調用 Spotify API 時提供的信息。 對 /authorize 的郵遞員請求給了我一個我已將這些路由添加到開發人員儀表板中的白名單:

https://localhost:4001/logincallback/ 
http://localhost:4001/logincallback/ 
https://localhost:4001/logincallback
http://localhost:4001/logincallback 
https://localhost:4001/

簡短版本:使用 spotifyApi.createAuthorizeURL 調用 Spotify API 不會觸發

http://localhost:4001/logincallback為 Spotify 上的回調 API 不起作用,因為 Spotify 服務器不知道將請求路由到哪里。 我讓它工作的方式是在heroku上部署我的應用程序(即獲取一個公共端點)並注冊回調。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM