본문 바로가기
프로그래밍 놀이터/안드로이드, Java

[android] Google Sign In 코드 짜기 #2

by 돼지왕 왕돼지 2018. 2. 23.
반응형

[android] Google Sign In 코드 짜기 #2


https://developers.google.com/identity/sign-in/android/sign-in

activity, addApi, Auth.GOOGLE_SIGN_IN_API, DEFAULT_SIGN_IN, email 주소 변경 가능성, enableAutoManage, example code, getDisplayName, getEmail, getFamilyName, getGivenName, getId, getPhotoUrl, getSignInAccount, getSignInIntent, getSignInResultFromIntent, google sign in, GoogleApiClient, GoogleSignInApi, GoogleSignInOptions, GoogleSignInResult, init, initialize, isSuccess, onConnected, OnConnectionFailedListener, requestEmail, requestProfile, REVOKE, revokeAccess, setResultCallback, sign in, Sign out, signOut, SilentSignIn, status, [android] Google Sign In 코드 짜기 #2, 앱과의 연결 끊기



GoogleSignInApi ref doc.

https://developers.google.com/android/reference/com/google/android/gms/auth/api/signin/package-summary



-

sign-in activity 의 onCreate 에서 GoogleSignInOptions 과 GoogleApiClient 를 생성한다.

private void doSomething() {

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)

            .requestEmail()

            .requestProfile()

            .build();

    mGoogleApiClient = new GoogleApiClient.Builder(this)

            .enableAutoManage(this, this) // 첫번째 this 는 FragmentActivity, 두번째 this 는 OnConnectionFailedListener

            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)

            .build();

}


GoogleSignInOptions.Builder ref doc.

https://developers.google.com/android/reference/com/google/android/gms/auth/api/signin/GoogleSignInOptions.Builder



-

Sign in 은 아래 코드로 한다.

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);

    if (requestCode == RC_SIGN_IN) {

        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);

        // 정보 가져오기

    }

}



-

Sign out 은 아래 코드로 한다.

private void signOut() {

    Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(

            new ResultCallback<Status>() {

                @Override

                public void onResult(Status status) {

                    // TODO

                }

            });

}


SignOut 전에는 반드시 GoogleApiClient 의 onConnected 가 불렸어야 한다.



-

앱과의 연결 끊기 (revoke)는 아래 코드로 한다.

private void revokeAccess() {

    Auth.GoogleSignInApi.revokeAccess(mGoogleApiClient).setResultCallback(

            new ResultCallback<Status>() {

                @Override

                public void onResult(Status status) {

                    // [START_EXCLUDE]

                    updateUI(false);

                    // [END_EXCLUDE]

                }

            });

}


Revoke 전에는 반드시 GoogleApiClient 의 onConnected 가 불렸어야 한다.



-

GoogleSignInResult 로부터 정보를 가져오는 코드는 아래와 같다.

if (result.isSuccess()) {

    GoogleSignInAccount acct = result.getSignInAccount();

    String personName = acct.getDisplayName();

    String personGivenName = acct.getGivenName();

    String personFamilyName = acct.getFamilyName();

    String personEmail = acct.getEmail();

    String personId = acct.getId();

    Uri personPhoto = acct.getPhotoUrl();

}


여기서 주의할 것은 email 주소는 바뀔 수 있기 때문에 getId 로 얻어온 id 값을 써야 한다.



-

이미 앱에 Signin 이 되어 있다면, SilentSignIn 을 통해 UI 없이 바로 로그인 시킬 수 있다.


SilentSignIn ref doc.

https://developers.google.com/android/reference/com/google/android/gms/auth/api/signin/GoogleSignInApi#silentSignIn(com.google.android.gms.common.api.GoogleApiClient)





반응형

댓글