当前位置: 首页 > 知识库问答 >
问题:

React Native中如何使用Axios设置授权头

彭涵衍
2023-03-14

我正在用Expo CLI构建一个React原生应用程序,我正在尝试用Axios和redux Thunk获取一些用户数据到我的redux商店。

export const fetchAccount = createAsyncThunk(
    'auth/fetchAccount',
    async (payload, { dispatch, rejectWithValue, getState }) => {
        try {
            const response = await axios.get('/manager/user/account', {
                headers: {
                    "Authorization": "Bearer " + jwt
                },
            });
            return response.data
        } catch (err) {
            if (!err.response) {
                throw err
            }
            console.log(err.response)
            return rejectWithValue(err.response.data)
        }
    }
);
curl -X GET "http://.......eu.pythonanywhere.com/manager/user/account/" -H "accept: application/json" -H "Authorization: Bearer eyJ0e.......0RmsJoBv9M"
Object {
  "config": Object {
    "adapter": [Function xhrAdapter],
    "baseURL": "http://......eu.pythonanywhere.com",
    "data": undefined,
    "headers": Object {
      "Accept": "application/json, text/plain, */*",
      "Authorization": "Bearer eyJ0e.......0RmsJoBv9M",
    },
    "maxContentLength": -1,
    "method": "get",
    "timeout": 0,
    "transformRequest": Array [
      [Function transformRequest],
    ],
    "transformResponse": Array [
      [Function transformResponse],
    ],
    "url": "/manager/user/account",
    "validateStatus": [Function validateStatus],
    "xsrfCookieName": "XSRF-TOKEN",
    "xsrfHeaderName": "X-XSRF-TOKEN",
  },
  "data": Object {
    "msg": "Missing Authorization Header",
  },
  "headers": Object {
    "connection": "keep-alive",
    "content-length": "44",
    "content-type": "application/json",
    "date": "Fri, 31 Jul 2020 16:00:53 GMT",
    "server": "PythonAnywhere",
  },
  "request": XMLHttpRequest {
    "DONE": 4,
    "HEADERS_RECEIVED": 2,
    "LOADING": 3,
    "OPENED": 1,
    "UNSENT": 0,
    "_aborted": false,
    "_cachedResponse": undefined,
    "_hasError": false,
    "_headers": Object {
      "accept": "application/json, text/plain, */*",
      "authorization": "Bearer eyJ0e.......0RmsJoBv9M",
    },
    "_incrementalEvents": false,
    "_lowerCaseResponseHeaders": Object {
      "connection": "keep-alive",
      "content-length": "44",
      "content-type": "application/json",
      "date": "Fri, 31 Jul 2020 16:00:53 GMT",
      "server": "PythonAnywhere",
    },
    "_method": "GET",
    "_requestId": null,
    "_response": "{
  \"msg\": \"Missing Authorization Header\"
}
",
    "_responseType": "",
    "_sent": true,
    "_subscriptions": Array [],
    "_timedOut": false,
    "_trackingName": "unknown",
    "_url": "http://.......eu.pythonanywhere.com/manager/user/account",
    "readyState": 4,
    "responseHeaders": Object {
      "Connection": "keep-alive",
      "Content-Length": "44",
      "Content-Type": "application/json",
      "Date": "Fri, 31 Jul 2020 16:00:53 GMT",
      "Server": "PythonAnywhere",
    },
    "responseURL": "http://........eu.pythonanywhere.com/manager/user/account/",
    "status": 401,
    "timeout": 0,
    "upload": XMLHttpRequestEventTarget {},
    "withCredentials": true,
  },
  "status": 401,
  "statusText": undefined,
}

共有1个答案

龙德海
2023-03-14

试试这个

       const config = {
         headers: {
             Authorization: "Bearer " + jwt
         }
     };

     axios.get('/manager/user/account', {...config});

authorization中不需要双引号

 类似资料:
  • 问题内容: 我一直在尝试使用axios向National Park Service API发出GET请求,并尝试了几种方法将请求标头中的API密钥设置为无效。任何帮助将不胜感激。 我努力了: 和 都返回401。当我在Postman中发送GET请求时工作,我在密钥字段中输入Authorization,在值字段中输入我的API密钥。 谢谢。 问题答案: 此问题是由浏览器中的CORS OPTIONS请求

  • 如何通过Axios.js发送带有令牌的身份验证头?我试过几件事都没有成功,比如: 给出以下错误:

  • 我使用中的此选项在服务器中运行: 我得到这个错误: 分析INI配置文件时出错:未知的选项安全性。

  • 版权设置 (版权用于设置前台页面底部的网站版权,是授权用户最重要的标志,只有授权用户才可更改版权信息,否则是要追究法律责任的哦)

  • 我有一个用于REST API的HttpClient。然而,我在设置授权头时遇到了麻烦。我需要将头设置为我在执行OAuth请求时收到的令牌。我看到了一些.NET的代码, 但是,WinRT中不存在凭据类。有人知道如何设置授权头吗?

  • 问题内容: 当我卷曲东西时,它可以正常工作: 我如何使它与axios一起正常工作?如果这很重要,我正在使用react: 由于某些原因,这不起作用。 问题答案: 这是我如何使用axios在react中上传文件 资源