OGeek|极客世界-中国程序员成长平台

标题: ios - 带有 IOS、node.js、passport 和 google 的 OAuth 2 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 15:39
标题: ios - 带有 IOS、node.js、passport 和 google 的 OAuth 2

我正在尝试让一个示例 Web 应用程序使用 OAuth 2 来确保接受 IOS 客户端的安全性,但遇到了一些麻烦。

浏览器客户端

使用 node.js/passport我添加了我的 google 客户端 ID + 密码 (https://code.google.com/apis/console) 的示例代码。效果很好——我所要做的就是将重定向 URI 指向我的服务器的授权回调。

IOS 客户端

使用与上面相同的服务器端代码,以及用于 IOS 的 gtm-oauth2 库,我遇到了一些麻烦。我按照谷歌的说明为已安装的应用程序创建了一个客户端 ID,并修改了服务器以使用它们并将它们添加到 ios 应用程序中。该应用程序能够访问 google 登录页面,但在重定向时会出现错误(这是有道理的,因为我没有更改重定向 uri)。

谷歌给了我two options对于重定向 URI:

  1. 放东西或其他
  2. 本地主机

服务器需要某种排序或重定向,但 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('/');
});

我得到两个不同的错误:

  1. 使用适用于浏览器客户端的重定向,以及已安装的/ios 应用客户端 ID + 密码 - 重定向错误
  2. 使用 ios 客户端 ID + secret + ios + 重定向 (urn) - 客户端错误

我是否需要将 IOS 重定向 URI 添加到 IOS 客户端,或者在 node.js 服务器中放入某种重定向参数来告诉它有关客户端的信息?还是我缺少一些基本的东西?



Best Answer-推荐答案


您在此处尝试实现的目标,即对 Installed Application 使用相同的凭据和 Web Server Applications流,不起作用。 Google 知道他们为哪种类型的应用程序颁发了凭据并执行此操作。(这是错误的,请参阅评论。)

您的场景的典型方法是在您的服务器上实现 Web 服务器应用程序流程,并通过打开 authorization endpoint URL 来启动登录。在 iOS 设备上,但将 redirect_uri 设置为您的服务器。这样您就可以在您的服务器上获得 访问 token 刷新 token ,并可以从那里调用 Google API。

您的 iOS 客户端和网络服务器之间的通信方式完全独立于其他一切。

关于ios - 带有 IOS、node.js、passport 和 google 的 OAuth 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12031202/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4