I have to get a token from a URL for an API of "ile de France mobilité", I'm using Fetch and I don't understand how to do this. This is my actual code:
var client_id = "(my client_id)"; var client_secret = "(my client_secret)"; // var url_get = "https://traffic.api.iledefrance-mobilites.fr/v1/tr-global/estimated-timetable"; var grant_type = 'client_credentials' var token_url = 'https://as.api.iledefrance-mobilites.fr/api/oauth/token' var scope = 'read-data' const options = { method: 'POST', headers: { 'Content-Type' : 'application/x-www-form-urlencoded', }, body: { grant_type: grant_type, client_id: client_id, client_secret: client_secret, scope: scope } } const fetch_response = await fetch(token_url, options) const json = await fetch_response console.log(json) response.json(json) And for answer I have
'Other stuff and :'
[Symbol(Response internals)]: { url: 'https://as.api.iledefrance-mobilites.fr/api/oauth/token', status: 401, statusText: 'Unauthorized', headers: Headers { [Symbol(map)]: [Object: null prototype] }, counter: 0 } } Does someone know how to do this?
In my index.html I call this script:
async function asyncCall() { const api_url = `/trajet` const response = await fetch(api_url, {method: 'POST'}) const json = await response //console.log(json) } asyncCall() server:
const app = express() app.listen(3000, () => console.log('listening at 3000')) app.use(express.static('public')) app.use(express.json()) app.use(bodyParser.json())
app.post('/trajet', async (request, response) =>and my route isirrelevant since your code never accesses that route - is the code you posted in a browser? It would be extremely odd (and insecure) if it isconst json = await fetch_responseshould beconst json = await fetch_response.json()for a startfetchresponse ... ifresponse = await fetchthen it isn't a Promise ... you need to use one of the methods like .text() or .json() etc to access response data