坏令牌+ Spotify api 上没有刷新令牌
我正在使用 Spotify API 制作一个应用程序,当我尝试获取访问令牌和刷新令牌时遇到问题。在 json 响应中,我没有任何刷新令牌,并且给定的访问令牌不起作用(与我直接在 Spotify 网站上获得的访问令牌相比,太短了。 如果您发现有问题,请告诉我(Spotify abi 基于 Oauth2.0)
这是我的代码
try {
String urlString = "https://accounts.spotify.com/api/token?";
URL website = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) website.openConnection();
connection.setRequestMethod("POST");
// Headers
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setDoOutput(true);
// Add parameters to the body
HashMap<String, String> params = new HashMap<>();
params.put("grant_type", "client_credentials");
params.put("redirect_uri", ID.REDIRECT_URI);
params.put("code", code);
params.put("client_id", ID.CLIENT_ID);
params.put("client_secret", ID.CLIEN_SECRET_ID);
OutputStream os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(os, StandardCharsets.UTF_8));
writer.write(getPostDataString(params));
writer.flush();
writer.close();
os.close();
// Open the connection
connection.connect();
JsonObject jsonResponse = Http.statusResponse(connection);
// Close the connection
connection.disconnect();
System.out.println(jsonResponse);
return jsonResponse;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
这是我得到的:
{“access_token”:“BQAQxzMFIqOY2vk9aWintAgOilaY77N6s-xL2nyHmVzWMsnu4t3wmvGJ-EK_2MDMXvniBEeYoydvbYZpxOY”,“token_type”:“承载”,“expires_in”:3600}
这是我应该得到的(基于spotify指南) : https://developer.spotify.com/documentation/general/指南/授权/代码流/)
{ "access_token": "NgCXRK...MzYjw", "token_type": "承载者", "scope": "用户读取私人用户读取电子邮件", “过期时间”:3600, "refresh_token": "NgAagA...Um_SHo" }
i'm making an application with the spotify API, when I try to get te access token and the refresh token I have a problem. On the json response I don't have any refresh token and the given access token does not work (way too short in comparison on the one I get directly on the spotify website.
Please tell me if you see something wrong on it (The spotify abi is based on Oauth2.0)
Here is my code
try {
String urlString = "https://accounts.spotify.com/api/token?";
URL website = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) website.openConnection();
connection.setRequestMethod("POST");
// Headers
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setDoOutput(true);
// Add parameters to the body
HashMap<String, String> params = new HashMap<>();
params.put("grant_type", "client_credentials");
params.put("redirect_uri", ID.REDIRECT_URI);
params.put("code", code);
params.put("client_id", ID.CLIENT_ID);
params.put("client_secret", ID.CLIEN_SECRET_ID);
OutputStream os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(os, StandardCharsets.UTF_8));
writer.write(getPostDataString(params));
writer.flush();
writer.close();
os.close();
// Open the connection
connection.connect();
JsonObject jsonResponse = Http.statusResponse(connection);
// Close the connection
connection.disconnect();
System.out.println(jsonResponse);
return jsonResponse;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
This is what I get:
{"access_token":"BQAQxzMFIqOY2vk9aWintAgOilaY77N6s-xL2nyHmVzWMsnu4t3wmvGJ-EK_2MDMXvniBEeYoydvbYZpxOY","token_type":"Bearer","expires_in":3600}
This is what I should get (based on the spotify guide : https://developer.spotify.com/documentation/general/guides/authorization/code-flow/)
{
"access_token": "NgCXRK...MzYjw",
"token_type": "Bearer",
"scope": "user-read-private user-read-email",
"expires_in": 3600,
"refresh_token": "NgAagA...Um_SHo"
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
grant_type
参数应为authorization_code
而不是client_credentials
。client_credentials
授予类型不授予对授权范围的访问权限。所以使用 params.put("grant_type", "authorization_code");
希望有帮助!
Your
grant_type
parameter should beauthorization_code
instead ofclient_credentials
. Theclient_credentials
grant type does not give access to authorization scopes.So use
params.put("grant_type", "authorization_code");
Hope that helps!