【问题标题】:Unable to retrieve Reddit OAuth2 access token using HTTP Basic Auth, 401 failure response无法使用 HTTP 基本身份验证检索 Reddit OAuth2 访问令牌,401 失败响应
【发布时间】:2019-07-08 02:43:58
【问题描述】:

按照此处的文档:https://github.com/reddit-archive/reddit/wiki/oauth2,我尝试获取访问令牌失败并且无法弄清楚原因。

我从我的 POST 请求中收到以下 401(未经授权)响应:

{
  "headers": {
    "normalizedNames": {},
    "lazyUpdate": null
  }
  ,
  "status":401,
  "statusText":"Unauthorized",
  "url":"https://www.reddit.com/api/v1/access_token",
  "ok":false,
  "name":"HttpErrorResponse",
  "message":"Http failure response for https://www.reddit.com/api/v1/access_token: 401 Unauthorized",
  "error": {
    "message": "Unauthorized", "error": 401
  }
}

我仔细检查了我的 clientId、clientSecret、redirect_uri 和 code 是否都是它们应该是的。我还检查了btoa 函数是否生成了我所期望的。我尝试使用不带“User-Agent”和“Content-Type”键的标头,但不管有没有,它都不起作用。

这是我正在做的事情:

getAccessToken() {
  const httpOptions = {
    headers: new HttpHeaders({
      'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:66.0) Gecko/20100101 Firefox/66.0',
      'Content-Type': 'application/x-www-form-urlencoded',
      'Authorization': 'Basic ' + btoa(myClientId + ':' + myClientSecret)
    })
  }

  const postData = {
    grant_type: 'authorization_code',
    code: myCode, 
    redirect_uri: 'http://localhost:4200',
  };

  return this.http.post('https://www.reddit.com/api/v1/access_token', postData, httpOptions )
}

任何帮助都非常感谢,我已经坚持了太久了。

【问题讨论】:

  • 您能发布完整的错误响应吗?
  • 用错误响应更新了我的帖子
  • 不知道oidc-client.js,我去看看,谢谢!

标签: angular reddit


【解决方案1】:
  1. 您的 User-Agent 字符串不安全,将被 Reddit 服务器拒绝。删除它 - 它将自动为您处理。

  2. 确保您的重定向 URI 与您在 postData 中的重定向 URI 完全匹配。很容易忘记 URI 末尾的尾随 / 斜杠,或者将 httphttps 混淆,如果您的应用配置中有一个设置,您的授权将失效。

    李>

  1. 您的grant_type 应该作为字符串传递,而不是Object

我能够使用以下函数从他们的 API 获得有效响应:

getAccessToken() {
  const httpOptions = {
    headers: new HttpHeaders({
      'Content-Type': 'application/x-www-form-urlencoded',
      'Authorization': 'Basic ' + btoa(myClientId + ':' + myClientSecret),
    }),
  };

  const grantType = 'authorization_code';
  const code = myCode;
  const redirectUri = 'http://localhost:4200/';
  const postdata = `grant_type=${grantType}&code=${code}&redirect_uri=${redirectUri}`;

  return this.http.post('https://www.reddit.com/api/v1/access_token', postdata, httpOptions);
}

最后一点 - 通过在getAccessToken() 服务调用的结果中放置一个console.log 来进行调试相对容易。当我在现有的 Angular 应用程序之一中执行您的代码时,它提供了更清晰的错误消息:

this.redditService
  .getAccessToken()
  .subscribe(response => console.log(response));

// Output: { error: "unsupported_grant_type" }

【讨论】:

  • 感谢您的回复!我完全复制了您的代码,但仍然收到相同的错误响应。我一直按照您提到的方式登录控制台,但从未收到unsupported_grant_type 错误,总是 401 Unauthorized。我确定我的 clientId、clientSecret 和 redirectUri 是正确的,因为它们是从上一步获取代码所必需的。让我觉得问题可能来自此代码之外的其他内容。可能与 CORS 相关?... 编辑:看起来它与 CORS 无关。
  • 如果您完全复制了我的代码并且您得到的响应是 401 响应,那么唯一可能的解释(根据文档)是您作为 HTTP 基本授权发送的客户端凭据无效。四重检查您是否使用了正确的值作为您的 clientID、clientSecret 以及您的 redirectURI 是否完全相同。我更新了屏幕截图以显示 clientID 和 secret 在 reddit 的应用首选项中的位置。
  • 另外请记住,reddit API 上的授权代码一次仅有效 1 小时,除非您特别请求永久代码。
  • 我检查了我的 clientID、clientSecret 和 redirectURI 很多次,它们完全符合它们的预期,但我仍然得到相同的 401...wtf。关于您的最后一条评论,我正在申请永久令牌。
猜你喜欢
  • 2018-09-07
  • 2014-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-25
  • 2016-09-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多