繁体   English   中英

使用基本身份验证 Angular 8 Express js 连接到 api

[英]Connecting to api using basic auth Angular 8 Express js

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

我在本地运行它,进行测试。 我的前端应用程序连接到http://localhost:4200/和后端到http://localhost:3000/我已经设置了一条快速路由来连接到Z5E056C500A1C4B6A7110B5podbean.com/v7。 access_token=baee9cb65384a814e704adc626dc969bb019f84d工作正常,返回所有播客

But the debugToken endpoint never works via the express route, if I use https://api.podbean.com/v1/oauth/debugToken?access_token=baee9cb65384a814e704adc626dc969bb019f84d Using postman with basic auth clientId = '7faf9a7ad38a01c7d900c' client_secret = 'a7a3825f02be39c57ff44' it works好的,但是当我通过本地主机连接时,我使用的是 GET

它必须正在连接,因为我收到了 object 返回,尽管这是一个错误

在 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' }

返回 object:

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

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

看起来你只是混淆了端点。 您正在将基本身份验证从 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) {
      ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM