Javascript ExpressJS Node.js角度错误:获取OAuth2 API授权令牌时getaddrinfo ENOTFOUND
我已经编写了一个angular应用程序,只要点击一个按钮,就会向Node/Express.js端点发出HTTP post请求,请求获得授权令牌 在《邮差》中,我成功地做到了以下几点:Javascript ExpressJS Node.js角度错误:获取OAuth2 API授权令牌时getaddrinfo ENOTFOUND,javascript,node.js,angular,express,oauth-2.0,Javascript,Node.js,Angular,Express,Oauth 2.0,我已经编写了一个angular应用程序,只要点击一个按钮,就会向Node/Express.js端点发出HTTP post请求,请求获得授权令牌 在《邮差》中,我成功地做到了以下几点: 已配置基本OAuth2授权令牌的相关凭据并获取访问令牌 我使用访问令牌调用restful端点,并在ldap目录上创建一个用户 在angular应用程序中,我创建了以下关键组件: 用户界面 AuthResponse接口 身份验证服务 用户界面组件 以下是angular frontend应用程序中Auth服务的逻辑摘录
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { User } from './../auth/user';
import { AuthResponse } from './../auth/auth-response';
import { tap } from 'rxjs/operators';
import { Observable, BehaviorSubject } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class AuthService {
AUTH_SERVER = "http://localhost:3000";
authSubject = new BehaviorSubject(false);
constructor(private httpClient: HttpClient) { }
register(user: User): Observable<AuthResponse> {
return this.httpClient.post<AuthResponse>(`${this.AUTH_SERVER}/register`, user).pipe(
tap((res: AuthResponse ) => {
if (res.token) {
console.log("ACCESS_TOKEN : "+ res.token.access_token);
localStorage.set("ACCESS_TOKEN", res.token.access_token);
localStorage.set("EXPIRES_IN", res.token.token_type);
localStorage.set("ACCESS_TOKEN", res.token.expires_in);
localStorage.set("EXPIRES_IN", res.token.refresh_token);
this.authSubject.next(true);
}
})
);
}
}
下面是html片段,该调用包含调用register端点的按钮:
<div>
<button (click)="register()" >Register</button>
</div>
my nodejs app.js中的以下端点可以工作:
路由器.get('/getTestResponse',(req,res)=>{
api\U助手。进行api\U调用(“”)
。然后(响应=>{
res.json(响应)
})
.catch(错误=>{
res.send(错误)
})
})
它使用以下辅助函数:
const request = require('request')
module.exports = {
/*
** This method returns a promise
** which gets resolved or rejected based
** on the result from the API
*/
make_API_call : function(url){
return new Promise((resolve, reject) => {
request(url, { json: true }, (err, res, body) => {
if (err) reject(err)
resolve(body)
});
})
}
}
问题是我在angular应用程序中单击register按钮时出现以下错误。
但是,nodejs/register端点正在被触发,即它将从请求对象检索到的用户名和密码记录到控制台:
Entering the server endpoint
events.js:288
throw er; // Unhandled 'error' event
^
Error: getaddrinfo ENOTFOUND https://[hostname]:[port-no]/osp/a/idm/auth/oauth2/token
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:64:26)
Emitted 'error' event on ClientRequest instance at:
at TLSSocket.socketErrorListener (_http_client.js:426:9)
at TLSSocket.emit (events.js:311:20)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
errno: 'ENOTFOUND',
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'https://[hostname]:[port-no]/osp/a/idm/auth/oauth2/token'
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! registration-express-app@0.0.1 start: `node ./src/app/app.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the registration-express-app@0.0.1 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/admin/.npm/_logs/2020-03-28T08_56_14_947Z-debug.log
问题在于http.request选项。基本上,hostname:应该只是hostname,而不是hostname! 然后对端点URI的其余部分使用path属性,即/xxx/auth/oauth2/token 这使事情有所进展,但我现在遇到另一个错误“错误:证书链中的自签名证书” 我将就此结束这个问题,以确保其他人能够从我的经验中获益。 回馈社会 请参阅下面的一些不错的参考资料:
const request = require('request')
module.exports = {
/*
** This method returns a promise
** which gets resolved or rejected based
** on the result from the API
*/
make_API_call : function(url){
return new Promise((resolve, reject) => {
request(url, { json: true }, (err, res, body) => {
if (err) reject(err)
resolve(body)
});
})
}
}
Entering the server endpoint
events.js:288
throw er; // Unhandled 'error' event
^
Error: getaddrinfo ENOTFOUND https://[hostname]:[port-no]/osp/a/idm/auth/oauth2/token
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:64:26)
Emitted 'error' event on ClientRequest instance at:
at TLSSocket.socketErrorListener (_http_client.js:426:9)
at TLSSocket.emit (events.js:311:20)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
errno: 'ENOTFOUND',
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'https://[hostname]:[port-no]/osp/a/idm/auth/oauth2/token'
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! registration-express-app@0.0.1 start: `node ./src/app/app.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the registration-express-app@0.0.1 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/admin/.npm/_logs/2020-03-28T08_56_14_947Z-debug.log