0

I am trying to have an Ouath2 access token based authentication. I am using django-oauth-toolkit for that. I have registered my app on http://localhost:8000/o/applications.

enter image description here

However, When I tried to hit the URL http://localhost:8000/o/token/ from the react-app . I got 401.

enter image description here

Here is my the useEffect hook that I used for calling that URL :

useEffect(() => {
// axios.get("api/tests")
// .then((res)=>{
//   setUsers(JSON.stringify(res.data))
// })
fetch("http://localhost:8000/o/token/", {
    body: new URLSearchParams({
        grant_type: 'client_credentials',
        client_id: client_id,
        client_secret: client_secret
    }),
    headers: {
        "Content-Type": "application/x-www-form-urlencoded",
        "Authorization": `Basic ${btoa(client_id+":"+client_secret)}`}
    ,
    method: "POST"
}).then((res)=>{
  console.log(res)
})
}, [])

Will look forward to some comments and solutions.

Ashish Tripathi
  • 115
  • 1
  • 6
  • It looks like you are not sending the Authorization header with your request. You should add the following line to your headers:headers: { "Content-Type": "application/x-www-form-urlencoded", "Authorization": "Basic base64(client_id:client_secret)"},Where the base64 string is generated from your client_id and client_secret. – Omar Dieh Feb 05 '23 at 12:12
  • ok @OmarDieh . I will try and get back – Ashish Tripathi Feb 05 '23 at 12:20
  • @OmarDieh No it didn't work. Still getting 401 – Ashish Tripathi Feb 05 '23 at 12:54

1 Answers1

0

I got the solution. Actually I was Using the encrypted version of the client secret. I forgot to copy it before saving. Once it is saved the value is encrypted. So it's better to copy it somewhere prior saving it.

Ashish Tripathi
  • 115
  • 1
  • 6