【问题标题】:Getting a 401 error when sending a POST for token, using Angular使用 Angular 为令牌发送 POST 时出现 401 错误
【发布时间】:2021-03-18 14:57:05
【问题描述】:

想知道我做错了什么。我正在尝试请求令牌以开始在 Angular 应用程序内使用 API。我已经成功地使用了您提供的 nodeJS SDK,但我想弄清楚如何使用 Angular 自己做这件事是一个额外的挑战,因为我并不精通 OAuth 的东西。显然我使用的客户 ID 和密码不同,但想避免在此处发布它们

httpOptions = {
    headers: new HttpHeaders({
      'Content-Type':  'application/x-www-form-urlencoded'
    })
  };
data = "grant_type=client_credentials&client_id={1234567897}&client_secret={123456}"


  getAmadeusAuthToken(): Observable<any> {
let headers = new HttpHeaders();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
return this.http.post('https://test.api.amadeus.com/v1/security/oauth2/token', this.data, this.httpOptions)
  }

我也尝试过以不同的方式格式化正文:

data = {
grant_type: 'client_credentials',
client_id: '1234567897',
client_secret: '123456',

}

也试过这个:

data = "grant_type=client_credentials&client_id=1234567897&client_secret=123456"

但我总是遇到同样的错误:

code: 38187
error: "invalid_client"
error_description: "Client credentials are invalid"
title: "Invalid parameters"

message: "Http failure response for https://test.api.amadeus.com/v1/security/oauth2/token: 

401 Unauthorized"
name: "HttpErrorResponse"
ok: false
status: 401
statusText: "Unauthorized"
url: "https://test.api.amadeus.com/v1/security/oauth2/token"

为了确保我没有输入错误的客户端 ID 或 Secret,我在您提供的 nodeJS SDK 中输入了相同的 ID,并且它可以正常工作。任何形式的帮助将不胜感激。我确定我在发布请求时做错了。

【问题讨论】:

  • 仅供参考,因为您一直说“您提供了”--> 这不是对任何公司的技术支持。这是一个公共问答网站;我们没有提供任何东西。 :)
  • 啊,是的,抱歉,我通过此处标记 Amadeus 以获得支持,因为您可以通过这种方式请求有关 API 相关问题的帮助,但我想通了,抱歉,如果我在帖子中听起来很笨拙

标签: angular amadeus


【解决方案1】:

在通过 api 测试器运行它后,我最终弄明白了。 Angular HTTP 对 IMO 不是很有帮助,无论如何,对于将来可能遇到问题的任何人来说,这里都是解决方案,在需要重构之前快速而肮脏:

httpOptions = {
headers: new HttpHeaders({
  'Content-Type':  'application/x-www-form-urlencoded'
})
  };


dataString = "grant_type=client_credentials&client_id= 1234567897&client_secret= 123456"

  getAmadeusAuthToken(): Observable<any> {
    return this.http.post('https://test.api.amadeus.com/v1/security/oauth2/token', this.dataString, this.httpOptions)
  }

【讨论】:

  • 您是否曾经在 Postman 中使用过这个功能?我下载了他们的 Postman 收藏,但无法在其中使用。
  • 我已经让它在 API Tester 和 ThunderClient 以及我的 Angular 应用程序中工作,我自己不使用邮递员
猜你喜欢
  • 2014-05-03
  • 2020-12-19
  • 1970-01-01
  • 2021-03-30
  • 2014-02-15
  • 2011-10-25
  • 2014-02-08
  • 2019-04-20
  • 2013-03-23
相关资源
最近更新 更多