我必须从URL中获取“ ile de Francemobilité” API的令牌,我正在使用Fetch,但我不知道该怎么做。这是我的实际代码:
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)
我有答案
“其他stuf和:”
[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
}
}
有人知道该怎么做吗?
在我的index.html中,我将此脚本称为:
async function asyncCall() {
const api_url = `/trajet`
const response = await fetch(api_url, {method: 'POST'})
const json = await response
//console.log(json)
}
asyncCall()
服务器:
const app = express()
app.listen(3000, () => console.log('listening at 3000'))
app.use(express.static('public'))
app.use(express.json())
app.use(bodyParser.json())
答案 0 :(得分:1)
HTTP状态代码401未经授权表示您的请求缺少有效的身份验证凭据。因此,您发送的体内肯定有问题。
尝试以这种格式发送正文:
body: 'grant_type=client_credentials&client_id=' + key + '&client_secret=' + secret,