我一直在试图获得我的spotify访问令牌和刷新令牌为我的react-native 应用程序和它一直给我严重的问题. https://docs.expo.dev/guides/authentication/#spotify 我能够使用useAuthRequest获取我的代码和状态.现在,我已经try 使用代码来获取我的访问令牌和刷新客户端上的应用程序的令牌,但我一直遇到错误400.因此,判断其他堆栈溢出问题,我意识到在服务器上处理它会更好,所以我决定创建一个express服务器来try 并获得访问代码.
router.post('/get-spotify-access-code',(req: Request, res: Response)=>{
console.log(req.body)
const accessCode = req.body;
var authOptions:AuthOptions = {
url: 'https://accounts.spotify.com/api/token',
form: {
code: accessCode,
redirect_uri: redirectUri,
grant_type: 'authorization_code'
},
headers: {
'content-type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + Buffer.from(clientID + ':' + clientSecret).toString('base64')
},
json: true
};
request.post(authOptions, (error: any, response:any, body:any)=>{
console.log(error);
console.log(response)
if(!error && response.statusCode === 200){
const access_token = body.access_token;
const refresh_token = body.refresh_token;
const expires_in = body.expires_in;
res.json({
'access_token': access_token,
'refresh_token': refresh_token,
'expires_in': expires_in
});
}
})
})
但我仍然得到错误400,我似乎不能找出它.我真的很感激你的回应.下面是我如何处理react本地应用程序的代码,
const [request2, response2, promptAsync2] = useAuthRequest({
clientId: clientID,
clientSecret: clientSecret,
scopes: [
'user-read-playback-state',
'user-modify-playback-state',
'user-read-currently-playing',
'streaming',
'playlist-read-private',
'playlist-read-collaborative',
'playlist-modify-private',
'playlist-modify-public',
'user-follow-modify',
'user-follow-read',
'user-read-playback-position',
'user-library-modify',
'user-library-read',
'user-read-email',
'user-read-private'
],
usePKCE: false,
redirectUri: makeRedirectUri({
scheme: undefined
})
},
discovery
)
useEffect(() => {
if (response2?.type === 'success') {
// get spotify access code
const { code } = response2.params;
const getSpotifyCode = async() =>{
const code2 = {
code
}
await axios.post('http://localhost:8005/get-spotify-access-code', code2).then(
response =>{
console.log(response);
}
).catch(error =>{
console.log(error)
})
}
getSpotifyCode()
}
}, [response2])