android - 如何在android上处理错误401 Spotify web api(包装器)
问题描述
我在更新 Spotify 访问令牌时遇到问题,实际上根本不知道如何执行此操作。我正在使用带有 android 包装器的 web api
我的实现:
//spotify WEB API Wrapper
implementation 'com.github.kaaes:spotify-web-api-android:0.4.1'
// spotify auth library
implementation 'com.spotify.android:auth:1.1.0'
带有用户同意屏幕的活动代码,该屏幕将用户发送到网页以授予对其帐户的访问权限
public class OAuthSpotify extends AppCompatActivity {
public static final String CLIENT_ID = "123456789";
public static final int AUTH_TOKEN_REQUEST_CODE = 0x10;
private final OkHttpClient mOkHttpClient = new OkHttpClient();
private String mAccessToken;
private Call mCall;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_oauth_spotify);
final AuthenticationRequest request = getAuthenticationRequest();
AuthenticationClient.openLoginActivity(this, AUTH_TOKEN_REQUEST_CODE, request);
new AuthenticationRequest.Builder(CLIENT_ID, AuthenticationResponse.Type.TOKEN, getRedirectUri().toString())
.setShowDialog(true)
.setScopes(new String[]{"user-read-email"})
.setCampaign("your-campaign-token")
.build();
}
@Override
protected void onDestroy() {
cancelCall();
super.onDestroy();
}
@SuppressWarnings("unused") public void getUserProfile() {
final Request request = new Request.Builder()
.url("https://api.spotify.com/v1/me")
.addHeader("Authorization", "Bearer " + mAccessToken)
.build();
cancelCall();
mCall = mOkHttpClient.newCall(request);
mCall.enqueue(new Callback() {
@Override public void onFailure(Request request, IOException e) {
Log.d(App.myFuckingUniqueTAG + "OAuthSpotify", "onFailure: " + "Failed to fetchArtURL data: " + e);
}
@Override
public void onResponse(com.squareup.okhttp.Response response) throws IOException {
try {
final JSONObject jsonObject = new JSONObject(response.body().string());
Log.d(App.myFuckingUniqueTAG + "OAuthSpotify", "onResponse: " + jsonObject.toString(3));
} catch (JSONException e) {
Log.d(App.myFuckingUniqueTAG + "OAuthSpotify", "onResponse: " + "Failed to fetchArtURL data: " + e);
}
}
});
}
private AuthenticationRequest getAuthenticationRequest() {
return new AuthenticationRequest.Builder(CLIENT_ID, AuthenticationResponse.Type.TOKEN, getRedirectUri().toString())
.setShowDialog(true)
.setScopes(new String[]{"user-read-email"})
.setCampaign("your-campaign-token")
.build();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(App.myFuckingUniqueTAG + "OAuthSpotify", "onActivityResult: ");
super.onActivityResult(requestCode, resultCode, data);
final AuthenticationResponse response = AuthenticationClient.getResponse(resultCode, data);
if (AUTH_TOKEN_REQUEST_CODE == requestCode) {
mAccessToken = response.getAccessToken();
Prefs.putString(c.spotify_access_token, mAccessToken);
App.spotify_access_token = mAccessToken;
finish();
// getUserProfile();
// tryAccess();
}
}
private void cancelCall() {
if (mCall != null) {
mCall.cancel();
}
}
private Uri getRedirectUri() {
return new Uri.Builder()
.scheme(getString(R.string.com_spotify_sdk_redirect_scheme))
.authority(getString(R.string.com_spotify_sdk_redirect_host))
.build();
}
}
一切正常,问题是我在 1 小时后过期的令牌,然后我必须请求用户批准才能一次又一次地访问他的帐户。
我知道有一种方法可以在没有用户批准的情况下使用第一次调用返回的第二个令牌来请求新令牌,但我无法使用当前库执行此过程,更不用说直接使用 web api。我实际上很迷失在这里。
我使用 Spotify 的 API 的目标只是从一些艺术家和音乐中获取元数据我对访问用户数据不感兴趣,所以我什至不知道为什么我需要请求授权
欢迎任何解决方法!
解决方案
推荐阅读
- spring - 值对象为 @AggregateIdentifier 和 @TargetAggregateIdentifier
- spring - Spring Cloud Contract - 字符串数组作为查询参数
- xamarin.forms - 从视图模型到另一个视图的方向
- laravel - Laravel/php 应用程序不执行更改
- angular - 如何提供资产文件夹之外的图像?
- android - BottomSheetFragment 是否需要 ViewModel?
- github - 来自分叉的拉取请求不会触发 travis ci
- c# - 计算有多少用户添加到数据库
- java - 使用 api 提供的签名哈希对 PDF 进行签名
- biztalk - BizTalk 2013R2 - 模糊转换失败错误