如何在不要求用户登录的情况下获取 Spotify api 访问令牌


How to get Spotify api access token without asking user to login?

PHP中编码,我尝试初始化Spotify API,不使用重定向URI,只使用client_id和client_secret。我尝试下面的代码:从响应网址中提取令牌 - Spotify API,但我得到一个空结果

我想知道如果我可以在不要求用户登录的情况下访问令牌(参见 https://developer.spotify.com/web-api/authorization-guide/#authorization-code-flow(

当然,您可以使用客户端凭据流。但是,请记住,在大多数终结点中,除非访问令牌连接到用户,否则没有意义。我相信是 PHP 包装器,它可以帮助您请求使用此流程获取访问令牌。

     private static String getBearerToken()
        {
            String fetchUrl = getSiteUrl(playlistId);
            String html2 = fetchHtml(fetchUrl);
            Document document2 = Jsoup.parse(html2);
            Element ele = document2.getElementById("session");
            String session = ele.html();
            JSONObject tokens = new JSONObject(session);
            final String accessToken = tokens.getString("accessToken");
            return accessToken;
        }
private static String getSiteUrl(final String playlistId)
    {
        return "https://open.spotify.com/playlist/"+playlistId;
    }
private static String fetchHtml(final String urlstr)
    {
        try {
            URL url = new URL(urlstr);
            HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
            return new String(urlConnection.getInputStream().readAllBytes(), StandardCharsets.UTF_8);
        }
        catch (final Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

像这里: https://github.com/BeatEngine/spotify-playlist-extractor-bpm

在 api 网址上设置此项:

HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
            urlConnection.setRequestProperty("client-token", "");
            urlConnection.setRequestProperty("authorization", " Bearer " + bearerToken);

正如我发现的,如果您有身份验证令牌,设置正确的客户端令牌并不重要。