谷歌登錄API: https://developers.google.com/identity/sign-in/android/ 1、註冊並且登錄google網站 https://accounts.google.com/ 2、進入google開發者平臺創建應用 https://developers. ...
谷歌登錄API: https://developers.google.com/identity/sign-in/android/
1、註冊並且登錄google網站
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 }