首页 > 解决方案 > 无法使用 Web 应用从 reddit 获取 access_token

问题描述

我想access_token从 Reddit 获得一个在我的代码中使用,但不断收到有关不受支持的授权类型的 401 错误。这是代码(过滤了个人内容):

const fetch = require('node-fetch');
const FormData = require('form-data');
let data = new FormData()
data.append('code', 'temporary_code')
data.append('grant_type', "authorization_code")
data.append('redirect_uri', "http://localhost")
fetch('https://www.reddit.com/api/v1/access_token', {
    method: 'post',
    body: data,
    headers: {'Authorization': `Basic ${Buffer.from('client_id:client_secret').toString('base64')}`, 'content-type': 'application/x-www-form-urlencoded'}
})
.then(res => res.json())
.then(console.log)

我不断尝试换掉content-type我的代码的值(主要是移动到不同的地方)。我什至尝试使用Postman发出帖子请求,但也失败了。我浏览了网络(主要是关于同一问题的 reddit 问题,但使用不同的应用程序类型或不同的编程语言)试图得到答案。我什至多次尝试在谷歌上搜索我的问题,但都失败了。这是我得到的确切错误:

{ message: 'Unauthorized', error: 401 }

在编写代码时,我使用Reddit Wiki来确保我做的一切都是正确的。我注意到我有unsupported_grant_type错误,最终我放弃了尝试修复自己。现在,我要求大家尝试帮助甚至回答我的问题。提前非常感谢。

标签: javascriptnode.jshttpsreddit

解决方案


推荐阅读