Google登录无法在Real Device中运行

阿卜杜拉·纳希德(Abdullah Al Nahid)

我正在从这里使用Google的friendlychat android代码:https//codelabs.developers.google.com/codelabs/firebase-android/#0

代码:https//github.com/firebase/friendlychat-android

将聊天应用程序连接到了Firebase。在我的android模拟器(Pixel 2 XL API 27)中工作正常。可以登录,发送消息。

但是每当我在手机(Huawei Y5ii;旧设备)中使用该应用程序时,它就会显示登录页面。我单击登录,选择我的电子邮件,但它从未打开过聊天屏幕。无法找出问题所在。

SignInActivity.java

package com.google.firebase.codelab.friendlychat;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.GoogleAuthProvider;

public class SignInActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener,
        View.OnClickListener {

    private static final String TAG = "SignInActivity";
    private static final int RC_SIGN_IN = 9001;
    private SignInButton mSignInButton;

    private GoogleApiClient mGoogleApiClient;
    private FirebaseAuth mFirebaseAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_in);

        // Assign fields
        mSignInButton = (SignInButton) findViewById(R.id.sign_in_button);

        // Set click listeners
        mSignInButton.setOnClickListener(this);

        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();

        // Initialize FirebaseAuth
        mFirebaseAuth = FirebaseAuth.getInstance();
    }

    private void handleFirebaseAuthResult(AuthResult authResult) {
        if (authResult != null) {
            // Welcome the user
            FirebaseUser user = authResult.getUser();
            Toast.makeText(this, "Welcome " + user.getEmail(), Toast.LENGTH_SHORT).show();

            // Go back to the main activity
            startActivity(new Intent(this, MainActivity.class));
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sign_in_button:
                signIn();
                break;
            default:
                return;
        }
    }

    private void signIn() {
        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            if (result.isSuccess()) {
                // Google Sign In was successful, authenticate with Firebase
                GoogleSignInAccount account = result.getSignInAccount();
                firebaseAuthWithGoogle(account);
            } else {
                // Google Sign In failed
                Log.e(TAG, "Google Sign In failed.");
            }
        }
    }

    private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
        Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
        AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
        mFirebaseAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());

                        // If sign in fails, display a message to the user. If sign in succeeds
                        // the auth state listener will be notified and logic to handle the
                        // signed in user can be handled in the listener.
                        if (!task.isSuccessful()) {
                            Log.w(TAG, "signInWithCredential", task.getException());
                            Toast.makeText(SignInActivity.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                        } else {
                            startActivity(new Intent(SignInActivity.this, MainActivity.class));
                            finish();
                        }
                    }
                });
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        // An unresolvable error has occurred and Google APIs (including Sign-In) will not
        // be available.
        Log.d(TAG, "onConnectionFailed:" + connectionResult);
        Toast.makeText(this, "Google Play Services error.", Toast.LENGTH_SHORT).show();
    }

}
阿伦·尚卡

正如我们在评论中讨论的那样,我将其添加为答案以供将来参考:

调试和发行签名证书的SHA1签名都应添加到firebase项目中,然后在部署之前必须在应用程序中添加该firebase项目中的google-services.json文件。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Google登录无法正常运行

Firebase Google登录无法正常运行

Firebase Google登录无法正常运行(Android)

无法登录Android Studio中的Google帐户

使用Google for Android登录无法在发行版APK中运行

无法从Android中的Google+获取登录用户的信息

Google Hangouts Chrome扩展程序中的永久登录按钮;无法登录并使用该应用

无法推送文件,因为我在 Bitbucket 中以 Google 登录身份登录

使用“ LinkedIn”登录无法在真实设备中运行

Firebase登录无法正常运行

.bashrc无法在登录时运行

使用Google+的Soundcloud API移动OAUTH登录无法在Android上运行

Google登录无法在带有Xcode 8 beta 6的iOS 10 Beta 7上运行

使用Facebook时Firebase无法登录,在Google上可以正常运行

无法在Google Colab中运行Pyspark

无法在Google App Engine中运行Coursebuilder

无法在Google Colab中运行FlappyBird PLE

横幅 Google AdMob 无法在 Swift 中运行

使用cordova的Google登录无法正常工作。Android中的错误10和IOS中的错误请求

登录系统在Postman中正常运行,但在浏览器中无法正常运行

Shell中的运行屏幕无法找到会话,但是在ssh登录期间可以运行屏幕

计算Laravel中无法登录的登录数

错误4003:无法ssh登录到我在Google Cloud Platform中创建的实例

在Alpha测试中释放已签名的APK后,Google登录无法正常工作

无法创建新的客户端ID(适用于iOS SDK中的Google Plus登录)

无法登录Google Cloud Platform中托管的datastax Cassandra群集中的cqlsh

使用 Firebase 的 Google 登录方法无法通过 Android Studio 中的版本控制共享项目

无法在 QWebEngineView 中登录 google:此应用可能不安全

无法集成Google登录?