[英]Request authorization error in Spotify API
The weirdest thing is happening to my react app.最奇怪的事情发生在我的 React 应用程序上。 I have moved the Client Id and Client Secret into the environment variables and am loading them into this file using dotenv to create an authorization request :
我已将 Client Id 和 Client Secret 移动到环境变量中,并使用 dotenv 将它们加载到此文件中以创建授权请求:
// Inject environment variables
import dotenv from 'dotenv';
dotenv.config();
const clientID = process.env.CLIENT_ID;
const clientSecret = process.env.CLIENT_SECRET;
// Request authorization
const authOptions = {
url: "https://accounts.spotify.com/api/token",
headers: {
"Authorization":
"Basic " +
new Buffer.from(clientID + ":" + clientSecret).toString("base64"),
"Content-Type": "application/x-www-form-urlencoded",
},
form: {
grant_type: "client_credentials",
},
json: true,
};
export default authOptions;
But I'm getting a Failed to load resource: the server responded with a status of 400 ()
error:但是我收到
Failed to load resource: the server responded with a status of 400 ()
错误:
{ error: "invalid_client", error_description: "Invalid client" }
The weird part is that if hard code the client id and secret, it works flawlessly.奇怪的是,如果对客户端 ID 和密码进行硬编码,它就可以完美运行。
I have even printed the environment variables in the console and am getting the correct values but the moment I use them in the authorization header it stops working.我什至在控制台中打印了环境变量并获得了正确的值,但是当我在授权 header 中使用它们时它停止工作。
Is there any way to fix this?有没有什么办法解决这一问题?
From what I see you doing is using an env variable in react which isn't available in the browser.从我看到你所做的是在浏览器中不可用的反应中使用 env 变量。
Just a side note , maybe you shouldn't load secrets in the browser as that isn't a secure environment.
只是一个旁注,也许你不应该在浏览器中加载秘密,因为那不是一个安全的环境。 Rather use the OAuth strategy for using your client id.
而是使用 OAuth 策略来使用您的客户端 ID。 Here's a link
这是一个链接
To load environment variables to your app.将环境变量加载到您的应用程序。 You'd need to prefix it with
REACT_APP_
.您需要在它前面加上
REACT_APP_
。 Here's a link 这是一个链接
Here's an example这是一个例子
.env .env
REACT_APP_CLIENT_ID=my-spotify-client-id
I had this error once.我有一次这个错误。 I reset my
client_secret
, added the new value in the .env
file, but forgot to terminate my app and run it again.我重置了我的
client_secret
,在.env
文件中添加了新值,但忘记终止我的应用程序并再次运行它。 After I rerun my app, it worked.在我重新运行我的应用程序后,它起作用了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.