OAuth 2与IOS,node.js,护照和谷歌

我试图让一个示例Web应用程序工作使用OAuth 2接受IOS客户端的安全性,有点麻烦。

浏览器客户端

使用来自我的node.js /护照示例代码添加了我的Google客户端ID +密码(https://code.google.com/apis/console)。 非常好用 – 我所要做的就是将redirectURI指向我服务器的授权callback。

IOS客户端

使用与上面相同的服务器端代码和IOS的gtm-oauth2库,我遇到了一些麻烦。 我根据谷歌的说明为安装的应用程序创build了一个客户端ID,并修改了服务器以使用这些ID并将它们添加到了ios应用程序中。 该应用程序能够到达谷歌login页面,但在redirect给出了一个错误(这是有道理的,因为我没有改变redirecturi)。

Google给了我两个redirectURI 选项 :

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

服务器需要进行一些sorting或redirect,但是IOSredirectURI中的子层不起作用,而且似乎不应该让服务器需要有一个需要validation的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. 使用浏览器客户端的redirect,以及安装的/ ios应用程序客户端ID +秘密 – redirect错误
  2. 使用ios客户端ID + secret + ios +redirect(urn) – 客户端错误

我是否需要将IOSredirectURI添加到IOS客户端,或者在node.js服务器中放入某种redirect参数来告诉客户端? 还是我缺less一些基本的东西?

你试图在这里实现的,即使用相同的凭据安装的应用程序和Web服务器应用程序stream程, 将无法正常工作 Google知道他们发布了哪些types的应用程序,并强制执行此操作。 (这是错误的,请参阅评论。)

您的scheme的典型方法是在您的服务器上实现Web服务器应用程序stream程 ,并通过在iOS设备上打开授权端点URL来启动login,但将redirect_uri设置为您的服务器。 这样你就可以在服务器上获得访问令牌刷新令牌 ,并可以从那里调用Google API。

您的iOS客户端和您的networking服务器之间的沟通方式完全独立于其他方面。