问题描述:
https://ai.baidu.com/ai-doc/REFERENCE/Ck3dwjhhu#%E4%BD%BF%E7%94%A8access-key-idsecret-access-key%E7%9A%84%E5%BC%80%E5%8F%91%E8%80%85%E6%B3%A8%E6%84%8Fhttps://ai.baidu.com/ai-doc/REFERENCE/Ck3dwjhhu#%E4%BD%BF%E7%94%A8access-key-idsecret-access-key%E7%9A%84%E5%BC%80%E5%8F%91%E8%80%85%E6%B3%A8%E6%84%8F百度这个接口文档,我们在调用的时候,发现报400,不论是get请求还是post
为什么呢?
因为请求参数必须放到url的后面带上,而不是写在data或者params中
测试1,你可以试一试,在postman中,使用get,然后把参数在url后面拼上,然后就能跑通了
/oauth/2.0/token?grant_type=client_credentials&client_id=xxx
测试2,直接在浏览器中请求,把完整的url携带参数的get请求,也通了
好,下面来上代码
错误写法:以下两段代码,都会报400错误,get和post请求
const params= {
grant_type : 'client_credentials', // 必须参数,固定为client_credentials
client_id : 'xxx', // 必须参数,应用的API Key
client_secret : 'xxx', // 必须参数,应用的Secret Key
}
axios.get('/baidu/oauth/2.0/token', params).then(res => {
console.log('res', res)
}).catch(e => {
console.log('e', e);
})
const data = {
grant_type : 'client_credentials', // 必须参数,固定为client_credentials
client_id : 'xxx', // 必须参数,应用的API Key
client_secret : 'xxx', // 必须参数,应用的Secret Key
}
axios.post('/baidu/oauth/2.0/token', data).then(res => {
console.log('res', res)
}).catch(e => {
console.log('e', e);
})
正确写法:以下两段代码就请求成功了,get和post
注意:把参数的xxx改为自己的key
axios.get('/baidu/oauth/2.0/token?grant_type=client_credentials&client_id=xxx&client_secret=xxx')
.then(res => {
console.log('res', res)
}).catch(e => {
console.log('e', e);
})
axios.post('/baidu/oauth/2.0/token?grant_type=client_credentials&client_id=xxx&client_secret=xxx')
.then(res => {
console.log('res', res)
}).catch(e => {
console.log('e', e);
})
结尾:我这里的url是进行了跨域,下面放出一个跨域的小问题