如何在 Android 上的 Parse 后端链接 Google + 登录用户? [英] How to link Google + signed in users on Parse backend on Android?

查看:24
本文介绍了如何在 Android 上的 Parse 后端链接 Google + 登录用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的安卓应用中使用 Parse 已经 3 个月了.现在我想在应用程序中添加电子邮件登录和社交登录(Facebook 和 Google+).我已成功添加电子邮件和 fb 登录名,用户可以同时连接电子邮件或 Facebook 或其中之一,该应用程序将识别用户.

I've been using Parse for 3 months in my android app. Now I want to add email login and social sign ons (Facebook and Google+) in the app. I have successfully added email and fb login and the user can connect both or either one of email or facebook and the app would recognise the user.

例如如果我通过电子邮件登录,然后连接 facebook,在另一台设备上使用该应用程序,通过 facebook 登录,该应用程序就会知道它是同一个用户,并且会为我定制并显示我的数据.电子邮件也有效.

e.g. If I login through email, then connect facebook, use the app on another device, login via facebook, the app would know it's the same user and would be customised for me and would show my data. And email also works.

我已添加 Android 版 Google+ 登录,但我无法连接用户的 Google+ 凭据与登录用户.

I have added Google+ sign-in for Android but I am not able to connect the user's Google+ credentials with the logged in user.

Parse Users 表有一个 authData 字段,它获取 facebook auth 数据,并将获取 Twitter 以及这两个登录都烘焙到 Parse SDK 中.

Parse Users table has an authData field which gets the facebook auth data and would get Twitter as well as both of these sign ons are baked into Parse SDKs.

Google+ 最好的做法是什么?我对数据库设计以及如何连接使用 Google+ 登录的用户感到困惑?

What should be the best thing to do for Google+? I'm confused about the db design as well as how to connect the user who signed in with Google+?

如果用户只是通过 Google+ 登录怎么办?如何创建 Parse 用户并在 Parse 上对用户进行身份验证?

What if the user just logs in via Google+? How do I make a Parse User and authenticate the user on Parse?

我对云代码和 Android 感到很满意,并且非常感谢能将我推向正确方向的某种帮助/说明.我从来没有使用过 OAuth2 并且使用 Parse 登录进行电子邮件和社交登录,我认为我不应该进入它.但如果我错了,请告诉我.

I'm comfortable with cloud code and Android and would really appreciate some sort of help/instructions just pushing me in the correct direction. I have never used OAuth2 and with Parse login for email and Social Sign ons, I don't think I should get into it. But let me know if I'm wrong.

谢谢!

更新:我已经阅读了很多关于解析问题的问题,并且多次检查了 become 方法(因为我一直认为我在阅读之后遗漏了一些东西).检查这个问题 - 我目前处于相同的情况.

Update: I have read a lot of questions on Parse Questions and have checked out the become method plenty of times (because I kept thinking I'm missing something after reading that). Check this question - I'm currently in the same situation.

我有:
1. 实施了 Google+ 登录.
2. 使用 GoogltAuthUtil 获得访问令牌.

卡在:
3. 用户登录Google+后,如何关联当前登录的Parse用户?
4. 如果 Google+ 是用户的第一个(也是唯一一个)登录选择,如何创建新的 Parse 用户?

I have:
1. Implemented Google+ sign in.
2. Got access token using GoogltAuthUtil.

Stuck with:
3. How to link currently signed in Parse user after the user signs in with Google+?
4. How to create a new Parse User if Google+ was the user's first (and only ) login choice?

推荐答案

这似乎与如何创建解析_User来自 Android Google 令牌的帐户?

以下是我在该线程中的回答:

Following is my answer in that thread:

流程如下:

  1. 用户授权并获取令牌
  2. 我们使用随机密码创建一个新用户

您可以在返回电子邮件的 newChooseAccountIntent() 方法中使用以下代码创建 ParseUser.

You can create a ParseUser using following code inside the newChooseAccountIntent() method that return email.

ParseUser user = new ParseUser();
user.setUsername(mEmail);
user.setPassword(randomPassword);
user.setEmail(mEmail);
user.signUpInBackground(new SignUpCallback() {
  public void done(ParseException e) {
    if (e == null) {
      // Hooray! Let them use the app now.
    } else {
      // Sign up didn't succeed. Look at the ParseException
      // to figure out what went wrong
    }
  }
});

2.回访用户

正如我在互联网上研究的那样,这是大多数人坚持的地方.流程如下:

2. Returning User

This is the where most of people stuck, as I researched over the Internet. The flow is as below:

  1. 用户授权,应用获得令牌
  2. 我们将此令牌传递给 Cloud Code 进行验证.我们需要检查此令牌是否由 Google 签名以及它是否适合我们(android-developers (2013).
  3. 验证令牌有效后,您可以使用Parse.Cloud.useMasterKey() 方法在Cloud Code 中查询用户并使用getSessionToken() 查询结果的方法.
  4. 通过调用 becomeInBackground 方法,使用会话密钥将登录状态保存在磁盘上
  1. User authorizes and the app gets a token
  2. We pass this token to Cloud Code to validate. We need to check if this token is signed by Google and if it is meant for us (android-developers (2013)).
  3. After you can verify that the token is valid, you can query for the user in Cloud Code using Parse.Cloud.useMasterKey() method and return the session key by using getSessionToken() method on the query result.
  4. Use the session key to save login state on disk by calling becomeInBackground method

要验证令牌,您可以将 Parse.Cloud.httprequest 发送到此端点:https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=.这在 Google 身份文档中有说明.您将收到如下数据:

To validate the token, you can send Parse.Cloud.httprequest to this endpoint: https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=. This is instructed in Google Identity Documentation. You will receive data as below:

{
 "iss": "https://accounts.google.com",
 "sub": "110169484474386276334",
 "azp": "1008719970978-hb24n2dstb40o45d4feuo2ukqmcc6381.apps.googleusercontent.com",
 "email": "billd1600@gmail.com",
 "at_hash": "X_B3Z3Fi4udZ2mf75RWo3w",
 "email_verified": "true",
 "aud": "1008719970978-hb24n2dstb40o45d4feuo2ukqmcc6381.apps.googleusercontent.com",
 "iat": "1433978353",
 "exp": "1433981953"
}

需要比较的是aud"、azp"和email",分别翻译为受众、授权方和电子邮件.

Things need to compare are "aud", "azp" and "email" which are translated as audience, authorized party and email.

在 Cloud Code 上查询当前用户:

var query = new Parse.Query(Parse.User);
query.equalTo("email",mEmail);
query.first({
  success: function(user) {
    // Use user..getSessionToken() to get a session token
  },
  error: function(user, error) {
    //
  },
  useMasterKey: true
});

注意:确保您有以下范围,以便在您检查 Cloud Code 时显示电子邮件:https://www.googleapis.com/auth/plus.profile.emails.read

Note: Make sure you have following scope so that the email will show up when you check on Cloud Code: https://www.googleapis.com/auth/plus.profile.emails.read

这篇关于如何在 Android 上的 Parse 后端链接 Google + 登录用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆