如何从Android Google令牌创建解析_User帐户?

fad*_*bee 6 android oauth-2.0 parse-platform

我找到了一些有用信息的片段.

http://blog.parse.com/announcements/bring-your-own-login/显示我如何登录Android应用,一旦我有一个Parse令牌.

我可以成功获取手机Google帐户的Google令牌.

https://developers.google.com/android/guides/http-auth

/**
 * Gets an authentication token from Google and handles any
 * GoogleAuthException that may occur.
 */
protected String fetchToken() throws IOException {
    try {
        return GoogleAuthUtil.getToken(mActivity, mEmail, mScope);
    } catch (UserRecoverableAuthException userRecoverableException) {
        // GooglePlayServices.apk is either old, disabled, or not present
        // so we need to show the user some UI in the activity to recover.
        mActivity.handleGoogleException(userRecoverableException);
    } catch (GoogleAuthException fatalException) {
        // Some other type of unrecoverable exception has occurred.
        // Report and log the error as appropriate for your app.
    }
    return null;
}
Run Code Online (Sandbox Code Playgroud)

如何让Parse使用Google令牌创建Parse令牌?

我认为这将涉及编写一些Cloud Code,但我不清楚Cloud Code应该做什么.我认为它需要为Google令牌创建或找到新的_User并返回Parse令牌.

是否有任何Parse Cloud Code示例如何处理Google Android登录/注册,或Faceboook/Twitter以外的任何其他示例?

Ral*_*ius 7

注意:此答案不适用于开源解析服务器,因为它仅使用可撤销会话.查看parse-server/issues/1392以获取进一步更新

更新(2016年1月):

您需要关闭撤销会话,以便调用getSessionTokenParse.User.转到应用程序设置 >> 用户 >>关闭需要撤销会话.这在2016年并不新鲜,但在给出答案时,作者并不知道这一变化.


我将分为2个案例,以便于遵循:新用户和返回用户.

1.新用户

流程如下:

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

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

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
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

2.退回用户

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

  1. 用户授权并且应用获取令牌
  2. 我们将此令牌传递给Cloud Code进行验证.我们需要检查此令牌是否由Google签名,以及它是否适用于我们(android-developers(2013)).
  3. 在验证令牌有效后,您可以使用Parse.Cloud.useMasterKey()方法在Cloud Code中查询用户,并使用getSessionToken()查询结果上的方法返回会话密钥.
  4. 使用会话密钥通过调用becomeInBackground方法将登录状态保存在磁盘上

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

{
 "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"
}
Run Code Online (Sandbox Code Playgroud)

需要比较的是"aud","azp"和"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
});
Run Code Online (Sandbox Code Playgroud)

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

  • http://pastebin.com/fVLQr302,这是我按照此处的说明创建的完整工作解析云代码片段。http://pastebin.com/133LVYbm,ios客户端代码使用相同。可能会帮助某人将所有部分联系起来。 (2认同)