https:aip.baidubce.comoauth2.0token报错unsupported

https:aip.baidubce.comoauth2.0token报错unsupported,第1张

问题描述:

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是进行了跨域,下面放出一个跨域的小问题

解决vue跨域302,301,404,问题_是泡沫呀的博客-CSDN博客_vue跨域请求404

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1320358.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-11
下一篇2022-06-11

发表评论

登录后才能评论

评论列表(0条)

    保存