我正在尝试使用 OAuth 2 来获取接受 IOS 客户端的安全性示例 Web 应用程序,但遇到了一些麻烦。
浏览器客户端
使用node.js/passport我添加了我的 google 客户端 ID + key (https://code.google.com/apis/console) 的示例代码。效果很好 - 我所要做的就是使重定向 URI 指向我的服务器的授权回调。
IOS客户端
使用与上面相同的服务器端代码以及适用于 IOS 的 gtm-oauth2 库,我遇到了一些麻烦。我按照 google 的说明为已安装的应用程序创建了一个客户端 ID,并修改了服务器以使用这些客户端 ID,并将它们添加到 ios 应用程序中。该应用程序能够访问 google 登录页面,但在重定向时出现错误(这是有道理的,因为我没有更改重定向 uri)。
Google 给我 two options对于重定向 URI:
- 瓮一些东西
- 本地主机
服务器需要某种排序或重定向,但 IOS 重定向 URI 中的子项不起作用,而且考虑到服务器需要调用某个 URI 进行验证,它们似乎不应该这样做:
passport.use(new GoogleStrategy({
clientID: GOOGLE_CLIENT_ID,
clientSecret: GOOGLE_CLIENT_SECRET,
callbackURL: "http://127.0.0.1:3000/auth/google/callback"
},
function(accessToken, refreshToken, profile, done) {
// asynchronous verification, for effect...
process.nextTick(function () {
// To keep the example simple, the user's Google profile is returned to
// represent the logged-in user. In a typical application, you would want
// to associate the Google account with a user record in your database,
// and return that user instead.
return done(null, profile);
});
}
));
...
app.get('/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
});
app.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});
我收到两个不同的错误:
- 使用适用于浏览器客户端的重定向以及已安装/iOS 应用客户端 ID + key - 重定向错误
- 使用 ios 客户端 ID + key + ios + 重定向 (urn) - 客户端错误
我是否需要将 IOS 重定向 URI 添加到 IOS 客户端,或者在 Node.js 服务器中放入某种重定向参数来告诉它有关客户端的信息?或者我错过了一些基本的东西?
最佳答案
您想要在这里实现的目标,即对 Installed Application 使用相同的凭据和 Web Server Applications流,不起作用。 Google 知道他们为哪种类型的应用程序颁发了凭据并强制执行此操作。(这是错误的,请参阅评论。)
您的场景的典型方法是在您的服务器上实现Web 服务器应用程序流,并通过打开 authorization endpoint URL 来启动登录。在 iOS 设备上,但将 redirect_uri
设置为您的服务器。这样,您就可以在服务器上获取访问 token 和刷新 token ,并可以从那里调用 Google API。
iOS 客户端和网络服务器之间的通信方式完全独立于其他一切。
关于ios - OAuth 2 与 IOS、node.js、passport 和 google,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12031202/