[Android] Google Account Login(구글 계정 로그인 Api) 사용법
1. 구글 계정 로그인 Api?
- OAuth 방식
2019/10/07 - [Dev/Etc] - [생활코딩] OAuth 2.0
- OAuth를 통해 사용자User(Resource Owner)로부터 허가를 받고 얻어낸 Access Token을 이용하여, Resource Server에서 얻어온 사용자 ID를 통해 사용자를 인증한다.
- 사용자의 허가를 받은 Access Token을 이용한 ID이므로, PW가 없어도 해당 사용자임이 증명되며 로그인 가능하다.
2. Android Studio 환경설정
(참고: https://developers.google.com/identity/sign-in/android/start-integrating)
1) Project단 build.gradle에 google()추가
allprojects {
repositories {
google()
// If you're using a version of Gradle lower than 4.1, you must instead use:
// maven {
// url 'https://maven.google.com'
// }
}
}
2) Module단 build.gradle에
'com.google.android.gms:play-services-auth:17.0.0' 추가
(version 참고: https://developers.google.com/android/guides/setup )
apply plugin: 'com.android.application'
...
dependencies {
implementation 'com.google.android.gms:play-services-auth:17.0.0'
}
3) Configure a Google API Console project
To configure a Google API Console project, click the button below, and specify your app's package name when prompted. You will also need to provide the SHA-1 hash of your signing certificate. See Authenticating Your Client for information.
4) Activity 코드
Configure a Google API Console project and set up your Android Studio project.
(※코드 추가 전에 구글 Api 콘솔 프로젝트를 설정하고, 안드로이드 프로젝트를 셋업할 것)
// 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();
sign-in activity의 onCreate에 본인이 원하는 옵션을 정의한 GoogleSignInClient 객체를 생성.
// Build a GoogleSignInClient with the options specified by gso.
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
sign-in activity의 onStart에 사용자가 구글로부터 당신의 앱에 이미 sign in했는지를 체크하도록 추가.
// Check for existing Google Sign In account, if the user is already signed in
// the GoogleSignInAccount will be non-null.
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
updateUI(account);
추가 참고
https://github.com/googlesamples/google-services
Reference
https://github.com/googlesamples/google-services
https://developers.google.com/identity/sign-in/android
https://developers.google.com/android/guides/setup
https://galid1.tistory.com/109