elisaado / somtoday-api-docs

Documentation for the SOMtoday API
69 stars 15 forks source link

Get user id not working #20

Closed Wolletje01 closed 3 years ago

Wolletje01 commented 3 years ago
const response = await fetch('https://somtoday.nl/oauth2/token', {method: 'post', body: params, headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    });
    const school = await response.json();
    console.log(school);
    console.log(school.somtoday_api_url) // https://api.somtoday.nl
    console.log(school.access_token) // <ACCES-TOKEN>
    const params1 = new URLSearchParams();
    params1.append("Content-Type", "application/x-www-form-urlencoded")
    params1.append("Authorization", school.access_token) 
    const leerlingen = await fetch(school.somtoday_api_url + '/rest/v1/leerlingen', { method: 'get', headers: params1  });
    const leerling1 = await leerlingen
    console.log(leerling1) // unauthorized
i want my grades in this api. How to fix that
elisaado commented 3 years ago

Hey, would you try the following code?

const response = await fetch("https://somtoday.nl/oauth2/token", {
  method: "post",
  body: params,
  headers: { "Content-Type": "application/x-www-form-urlencoded" },
});
const school = await response.json();
console.log(school);
console.log(school.somtoday_api_url); // https://api.somtoday.nl
console.log(school.access_token); // <ACCES-TOKEN>
const leerlingen = await fetch(
  school.somtoday_api_url + "/rest/v1/leerlingen",
  { method: "get", headers: { "Content-Type": "application/x-www-form-urlencoded", "Authorization": `Bearer ${school.access_token}` } }
);
const leerling1 = await leerlingen;
console.log(leerling1); // unauthorized

I added the word Bearer before the token, and replaced URLSearchParams with a plain object to achieve that.

Wolletje01 commented 3 years ago

It works thnx

elisaado commented 3 years ago

joe no problem