【发布时间】:2021-11-26 07:00:05
【问题描述】:
我正在尝试使用 Vue 3 和 Axios 从提要中获取最新的 Instagram 帖子。 Instagram 推出了strict-origin-when-cross-origin 政策,所以我一直被屏蔽。访问令牌和一切都已经设置好了。
使用Instagram Basic Display API 的指南并没有什么帮助,大吃一惊。
main.js
import { createApp } from 'vue'
import axios from 'axios'
import VueAxios from 'vue-axios'
import App from './App.vue'
const app = createApp(App)
app.use(BootstrapVue3, VueAxios, axios)
app.provide('axios', app.config.globalProperties.axios)
app.mount('#app')
App.vue
<template>
<Home />
</template>
<script>
import { inject } from 'vue'
import Home from './components/Home.vue'
export default {
name: 'App',
components: { Home },
setup() {
inject('axios')
}
}
</script>
Home.vue
export default {
name: 'Home',
mounted() {
axios.get('https://api.instagram.com/me?fields=id&access_token=myToken')
.then(response => {
console.dir(response)
}).catch(err => {
console.error(err)
})
}
}
</script>
Access to XMLHttpRequest at 'https://api.instagram.com/me?access_token={myToken}'
from origin 'http://localhost:1234' has been blocked by CORS policy: No 'Access-Control-Allow-Origin'
header is present on the requested resource.
CORS Header Proxy 或 cors-anywhere 当然会有所帮助,但我想正确地做到这一点......而且我不知道如何在 Vue 中实现 CORS Header Proxy :)
【问题讨论】:
-
“我想正确地执行此操作” 是什么意思?如果 Instagram 尚未为您的客户端域启用 CORS 访问,您将需要使用代理或使用服务器端技术编写应用程序的这一部分。没有其他选择
标签: javascript vue.js axios cors