简体   繁体   中英

VueJS GET request with Bearer token

I am experimenting with Kentico Delivery Preview API using VueJS, which allows you to get unpublished content by submitting a bearer token for authorisation ( https://developer.kenticocloud.com/reference#authentication ). However, no matter what I do I get a 401 in response. PROJECT_ID, ITEM_NAME and TOKEN are all correct, taken from the project, so it's not a typo issue. I admit I don't have much experience with auth, but any help would be appreciated:

var app = new Vue({
    el: '#app',
    data: {
        message: 'Hello Vue!'
    },
    mounted () {
        axios
          .request({
            url: '/items/ITEM_NAME',
            method: 'get',
            baseURL: 'https://preview-deliver.kenticocloud.com/PROJECT_ID',
            headers: {
                'Authorisation': 'Bearer TOKEN'
            }
          })
          .then(response => {
            console.log(response.data)
          })
      }

})

As pointed out by Walter in the comments, I spelt Authorization with an S rather than a Z.. because I'm English. Whoops.

Use create to config axios headers before your request

const TOKEN = 'Token';
const BASEURL = 'https://preview-deliver.kenticocloud.com/PROJECT_ID';
const ENDPOINT = '/items/ITEM_NAME';

axios.create({
        baseURL: BASEURL,
        headers: {
            'Content-Type': 'application/json',
            'Authorization': 'Bearer '+TOKEN
        }
    })
    .get(ENDPOINT)
    .then(res => {
            console.log(res);
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM