【问题标题】:Connecting to api using basic auth Angular 8 Express js使用基本身份验证 Angular 8 Express js 连接到 api
【发布时间】:2019-09-19 20:27:56
【问题描述】:

我已经设置了一个 angular 8 应用程序,它连接到一个 express API。

我在本地运行它,进行测试。 我的前端应用程序连接到http://localhost:4200/,后端连接到http://localhost:3000/ 我已经设置了一条快速路由来连接到https://api.podbean.com/v1/podcasts?access_token=baee9cb65384a814e704adc626dc969bb019f84d 效果很好,返回所有播客

但如果我使用https://api.podbean.com/v1/oauth/debugToken?access_token=baee9cb65384a814e704adc626dc969bb019f84d,debugToken 端点永远不会通过快速路由工作 使用带有基本身份验证 clientId = '7faf9a7ad38a01c7d900c' client_secret = 'a7a3825f02be39c57ff44' 的邮递员可以正常工作,但是通过本地主机连接时永远不会 我正在使用 GET

它一定是连接的,因为我得到了一个返回的对象,虽然这是一个错误

在 Angular 中:

debug() {
    const httpOptions = {
      headers: new HttpHeaders({
        'Content-Type':  'application/json',
        Authorization: 'Basic ' + btoa('7faf9a7ad38a01c7d900c:a7a3825f02be39c57ff44')
      })
    };
    console.log(httpOptions);
    return this.http.get(`${this.configUrl}/debug/`, httpOptions);
  }

快递:

  router.get('/debug', function (req, res, next) {
    var options = {
      url: `https://api.podbean.com/v1/oauth/debugToken?access_token=${accessToken}`
    }
     request(options, function (err, response, body) {
        console.log( req.headers);
       if(err){
         return res.status(500).json({
           title: 'An error has occured',
           error: err
         })
       }
            res.json(JSON.parse(body));
           next();
      })

  });

当我在 express/node 端记录请求标头时

{host: 'localhost:3000',
  connection: 'keep-alive',
  pragma: 'no-cache',
  'cache-control': 'no-cache',
  'sec-fetch-mode': 'cors',
  origin: 'http://localhost:4200',
  authorization: 'Basic N2ZhZjlhN2FkMzhhMDFjN2Q5MDBjOmE3YTM4MjVmMDJiZTM5YzU3ZmY0NA==',
  'content-type': 'application/json',
  accept: 'application/json, text/plain, */*',
  'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36',
  dnt: '1',
  'sec-fetch-site': 'same-site',
  referer: 'http://localhost:4200/',
  'accept-encoding': 'gzip, deflate, br',
  'accept-language': 'en-GB,en;q=0.9,en-US;q=0.8,it;q=0.7,es;q=0.6' }

返回对象:

{"error":"invalid_token","error_description":""}

这告诉我我正在连接,只是不正确

【问题讨论】:

  • 您是否尝试过将令牌类型从“Basic”切换为“Bearer”?这似乎是您尝试集成的 api 的配置问题。
  • 感谢您的回复,但是,这不起作用,文档说应该使用基本身份验证,而且,如果我使用邮递员并直接连接到端点,它也可以工作

标签: node.js angular express


【解决方案1】:

看起来你只是混淆了端点。您正在将基本身份验证从 Angular 页面发送到您的 Express 端点, 这没有多大意义,因为需要授权的是 https://api.podbean.com,而不是您的 Express 服务器。

尝试将基本身份验证凭据添加到从 Express 服务器到 api.podbean.com 的请求中

  router.get('/debug', function (req, res, next) {
    var options = {
      url: `https://api.podbean.com/v1/oauth/debugToken?access_token=${accessToken}`,
      headers: {               
         'Authorization': 'Basic ' + new Buffer("7faf9a7ad38a01c7d900c:a7a3825f02be39c57ff44").toString('base64')
      }
    }
    request(options, function (err, response, body) {
      ...

【讨论】:

  • 谢谢!我已经开始使用 var auth = 'Basic ' + Buffer.from(clientId+':'+client_secret).toString('base64'); 将我的授权添加到 express 的请求中。 req.headers.authorization = 身份验证;但它不起作用,但是在选项中添加标题对象就是这样做的,再次感谢
猜你喜欢
  • 2021-07-30
  • 2013-08-02
  • 1970-01-01
  • 1970-01-01
  • 2015-03-27
  • 1970-01-01
  • 2011-12-16
  • 1970-01-01
  • 2022-07-07
相关资源
最近更新 更多