不得不承认,在 firewall
之内网络环境太复杂了。不过这个大多数人都不会如此强烈明显地感知到。
在此记录一下最近碰到的一个做 Google +
第三方登录的时候碰上的问题和解决办法。
问题的具体表现在于:通过 passport
这个库,走 OAuth 2.0
协议以便拿到第三方授权来做登录模块,但依旧拿不到第三方服务提供商 Google +
返回来的 access token
,最终没办法拿到用户的信息。
得到的返回消息是:
InternalOAuthError: Failed to obtain access token
而 status of 500 和 Internal Server Error 的错误让我感觉是网络的问题。
即便是通过系统的网络设置里边,吧 localhost
从 “忽略主机或域的代理设置” 列表当中去掉也无济于事。
在 Stack Overflow
上搜索到的答案也没能够直接解决问题,但是因此得到了一些启发:去 OAuth
依赖库里边修改源码,手动让其去走代理线路。
在 your_project/node_modules/oauth/lib/oauth2.js
目录中做如下修改:
var querystring= require('querystring'),
crypto= require('crypto'),
https= require('https'),
http= require('http'),
URL= require('url'),
OAuthUtils= require('./_utils');
// line codes to add
var HttpsProxyAgent = require('https-proxy-agent');
let httpsProxyAgent = null
if (process.env['https_proxy']) {
httpsProxyAgent = new HttpsProxyAgent("http://127.0.0.1:1087");
// fill in your proxy agent ip and port
}
....
// line codes to add
options.agent = httpsProxyAgent;
this._executeRequest( http_library, options, post_body, callback );
}
exports.OAuth2.prototype._request= function(method, url, headers, post_body, access_token, callback) {
...
重新运行代码,便可以在 callback function 当中拿到所需要的 profile 数据。
网友评论