Android 應用程序集成Google 登錄及二次封裝


最新更新(2020/4/15)

最新有個項目要集成 google 登陸,我又又又又 重新做了一遍,發現很多重要 api 都變了,這里重新總結一下,用 Kotlin 編寫。

首先依賴包已經更新到 18.0.0 

 api 'com.google.android.gms:play-services-auth:18.0.0'

GoogleLoginUtil 工具類:

package com.enjoy.literaturemodule.account.login.google

import android.content.Context import androidx.fragment.app.FragmentActivity import com.enjoy.library.utils.LogUtils import com.enjoy.literaturemodule.R import com.google.android.gms.auth.api.signin.* import com.google.android.gms.common.api.ApiException import com.google.android.gms.tasks.Task /** * @author yanjun.zhao * @time 2020/4/15 3:20 PM * @desc Google 登陸 */ class GoogleLoginUtil(var activity: FragmentActivity, private var googleSignListener: GoogleSignListener) { private var gso: GoogleSignInOptions? = null private var mGoogleSignClient: GoogleSignInClient? = null companion object { const val requestCode = 10 } init { gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestEmail() .requestId() .requestIdToken(activity.getString(R.string.google_server_client_id)) .requestProfile() .build() mGoogleSignClient = GoogleSignIn.getClient(activity, gso!!) } /** * 登陸 */ fun login() { var intent = mGoogleSignClient?.signInIntent activity.startActivityForResult(intent, requestCode) } /** * 退出登陸 */ fun signOut() { mGoogleSignClient?.signOut() ?.addOnCompleteListener { googleSignListener?.googleLogoutSuccess() } ?.addOnFailureListener { googleSignListener?.googleLogoutFail() } } /** * 是否 登陸過 */ fun hasLogin(context: Context): Boolean { var accout = GoogleSignIn.getLastSignedInAccount(context) if (accout == null) { //是新用戶,沒有登陸過 return false } else { //老用戶,已經登陸過 //獲取用戶信息 var displayName = accout?.displayName var email = accout?.email val id = accout?.id val photoUrl = accout?.photoUrl val familyName = accout?.familyName val givenName = accout?.givenName return true } } fun handleSignInResult(completedTask: Task<GoogleSignInAccount>) { try { var accout = completedTask.getResult(ApiException::class.java) //登陸成功 var displayName = accout?.displayName var email = accout?.email val id = accout?.id val photoUrl = accout?.photoUrl val familyName = accout?.familyName val givenName = accout?.givenName LogUtils.d("google-displayName", displayName) LogUtils.d("google-email", email) LogUtils.d("google-photoUrl", photoUrl?.encodedPath) LogUtils.d("google-photoUrl", photoUrl?.toString()) LogUtils.d("google-familyName", familyName) LogUtils.d("google-givenName", givenName) LogUtils.d("google-id", id) googleSignListener?.googleLoginSuccess() } catch (e: ApiException) { googleSignListener?.googleLoginFail() } } interface GoogleSignListener { fun googleLoginSuccess() fun googleLoginFail() fun googleLogoutSuccess() fun googleLogoutFail() } }

Activity 使用

package com.enjoy.literaturemodule.account

import android.content.Intent import android.os.Bundle import android.util.Log import androidx.fragment.app.FragmentActivity import com.enjoy.literaturemodule.account.login.google.GoogleLoginUtil import com.google.android.gms.auth.api.signin.GoogleSignIn import kotlinx.android.synthetic.main.activity_login.* /** * 登陸 */ class LoginActivity : FragmentActivity(), GoogleLoginUtil.GoogleSignListener { private var googleLoginUtil: GoogleLoginUtil? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) googleLoginUtil = GoogleLoginUtil(this, this) //登陸  google_signIn_bt.setOnClickListener { googleLoginUtil?.login() } } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { //google登陸 if (requestCode == GoogleLoginUtil.requestCode) { var googleResultTask = GoogleSignIn.getSignedInAccountFromIntent(data) googleLoginUtil?.handleSignInResult(googleResultTask) } super.onActivityResult(requestCode, resultCode, data) } override fun googleLoginSuccess() { Log.e("google", "googleLoginSuccess") } override fun googleLoginFail() { Log.e("google", "googleLoginFail") } override fun googleLogoutSuccess() { Log.e("google", "googleLogoutSuccess") } override fun googleLogoutFail() { Log.e("google", "googleLogoutFail") } }

在做的時候,發現一直登陸不成功 StatusCode = 12500折騰了很久,才弄好,有兩點需要注意:

注意事項1:

一旦添加了 google_server_client_id , 那么 客戶端 google-services.json 一定要更新。

注意事項2:

項目集成 Google 登陸,一定要在 Firebase 后台開啟 Google 登陸功能,一定、一定、一定。

開啟之前的狀態  

 開啟之后的狀態:

 


 

補充:(2016/8/9)

    有很多人留言或者發私信說一直登陸失敗。然后我就自己寫了一個demo,核心代碼完全復制博客里面的 GoogleLogin (代碼見第六個標題, 6、Google SDK 二次封裝) 里面的代碼,真的是一行代碼都沒有改,完完全全復制的。最后實現登陸成功,可見博客里面的代碼是沒有問題的。我在操作過程中,有幾個關鍵的地方提一下。

1、權限問題:只需要添加聯網權限即可   

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

 2、簽名問題

      在開發者后台創建app的時候,需要添加 SHA-1的值,這個值是從你的簽名里面獲取的。注意,在你運行demo的時候,如果直接運行是不行的,因為直接運行,android studio 會為你的app使用默認簽名,這個簽名是系統自帶的,這個簽名的SHA-1值肯定和你真正的簽名文件SHA-1值不一樣,所以必然會登錄失敗。 你需要修改你的app debug 時的簽名和 發布時的簽名一致就好了。 

 


正文: 

谷歌登錄API:  https://developers.google.com/identity/sign-in/android/

1、注冊並且登錄google網站

       https://accounts.google.com/

2、進入google開發者平台創建應用

     https://developers.google.com/mobile/add?platform=android&cntapi=signin&cntapp=Default%20Demo%20App&cntpkg=com.google.samples.quickstart.signin&cnturl=https:%2F%2Fdevelopers.google.com%2Fidentity%2Fsign-in%2Fandroid%2Fstart%3Fconfigured%3Dtrue&cntlbl=Continue%20with%20Try%20Sign-In

3、在開發者平台填寫響應的信息,包括 應用名、包名、簽名的SHA-1值

     圖1

     

      圖2

        

      圖3

        

 

4、在項目中添加谷歌服務

       4.1、在SDK Manager 里面下載 google service

                

        4.2、在project目錄下的build.gradle下添加

                 classpath 'com.google.gms:google-services:2.1.0-alpha4'

              查看最新版本號:https://jcenter.bintray.com/com/google/gms/google-services/

        4.3  在model目錄下的build.gradle下添加

               compile 'com.google.android.gms:play-services-auth:8.4.0'

 5、代碼實現

         5.1、在布局文件中            

 1  <com.google.android.gms.common.SignInButton
 2             android:id="@+id/google_signIn_bt"
 3             android:layout_width="wrap_content"
 4             android:layout_height="wrap_content" />
 5 
 6         <Button
 7             android:id="@+id/google_loginOut_bt"
 8             android:layout_width="match_parent"
 9             android:layout_height="wrap_content"
10             android:text="google退出登錄"
11             android:layout_gravity="center"
12             android:gravity="center"
13             >
14         </Button>

        5.2、java代碼

  1 package com.pegasus.map.presentation.ui.activity;
  2 
  3 import android.content.Intent;
  4 import android.os.Bundle;
  5 import android.view.View;
  6 import android.widget.Button;
  7 
  8 import com.google.android.gms.auth.api.Auth;
  9 import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
 10 import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
 11 import com.google.android.gms.auth.api.signin.GoogleSignInResult;
 12 import com.google.android.gms.common.ConnectionResult;
 13 import com.google.android.gms.common.SignInButton;
 14 import com.google.android.gms.common.api.GoogleApiClient;
 15 import com.google.android.gms.common.api.ResultCallback;
 16 import com.google.android.gms.common.api.Status;
 17 import com.pegasus.map.R;
 18 import com.pegasus.map.presentation.ui.base.BaseActivity;
 19 
 20 import butterknife.Bind;
 21 import butterknife.ButterKnife;
 22 
 23 /**
 24  * Created by ${zyj} on 2016/3/24.
 25  * 登錄
 26  */
 27 
 28 public class LoginActivity1 extends BaseActivity implements GoogleApiClient.OnConnectionFailedListener , View.OnClickListener  {
 29 
 30     public int RequestCode = 10 ;
 31 
 32     @Bind( R.id.google_signIn_bt )
 33     public SignInButton google_signIn_bt ;
 34 
 35     @Bind( R.id.google_loginOut_bt )
 36     public Button google_loginOut_bt ;
 37 
 38     public GoogleSignInOptions gso ;
 39     public GoogleApiClient mGoogleApiClient ;
 40 
 41     @Override
 42     protected void onCreate(Bundle savedInstanceState) {
 43         super.onCreate(savedInstanceState);
 44         setContentView(R.layout.activity_login);
 45         ButterKnife.bind(this);
 46         init();
 47     }
 48 
 49 
 50     private void init() {
 51         //初始化谷歌登錄服務
 52         gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
 53                 .requestEmail()   //獲取郵箱
 54                 .requestId()      //獲取id 號
 55                 .requestIdToken("456212545785")  //獲取token
 56                 .build();
 57 
 58         // Build a GoogleApiClient with access to GoogleSignIn.API and the options above.
 59         mGoogleApiClient = new GoogleApiClient.Builder( this )
 60                 .enableAutoManage( this , this )
 61                 .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
 62                 .build();
 63 
 64         //登錄
 65         google_signIn_bt.setSize(SignInButton.SIZE_STANDARD);
 66         google_signIn_bt.setScopes(gso.getScopeArray());
 67         google_signIn_bt.setOnClickListener(this) ;
 68 
 69         //退出
 70         google_loginOut_bt.setOnClickListener(this) ;
 71 
 72     }
 73 
 74     @Override
 75     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 76         super.onActivityResult(requestCode, resultCode, data);
 77 
 78         //谷歌登錄成功回調
 79         if ( requestCode == RequestCode ) {
 80             GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
 81             handleSignInResult( result ) ;
 82         }
 83     }
 84 
 85     @Override
 86     public void onConnectionFailed(ConnectionResult connectionResult) {
 87 
 88     }
 89 
 90     @Override
 91     public void onClick(View v) {
 92         switch ( v.getId() ){
 93             case R.id.google_signIn_bt :   //登錄
 94                 signIn();
 95                 break;
 96 
 97             case R.id.google_loginOut_bt :
 98                 signOut();
 99                 break;
100         }
101     }
102 
103     /**
104      * 登錄
105      */
106     private void signIn() {
107         Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
108         startActivityForResult(signInIntent, RequestCode );
109     }
110 
111     /**
112      * 退出
113      */
114     private void signOut() {
115         Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
116                 new ResultCallback<Status>() {
117                     @Override
118                     public void onResult(Status status) {
119                         // ...
120                     }
121                 });
122     }
123 
124     private void handleSignInResult(GoogleSignInResult result) {
125         if (result.isSuccess()) {
126             // Signed in successfully, show authenticated UI.
127             GoogleSignInAccount acct = result.getSignInAccount();
128             //獲取用戶名
129             String name = acct.getDisplayName() ;
130             String email = acct.getEmail() ;
131             String token = acct.getIdToken() ;
132             String id = acct.getId() ;
133 
134         } else {
135             // Signed out, show unauthenticated UI.
136         }
137     }
138 
139 }

 

 6、Google SDK 二次封裝

         

  1 package com.pegasus.map.presentation.utils;
  2 
  3 import android.content.Intent;
  4 import android.support.v4.app.FragmentActivity;
  5 import android.util.Log;
  6 import android.widget.Toast;
  7 
  8 import com.google.android.gms.auth.api.Auth;
  9 import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
 10 import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
 11 import com.google.android.gms.auth.api.signin.GoogleSignInResult;
 12 import com.google.android.gms.common.api.GoogleApiClient;
 13 import com.google.android.gms.common.api.ResultCallback;
 14 import com.google.android.gms.common.api.Status;
 15 import com.pegasus.map.R;
 16 
 17 /**
 18  * Created by ${zyj} on 2016/3/30.
 19  */
 20 public class GoogleLogin {
 21 
 22     public int requestCode = 10 ;
 23     private FragmentActivity activity ;
 24     public GoogleSignInOptions gso ;
 25     public GoogleApiClient mGoogleApiClient ;
 26     public GoogleApiClient.OnConnectionFailedListener listener ;
 27     private GoogleSignListener googleSignListener ;
 28 
 29     public GoogleLogin(FragmentActivity activity , GoogleApiClient.OnConnectionFailedListener listener ){
 30         this.activity = activity ;
 31         this.listener = listener ;
 32 
 33         //初始化谷歌登錄服務
 34         gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
 35                 .requestEmail()
 36                 .requestId()
 37                 .requestIdToken( activity.getString(R.string.google_server_client_id))
 38                 .requestProfile()
 39                 .build();
 40 
 41         // Build a GoogleApiClient with access to GoogleSignIn.API and the options above.
 42         mGoogleApiClient = new GoogleApiClient.Builder( activity )
 43                 .enableAutoManage( activity , listener )
 44                 .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
 45                 .build();
 46     }
 47 
 48 
 49     /**
 50      * 登錄
 51      */
 52     public void signIn() {
 53         Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
 54         activity.startActivityForResult(signInIntent, requestCode);
 55     }
 56 
 57     /**
 58      * 退出登錄
 59      */
 60     public void signOut() {
 61         Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
 62                 new ResultCallback<Status>() {
 63                     @Override
 64                     public void onResult(Status status) {
 65                         if ( status.isSuccess() ){
 66                             if ( googleSignListener != null ){
 67                                 googleSignListener.googleLogoutSuccess();
 68                             }
 69                         }else {
 70                             if ( googleSignListener!= null ){
 71                                 googleSignListener.googleLogoutFail();
 72                             }
 73                         }
 74                     }
 75                 });
 76     }
 77 
 78     public String handleSignInResult(GoogleSignInResult result) {
 79         String res = "" ;
 80         if (result.isSuccess()) {
 81             //登錄成功
 82             GoogleSignInAccount acct = result.getSignInAccount();
 83             res = "登錄成功"
 84                     + "用戶名為:" + acct.getDisplayName()
 85                     + "  郵箱為:" + acct.getEmail()
 86                     + " token為:" + acct.getIdToken()
 87                     + " 頭像地址為:" + acct.getPhotoUrl()
 88                     + " Id為:" + acct.getId()
 89                     + " GrantedScopes為:" + acct.getGrantedScopes() ;
 90             Log.e("res", "res:"+res);
 91             Toast.makeText( activity, res, Toast.LENGTH_SHORT).show();
 92             if ( googleSignListener != null ){
 93                 googleSignListener.googleLoginSuccess();
 94             }
 95         } else {
 96             // Signed out, show unauthenticated UI.
 97             res = "-1" ;  //-1代表用戶退出登錄了 , 可以自定義
 98             Toast.makeText( activity , "退出登錄", Toast.LENGTH_SHORT).show();
 99             if ( googleSignListener != null ){
100                 googleSignListener.googleLoginFail();
101             }
102         }
103         return res ;
104     }
105 
106 
107     public void setGoogleSignListener( GoogleSignListener googleSignListener ){
108         this.googleSignListener = googleSignListener ;
109     }
110 
111     public interface GoogleSignListener {
112         void googleLoginSuccess();
113         void googleLoginFail() ;
114         void googleLogoutSuccess();
115         void googleLogoutFail() ;
116     }
117 
118 }

   注意:當你把 GoogleLogin 類復制到你項目中的時候,activity.getString(R.string.google_server_client_id)  這一句會報錯,可以先不用管,在6.5 部分會做說明

 

  6.1  在activity里使用封裝類

   activity 實現   GoogleApiClient.OnConnectionFailedListener , GoogleLogin.GoogleSignListener  兩個接口

 6.2

 //初始化谷歌登錄服務
 GoogleLogin googleLogin = new GoogleLogin( this , this ) ;
 googleLogin.setGoogleSignListener(this);

 6.3

 1     @Override
 2     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 3         super.onActivityResult(requestCode, resultCode, data);
 4         
 5         //谷歌登錄成功回調
 6         if (requestCode == googleLogin.requestCode ) {
 7             GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
 8             googleLogin.handleSignInResult( result ) ;
 9         }
10     }

6.4

1 //登錄
2 googleLogin.signIn();
3                 
4  //退出
5 googleLogin.signOut();

 6.5 獲取 google_server_client_id , 並在strings 里面配置

       https://console.developers.google.com/projectselector/apis/credentials

       在上面的網站里選擇

         

        

         點擊選擇項目,然后選擇你創建的app,進入下面一個界面,得到對應的Client ID

       

           


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM