OAuth 的学习笔记
OAuth 的全流程
windows下遇到的一个坑爹问题
进行到Google给我返回 AccessToken 的步骤,代码片如下。
passport.use(
new GoogleStrategy(
{
clientID: keys.googleClientID,
clientSecret: keys.googleClientSecret,
callbackURL: '/auth/google/callback'
},
(accessToken,refreshToken,profile,done) => {
console.log('access token',accessToken);
console.log('refresh token',refreshToken);
console.log('profile',profile);
}
)
);
Node 并没有给我返回 accessToken
,而是报了下面的错误。
//网页显示
Internal Server Error
//Chrome Console 显示
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
//Node显示
InternalOAuthError: Failed to obtain access token
at Strategy.OAuth2Strategy._createOAuthError (E:\Dropbox\Tech\ReactEmailApp\server\node_modules\passport-oauth2\lib\strategy.js:379:17)
at E:\Dropbox\Tech\ReactEmailApp\server\node_modules\passport-oauth2\lib\strategy.js:166:45
at E:\Dropbox\Tech\ReactEmailApp\server\node_modules\oauth\lib\oauth2.js:191:18
at ClientRequest.<anonymous> (E:\Dropbox\Tech\ReactEmailApp\server\node_modules\oauth\lib\oauth2.js:162:5)
at emitOne (events.js:115:13)
at ClientRequest.emit (events.js:210:7)
at TLSSocket.socketErrorListener (_http_client.js:385:9)
at emitOne (events.js:115:13)
at TLSSocket.emit (events.js:210:7)
at emitErrorNT (internal/streams/destroy.js:64:8)
我是在 windows 下进行测试的,而 status of 500
和 Internal Server Error
的错误让我感觉是网络的问题。但是明明我能上 Google 的呀?在测试了全局模式之后一样报 500 错,我意识到是不是本地测试并不会走全局流量?
在 Chrome 中查看了一下代理
发现 PAC模式
下,代理是使用自动脚本配置。
而在全局模式下本地地址根本不使用代理服务器。
在 高级
选项中,localhost
这个地址也完全不使用代理,怪不得哪怕是全局模式也连不上。
那么全局模式下这个问题倒是容易解决了,直接把 localhost
删掉就好了。
至于Pac模式下,再看来要看下这个的 pac
里是怎么处理 localhost
这个连接的。
下载下来 我的pac脚本 后搜索了一下 并没有找到 localhost
,但是 127.0.0.1
这个地址倒是完全直连。
var wall_proxy = function(){ return "PROXY 127.0.0.1:1080;DIRECT;"; };
然而经过测试发现似乎没效果…看来只能全局模式了么…
不过=全局设置之后,总算拿到 accessToken 了。
治本之法
以上这种方式还是治标不治本,有没有从根源上解决 Strategy 的问题?
有的,而且非常简单… 加入一行 proxy: true
就好了…
passport.use(
new XXXStrategy(
{
clientID: XXXClientID,
clientSecret: XXXClientSecret,
callbackURL: '/auth/XXX/callback',
// 使得 Strategy 信任所有代理设置
proxy: true
},
......
我是空谷,独立产品经理、全栈设计师、全栈工程师。
技术栈为 React/Dva/Umi/Node/Egg/Electron
欢迎扫码加我微信交流,备注来源:CSDN