gpt4 book ai didi

java - Android:Google Sign 与 Sashido (Parse.com SDK) 集成,无需云代码

转载 作者:行者123 更新时间:2023-12-02 03:06:22 25 4
gpt4 key购买 nike

我写这个问题和答案是因为我还没有看到使用 Facebook 的 Parse SDK(或我的例子中的 Sashido)作为后端在 Android 上集成 Google 登录的完整解决方案没有云代码

相关问题:

<小时/>

如何在没有 Cloud Code 的情况下将 Google Sign in 与 Parse 后端集成?

最佳答案

首先,按照 Android 开发人员提供的步骤启动和实现集成。

  1. Start Integrating Google Sign-In into Your Android App
  2. Integrating Google Sign-In into Your Android App
  3. 在您的 Activity 的 onCreate 中,您需要构建 GoogleSignInOptions

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
    .requestIdToken(getString(R.string.default_web_client_id))
    .requestEmail()
    .build();

当您将 Google 服务添加到您的项目到您的 Google Developers 帐户时,您可以获取您的 web_client_id。了解更多信息:Creating a Google API Console project and client ID

  • 构建您的GoogleApiClient (使其成为全局实例private GoogleApiClient mGoogleApiClient;)

    mGoogleApiClient = new GoogleApiClient.Builder(this)
    .enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    Log.e("Failed", "failed" + connectionResult.getErrorMessage());
    }
    })
    .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
    .build();
  • 监听用于 Google 登录的专用按钮的点击,然后启动 Auth.GoogleSignInApi.getSignIntent(mGoogleApiClient);

    案例 R.id.btn_google:
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    startActivityForResult(signInIntent, RC_SIGN_IN);
    中断;

  • 确保您已为 RC_SIGN_IN 分配了一个值(我已经分配了 1000 个)

  • 现在开始向您的 onActivityResult 方法添加实现

    // Result returned from launching the Intent from
    // GoogleSignInApi.getSignInIntent(...);
    if (requestCode == RC_SIGN_IN) {
    GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
    handleSignInResult(result);
    } else {
    ParseFacebookUtils.onActivityResult(requestCode, resultCode, data);
    }
  • 现在处理登录请求:

    `private void handleSignInResult(GoogleSignInResult result) {
    Log.e("handleSignIn", "handleSignInResult:" + result.isSuccess());
    if (result.isSuccess()) {
    // Signed in successfully, show authenticated UI.
    final GoogleSignInAccount acct = result.getSignInAccount();
    if (acct != null) {
    ParseQuery<ParseObject> query = ParseQuery.getQuery("_User");
    query.setLimit(10000);
    query.whereEqualTo("email", acct.getEmail());
    query.findInBackground(new FindCallback<ParseObject>() {
    @Override
    public void done(List<ParseObject> objects, ParseException e) {
    if (e == null) {
    if (objects.size() == 0) {
    saveNewUserGoogle(acct);
    } else {
    loginGoogleUser(objects.get(0), acct);
    }
    } else {
    saveNewUserGoogle(acct);
    }
    }
    });
    }
    } else {
    Log.e("failed", "failed to sign in");
    // Signed out, show unauthenticated UI.
    }
    }`

    因此,此方法的作用是,如果对 GoogleSignIn 请求的请求成功,则获取帐户详细信息,查询数据库中的 _User 表,并查看具有该帐户的电子邮件是否匹配。如果是,请让用户登录。

    private void loginGoogleUser(ParseObject j, GoogleSignInAccount acct) {
    ParseUser.logInInBackground(j.getString("用户名"), String.valueOf(acct.getId()), new LogInCallback() {
    公共(public)无效完成(ParseUser用户,ParseException e){
    如果(用户!=空){
    Intent i = new Intent(AllLoginActivity.this, MainActivity.class);
    开始 Activity (一);
    结束();
    } 别的 {
    Log.e("失败", "无法验证");
    }
    }
    });
    }

    否则注册用户:

    private void saveNewUserGoogle(GoogleSignInAccount acct) {
    google = true;
    final ParseUser user = new ParseUser();
    String mFullName = acct.getDisplayName();
    String mEmail = acct.getEmail();
    String mProfilePic = String.valueOf(acct.getPhotoUrl());
    String mUsername = acct.getId();
    String password = acct.getId();
    user.setUsername(mUsername);
    user.setEmail(mEmail);
    user.setPassword(password);
    user.put("userEmail", mEmail);
    user.put("uniqueID", mUsername);
    user.put("name", mFullName);
    user.put("loginMethod", "Google");
    user.put("profilePicture", mProfilePic);
    user.signUpInBackground(new SignUpCallback() {
    public void done(ParseException e) {
    if (e == null) {
    Log.e("SaveTest", "Successful");
    //sign user up
    } else {
    switch (e.getCode()) {
    case ParseException.USERNAME_TAKEN:
    Toast.makeText(context, "Sorry, this username has already been taken.", Toast.LENGTH_SHORT).show();
    break;
    case ParseException.USERNAME_MISSING:
    Toast.makeText(context, "Sorry, a username is needed", Toast.LENGTH_SHORT).show();
    break;
    case ParseException.PASSWORD_MISSING:
    Toast.makeText(context, "Sorry, a password is needed.", Toast.LENGTH_SHORT).show();
    break;
    case ParseException.OBJECT_NOT_FOUND:
    Toast.makeText(context, "invalid credentials", Toast.LENGTH_SHORT).show();
    break;
    case ParseException.CONNECTION_FAILED:
    Toast.makeText(context, "Sorry, internet is needed.", Toast.LENGTH_SHORT).show();
    break;
    default:
    Log.d("Testing", e.getLocalizedMessage());
    break;
    }
    }
    }
    });

    }

    因此,如果您将 Parse 上的密码设置为 clientID,那么该密码对于该用户来说是唯一的,并且可以由 Parse 读取并由 Google 提供。

    注意:我将用户名设置为 Google 标识符,然后当他们成功连接并注册后,我会显示一个用户名对话框,他们可以在其中输入用户名,因此可以显示为纯文本而不是数字的内容。

    关于java - Android:Google Sign 与 Sashido (Parse.com SDK) 集成,无需云代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41681202/

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