我正在开发一款移动应用程序,该应用程序在 iOS 和 Android 上都能完美登录。但是,登录后,当我尝试在 Android 上使用授权令牌发出后续 API 请求时,我始终收到 401 未经授权的错误。相同的 API 调用在 iOS 上运行良好。
export const poster = async ({url, method = 'POST', body}: PosterType) => {
const {cookie} = store.getState().userReducer;
// Only add Authorization header if cookie exists
return await axios({
url,
method: method,
headers: {
Authorization: `Bearer ${cookie}`,
'Content-Type': 'application/json',
Accept: 'application/json',
},
data: body,
withCredentials: false,
// mode: 'cors',
})
.then(async value => {
const data = await value.data;
if (!value.status) {
throw new Error(data.message ?? data?.error?.message);
}
return data;
})
.catch(error => {
// console.log(JSON.stringify(error), 'errorerrorerrorerror');
throw error;
});
};
为什么这个问题只发生在 Android 上?什么可能导致标头或令牌无法在 Android 上正确发送或解析? 如何在 Android 上解决此问题,以确保授权令牌正确传递并由服务器进行身份验证,就像在 iOS 上一样?
确保您已将此行添加到 android 文件夹内的清单文件中。
<application
android:usesCleartextTraffic="true"
...
</application>