坏令牌+ Spotify api 上没有刷新令牌

发布于 2025-01-11 02:03:57 字数 2377 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薔薇婲 2025-01-18 02:03:57

您的 grant_type 参数应为 authorization_code 而不是 client_credentialsclient_credentials 授予类型不授予对授权范围的访问权限。

所以使用 params.put("grant_type", "authorization_code");

希望有帮助!

Your grant_type parameter should be authorization_code instead of client_credentials. The client_credentials grant type does not give access to authorization scopes.

So use params.put("grant_type", "authorization_code");

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文