0

I have been searching for a while and cannot find and complete example on how to correctly build an authorization header, finding only articles that have a lot of time so I can't get it to work.

The problem is not really code, but rather "instructions", because i keep getting error "401 Unauthorized". I have tested all the calls I need in the Postman software but i cant get in to work in a react ".jsx" file through an axios.get() method.

And I think I have one or all of these problems:

  • I'm missing paramaters in my header which are causing my request to be denied;
  • I'm inserting the keys(Client Id,Client Secret,Access token) has the wrong parameters;
  • I'm not doing the request correctly(?)

I'm pretty new at React, so i don't really handle well a lot of... lets say "good pratics" of coding. Anyways, I have written the following header:

this.apiCallParams = {
            method : "GET",
            headers: {
                "Cache-Control" : "no-cache",
                "Accept" : "application/json",
                "Content-Type" : "application/json"
            },
            body : JSON.stringify({
                "client_id" : "***********",
                "client_secret" : "******************************************************",
                "access_token" : "**************************************--",
                "grant_type":"client_credentials"
            })
        }

And made the request like this:

this.state ={
            requestURL : "https://api.predicthq.com/v1/events?q=" + this.props.location.state.textInput,
            searchResultsList : [],
        }
    }
    componentDidMount(){
        axios.get(this.state.requestURL, this.apiCallParams).then( res => { const searchResultsList = res.data; this.setState({ searchResultsList : searchResultsList.results })});
    }

I have replaced the keys as the same amount of "*", don't know if that matters,and left the "--" because it actually belongs to the access token, either way, I don't know why I keeping getting 401, can some one tell me what is wrong with my header.

Phil
  • 163
  • 6
  • See the accepted answer shows how to send body and headers with axios: https://stackoverflow.com/questions/44617825/passing-headers-with-axios-post-request – OWS Jun 25 '20 at 23:03
  • That post referes a "POST" method, is it the same for a GET? – Phil Jun 25 '20 at 23:57
  • you change it "Get" method. Checkout some restful methods and learn the difference when to send body and params and other data. – OWS Jun 26 '20 at 04:55

0 Answers0