Using an authorization header with Fetch in React Native

前端 未结 4 764
天涯浪人
天涯浪人 2020-11-27 10:18

I\'m trying to use fetch in React Native to grab information from the Product Hunt API. I\'ve obtained the proper Access Token and have saved it to State, but d

4条回答
  •  旧时难觅i
    2020-11-27 11:17

    I had this identical problem, I was using django-rest-knox for authentication tokens. It turns out that nothing was wrong with my fetch method which looked like this:

    ...
        let headers = {"Content-Type": "application/json"};
        if (token) {
          headers["Authorization"] = `Token ${token}`;
        }
        return fetch("/api/instruments/", {headers,})
          .then(res => {
    ...
    

    I was running apache.

    What solved this problem for me was changing WSGIPassAuthorization to 'On' in wsgi.conf.

    I had a Django app deployed on AWS EC2, and I used Elastic Beanstalk to manage my application, so in the django.config, I did this:

    container_commands:
      01wsgipass:
        command: 'echo "WSGIPassAuthorization On" >> ../wsgi.conf'
    

提交回复
热议问题