Android三方登錄之google登錄

    在做公司的一個國外項目的時候,需要用到google的三方快捷登錄,中間碰到了一些坑,在此Mark一下,如果有同樣的問題的小夥伴看到可以少走些彎路,給自己也總結下開發過程。
點擊進入谷歌的官方文檔,基本上google三方登錄的流程條件都在裏面了,需要耐心的順流程
1.我按我自己的順序一點點寫吧,找到 https://console.firebase.google.com/ ,創建項目,然後創建應用
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
注意第二步的json,這些基本沒什麼問題,跟着說明文檔往下走就行
在這裏插入圖片描述以上這一步請注意:有的時候會因爲項目的問題,導致集成失敗,或者有衝突,這個解決方式只需要把版本調到降一些即可,這是我遇到的第一個小坑,因爲是繼承的祖傳代碼,不敢亂升級,所以只好用歷史版本即可
在這裏插入圖片描述
可以明顯的看到第四步的進度條一直在轉,這個請注意,在第三步添加Firebase SDK 之後,要點擊sync一下,讓關聯到服務器之後,這個時候會加快進度條,不然的話,小圓圈會一直轉下去的,集成是不成功的,就是按照順序往下走,不要跳過,跳過後也可以,需要重新設置,我跳過了,然後需要重新設置SDK方可。

代碼部分

1.在界面初始化加載

 GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .build();

// Build a GoogleSignInClient with the options specified by gso.
   mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

2.點擊登陸按鈕,

 findViewById(R.id.login_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent signInIntent = mGoogleSignInClient.getSignInIntent();
                startActivityForResult(signInIntent, RC_SIGN_IN);
            }
        });

3.回調

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

        // Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            // The Task returned from this call is always completed, no need to attach
            // a listener.
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            handleSignInResult(task);
        }
    }

    private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
        try {
            GoogleSignInAccount account = completedTask.getResult(ApiException.class);

            // Signed in successfully, show authenticated UI.
            Log.e(TAG, "account--------------->>>" + account);
        } catch (ApiException e) {
            // The ApiException status code indicates the detailed failure reason.
            // Please refer to the GoogleSignInStatusCodes class reference for more information.
            Log.e(TAG, "signInResult:failed code=" + e.getStatusCode());
        }
    }

基本上的流程順序就是這樣,我再來個整體代碼吧

package com.example.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.tasks.Task;

/**
 * @Author: mixiaojiu
 * @CreateDate: 2020/3/16 14:21
 * @Description:
 */
public class LoginGoogleActivity extends Activity {
    private final String TAG = LoginGoogleActivity.class.getSimpleName();
    private GoogleSignInClient mGoogleSignInClient;
    private int RC_SIGN_IN = 127;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_google);
        // Configure sign-in to request the user's ID, email address, and basic
// profile. ID and basic profile are included in DEFAULT_SIGN_IN.
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .build();

// Build a GoogleSignInClient with the options specified by gso.
        mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

        findViewById(R.id.login_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent signInIntent = mGoogleSignInClient.getSignInIntent();
                startActivityForResult(signInIntent, RC_SIGN_IN);
            }
        });
    }

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

        // Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            // The Task returned from this call is always completed, no need to attach
            // a listener.
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            handleSignInResult(task);
        }
    }

    private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
        try {
            GoogleSignInAccount account = completedTask.getResult(ApiException.class);

            // Signed in successfully, show authenticated UI.
            Log.e(TAG, "account--------------->>>" + account);
        } catch (ApiException e) {
            // The ApiException status code indicates the detailed failure reason.
            // Please refer to the GoogleSignInStatusCodes class reference for more information.
            Log.e(TAG, "signInResult:failed code=" + e.getStatusCode());
        }
    }
}

通過以上也能看的出來,其實代碼很簡單,就是因爲是國外環境,所以有很多的不便

不便之一: 開發之外,所使用的手機也要支持google服務,顧名思義你要使用外網環境,手機也要科學上網纔行
不便之二: 文檔閱讀不便,由於是國外的文檔,所以很多邏輯順序流程需要嘗試,還有就是,第一次建議使用demo測試,不要用於正式環境,因爲google的那個項目id刪除需要時間,如果有問題導致的誤操作,不太好恢復,我碰到了,很坑
其實整體來說,google登錄的代碼不復雜,就是註冊賬號時候繞一些,真正的代碼部分不復雜,另外有個12500的錯誤代碼困擾了很久,這個有時候不僅僅是share1配置的問題,我上網頁看來很多,基本上各種情況都有,我的問題是因爲要手動的開google服務。再次Mark

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章