首页 > 解决方案 > atlassian cloud oauth 2.0,POST 请求获取令牌时出现错误 403

问题描述

我打算将我的 Angular 应用程序与 Jira 云集成。为了对用户进行身份验证,Atlassian 使用 OAuth 2.0 让外部应用程序代表用户访问产品 API。我按照此页面的说明进行操作

  1. 将用户定向到授权 URL 以获取授权码
  2. 交换访问令牌的授权码
  3. 使用访问令牌授权对产品 API 的任何调用
  4. 检查应用程序的站点访问权限

我顺利通过了第一步。但是在第二步,我获取令牌的 POST 请求失败了。这是我的请求数据

    {
    "grant_type":"authorization_code",
    "client_id":"xxx", 
    "client_secret":"xxx",
    "code":"xxx", // I receive this code from callback url from atlassian login page
    "redirect_uri":"http://localhost:4200/"}

这是标题:

{'Content-Type': 'application/json'}

这就是我收到的:

{"error":"invalid_grant","error_description":"Invalid authorization code"}

这是用 Angular 10 编写的请求代码:

this.http
        .post(
          'https://auth.atlassian.com/oauth/token', {
            grant_type: 'authorization_code',
            client_id: environment.oauth.clientID,
            client_secret: environment.oauth.secret,
            code: codeStr,
            redirect_uri: environment.oauth.redirect_uri
          }, {
            headers: exchangeAuthCodeHeader,
            responseType: 'json'}
        ).subscribe((data) => console.log(data), (error) => console.log(error));

我已经陷入这个错误几天了:(任何想法都会有很大帮助。

标签: angularoauth-2.0http-status-code-403

解决方案


我认为这是因为您尝试使用相同的代码两次。

一旦您授权 Jira,您被重定向到的路由可能会立即使用该代码。检查您是否有请求处理程序http://localhost:4200/

如果你这样做了,是在哪里https://auth.atlassian.com/oauth/token调用请求?如果是这样,代码会立即用于对 redirect_url 的 get 请求


推荐阅读