https://aip.baidubce.com/oauth/2.0/token报错unsupported_grant_type/The authorization grant type is not

问题描述:

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%8Ficon-default.png?t=M4ADhttps://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

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值