【问题标题】:Spotify Android - Make Authorized Web RequestsSpotify Android - 发出授权的网络请求
【发布时间】:2017-04-08 22:41:38
【问题描述】:

我正在开发一个实现 Spotify API 以允许用户听音乐的 Android 应用。我已经配置了 Spotify 为 Android 设备创建的播放器,但它的功能非常有限,所以我不得不通过 Spotify 的 Web API 来实现更高级的功能。

我在尝试获取用户自己的播放列表时遇到了一个错误。我正在使用:

URL url = new URL("https://api.spotify.com/v1/me/playlists");
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

但是,这个命令并没有像我对其他 Web API 请求那样执行,而是引发了错误:

 java.io.FileNotFoundException: https://api.spotify.com/v1/me/playlists
                                                                               at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:242)
                                                                               at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
                                                                               at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25)
                                                                               at com.tmacstudios.spotifyvoice.WebWrapper$override.searchUserPlaylist(WebWrapper.java:257)
                                                                               at com.tmacstudios.spotifyvoice.WebWrapper$override.access$dispatch(WebWrapper.java)
                                                                               at com.tmacstudios.spotifyvoice.WebWrapper.searchUserPlaylist(WebWrapper.java:0)
                                                                               at com.tmacstudios.spotifyvoice.MainActivity$6.run(MainActivity.java:382)
                                                                               at java.lang.Thread.run(Thread.java:818)

我不完全确定为什么会收到此错误,但我认为这可能与未获得发出此请求的必要授权有关。谁能帮我解决这个问题?

【问题讨论】:

    标签: android api web authorization spotify


    【解决方案1】:

    在进一步研究了文档之后,我找到了解决问题的方法。事实上,由于我没有使用正确的授权,我得到了错误。

    您可以使用以下代码在 OnActivityResult 中获取授权令牌:

    AuthenticationResponse response = AuthenticationClient.getResponse(resultCode, intent);
                if (response.getType() == AuthenticationResponse.Type.TOKEN) {
    
                    authToken = response.getAccessToken();
                    Log.e("MainActivity","Auth Token: "+authToken.toString());
     ...
    

    然后在进行 URL 请求时,只需将授权令牌作为标头传入即可。

    URL url = new URL("https://api.spotify.com/v1/me/playlists");
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestProperty("Authorization", "Bearer " + authToken);
    
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
     ...
    

    另请注意,当您最初形成授权请求时,您必须启用您正在使用的权限作为范围。

    【讨论】:

      猜你喜欢
      • 2017-12-29
      • 2018-06-14
      • 1970-01-01
      • 2021-04-18
      • 1970-01-01
      • 2018-05-21
      • 2014-06-05
      • 2014-04-15
      • 2021-05-24
      相关资源
      最近更新 更多