【发布时间】:2022-11-23 10:25:30
【问题描述】:
我对 JavaScript 和 React 还很陌生,我目前正在 ReactNative 中创建一个应用程序,并且在使用 Spotify API 进行身份验证时遇到了问题。目前我只是想从 API 中获取顶级艺术家和歌曲并查看它们。我正在使用 expo 来测试该应用程序,使用我的 Spotify 帐户它可以正常工作(用于开发应用程序的同一帐户).
我可以进行身份验证,然后检查热门歌曲和艺术家,但是一旦我在另一台设备上使用不同的 Spotify 对其进行测试,我就可以进行身份验证,但是当我尝试查看我的热门歌曲/艺术家时,我得到了错误[AxiosError: Request failed with status code 403].还值得一提的是查看统计数据第一次工作但是在我收到该错误后的任何时候,即使在重新运行 expo 或刷新之后也是如此。
这是我的 Spotify 身份验证页面代码。
import React, { useEffect, useState } from "react";
import { ResponseType, useAuthRequest } from "expo-auth-session";
import * as WebBrowser from "expo-web-browser";
import useAuth from "../../hooks/useAuth";
import { setSpotifyToken, getSpotifyToken } from "../../hooks/spotifyAuth";
import {
View,
Button,
Text,
StyleSheet,
Image,
SafeAreaView,
TouchableOpacity,
} from "react-native";
import axios from "axios";
import * as Linking from "expo-linking";
WebBrowser.maybeCompleteAuthSession();
const ConnectSpotifyScreen = () => {
//Getting hook for sign out button
const { signOut } = useAuth();
const { spotifyToken, setSpotifyToken } = useAuth();
//Spotify API information
const discovery = {
authorizationEndpoint: "https://accounts.spotify.com/authorize",
tokenEndpoint: "https://accounts.spotify.com/api/token",
};
const [request, response, promptAsync] = useAuthRequest(
{
responseType: ResponseType.Token,
clientId: "ecf0bbd85d8c4456a8551dc30224ee83",
clientSecret: "SECRET",
scopes: [
"user-read-recently-played",
"user-top-read",
"user-read-email",
"user-read-private",
],
usePKCE: false,
redirectUri: "exp://10.0.0.40:19000",
},
discovery
);
//Runs when page is opened, retrieves token from spotify and stores it
React.useEffect(() => {
if (response?.type === "success") {
const { access_token } = response.params;
setSpotifyToken(access_token);
console.log("accessToken", access_token);
}
}, [response]);
//Page to be rendered
return (
<SafeAreaView style={styles.container}>
<View style={styles.container}>
<View style={styles.headerButton}>
<Text style={{ fontSize: 24 }}>Spotify Authentication</Text>
</View>
<Image
style={styles.logo}
source={require("../../../assets/spotifyLogo.png")}
/>
<TouchableOpacity style={styles.button}>
<Button
light
disabled={!request}
title="Connect to Spotify"
onPress={() => {
promptAsync();
}}
/>
</TouchableOpacity>
<TouchableOpacity style={styles.button}>
<Button onPress={signOut} title="Sign out"></Button>
</TouchableOpacity>
<View style={styles.button}>
<Text style={{ textAlign: "center", fontWeight: "bold" }}>
Note: After authenticating, please sign out and sign back in!
</Text>
</View>
</View>
</SafeAreaView>
);
};
//Styling sheet for the page
这是我设置和获取用户 Spotify 令牌的代码。
import React, { useEffect, useState } from 'react'
//Initially setting the token to null
var spotifyToken = null;
//Function to set the token
function setSpotifyToken(token) {
spotifyToken = token;
}
//Function to get the token
function getSpotifyToken() {
return spotifyToken;
}
export { setSpotifyToken, getSpotifyToken };
我来自 Java 背景,所以请原谅糟糕的语法和糟糕的代码。先感谢您!
【问题讨论】:
标签: javascript reactjs react-native api spotify