javascript - Vue JS 中 axios 的 Slack API CORS 错误
问题描述
我正在使用 Capacitor JS 和 Nuxt JS 构建一个应用程序来与 Slack API 交互,以便我可以设置我的 Slack 状态,我已经创建了一个 Slack 应用程序并且有一个令牌,当我通过请求xoxp-
到达端点时它工作得很好POST
通过邮递员,但从我的浏览器(本地主机)和手机上正在运行的应用程序中,我收到以下 CORS 错误:
从源“http://localhost:3000”访问“https://slack.com/api/users.profile.set”的 XMLHttpRequest 已被 CORS 策略阻止:访问控制不允许请求标头字段授权- 预检响应中的允许标头。
现在这似乎很愚蠢,因为您必须使用authorization
标头来提供 Bearer 令牌进行身份验证,但即使暂时省略了这一点,CORS 错误仍然存在。
我正在尝试到users.profile.setPOST
的端点查看另一种方法
我的 Axios 代码中缺少什么?
setSlackStatusWithReminder (title, expiry) {
const body = this.convertToQueryString({
profile: this.convertToQueryString(this.profile),
token: 'xoxp-mytoken'
})
this.$axios.post('https://slack.com/api/users.profile.set', body, {
timeout: 10000,
transformRequest(data, headers) {
delete headers.common['Content-Type'];
return data;
}
}).then(res => {
console.log(res)
if (res.data.ok != true) {
alert('something went wrong with the .then')
}
this.isSettingStatus = false
this.actions.isShown = false
}).catch(err => {
this.isSettingStatus = false
this.actions.isShown = false
})
},
更新
我有一个函数可以将我的请求正文从我的数据中转换为查询字符串,如下所示:
export default {
data () {
return {
profile: {
status_text: '',
status_emoji: '',
status_expiration: 0
}
}
}
}
查询字符串函数转换正文
convertToQueryString (obj) {
const convert = Object.keys(obj)
.map((key, index) => `${key}=${encodeURIComponent(obj[key])}`)
.join('&')
return convert
},
我正在构建它:
const body = this.convertToQueryString({
profile: this.convertToQueryString(this.profile),
token: 'xoxp-mytoken'
})
它正在给我一个invalid_profile
回应。
解决方案
推荐阅读
- python - 使用电报机器人按计划发送消息
- python - 带有一组链条的芹菜弦,链条中有例外
- angular - 错误:无法在 Angular 5 中解析“child_process”
- android - 高效且定期地更新变量
- nuxt.js - 包含阿拉伯字符的 URL 的 Nuxtjs SSR 错误“请求路径包含未转义的字符”
- haskell - ''hello'' ++ ''world'' 在 Haskell 中不起作用
- javascript - 从调用的窗口javascript获取文件对象
- azure-devtest-labs - 使用 cicd 管道在开发测试实验室中创建 ServiceFabric-LabCluster
- c++ - 为什么编译器接受带有长双字面值的浮点数的初始化?
- javascript - 将 JavaScript 变量发送到 HTML 页面