node.js - 在 NestJS 上使用带有 Discord API 的 OAuth2 出现问题
问题描述
我正在尝试为 Discord 创建自定义 API,但现在我正在尝试设置 OAuth2。
为了发出必要的 HTTP 请求,我在 NestJS 中使用了包含的 HttpModule,它是 axios,所以在我的服务中,我定义了一个函数来检索代码(之前由 Discord 发送)并最终获得访问令牌,这是它的外观:
@Injectable()
export class AuthService {
constructor(private http: HttpService) {}
async validateToken(token: string) {
const data = {
client_id: [some thing],
client_secret: [some thing],
grant_type: 'authorization_code',
code: token,
redirect_url: encodeURI('https://49fe5ab2.ngrok.io/auth'),
scope: 'identify',
};
return await this.http
.post('https://discordapp.com/api/oauth2/token', null, { params: data, headers: {"Content-Type": "application/x-www-form-urlencoded"}})
.toPromise()
.then(resp => Logger.log(resp))
.catch(err => Logger.error(err));
}
}
在控制器中有我为 OAuth2 设置的路由,它只是一个函数
@Controller('auth')
export class AuthController {
constructor(private readonly AuthService: AuthService) {}
@Get()
responseRetrieve(@Req() request) {
return this.AuthService.validateToken(request.query.code);
}
}
发生的事情是我只得到 401 状态码,这意味着我的所有访问都是未经授权的。我已经尝试将数据作为请求正文传递,最后将其作为参数传递。
OBS:我知道我应该为客户端 ID 和 client_secret 使用环境变量,但现在我正在尝试完成这项工作。
我没有明白我做错了什么,因为唯一不起作用的是我收到代码后的 axios POST 请求(在服务函数中也称为令牌
解决方案
推荐阅读
- javascript - 如何在Javascript中计算每个进程的CPU使用率
- coordinates - 显示每个用户输入的坐标
- php - 循环浏览页面并为函数内部的每个内部更新 ACF 字段数据
- flutter - 没有为“FirebaseMessaging”类型定义“配置”方法。任何人都可以正确地重新输入我的代码吗?谢谢
- mysql - 从文件目录中的 Xampp MySQL 导出表
- windows - 无法通过 Windows 中的 SQL Developer 将转储文件导入 oracle
- azure-devops - 在 Azure DevOps 中部署选定的版本
- reactjs - 尽管 DOM 行为正确,但在 React 测试库/Jest 中测试失败
- c++ - 使用“#pragma once”时如何禁用包含标题(类似于使用包含警卫时)?
- python - 您如何使用一个函数的输出作为另一个函数的输入值?