gpt4 book ai didi

ios - OAuth 2 与 IOS、node.js、护照和谷歌

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:14:18 25 4
gpt4 key购买 nike

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

浏览器客户端

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

IOS 客户端

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

Google 给我 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 + 密码 + 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、护照和谷歌,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12031202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com