# Google Credential in Android
###### tags: `Android` `Google Credential`
[TOC]
## How to integrate Google Sign-In?
**Google Site Steps:**
URL: https://developers.google.com/identity/sign-in/android/start-integrating#next_steps
>**Note:**
Remember to always set Internet permission in Manifest![color=#187fce]
``` java==
<uses-permission android:name="android.permission.INTERNET" />
```
### 1. Build Gradle (Project)
``` java==
allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}
```
### 2. Build Gradle (App)
``` java==
dependencies {
//Google Credential
compile 'com.google.android.gms:play-services-auth:16.0.1'
}
```
### 3. Configure a Google API Console project
**Step 1:**

**Step 2:**

:::info
**SHA-1 signing certificate:**
1. Go to Gradle -> Android -> signing Report

2. Double Click *++Signing Report++* and you will get SHA1 in your **"Run Window"**.

:::
**Step 3:**
Download your client configuration(json file) and place it in your app folder.

### 4. Add Google Sign-In Button to XML
You can decide whether you want to use Google Default Button or other. It will not affect the google sign-in process.
Default Button Preview:

Default Button XML:
``` java==
<com.google.android.gms.common.SignInButton
android:id="@+id/sign_in_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
```
### 5. Set up GoogleSignInOptions and GoogleSignInClient
**GoogleSignInOptions:**
Configures Google Sign-In to request users' ID and basic profile information.
``` java==
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
this.mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
//Check whether google account is signed or not
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
if(account != null){
//Get information
setUpGLUser(account);
logInSuccessful();
}
```
#### Check for an existing signed-in user:
Use the client made in onCreate to check whether there is an account already logged in.
>Note:
>GL_RC in this case is a final int I made to use as requestCode.[color=#55a6e8]
``` java==
public void glSignIn(){
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, GL_RC);
}
```
### 6. Handle Sign-In Result
We may be able to log in successfully but also fail. Therefore, we need to handle the possible error by using following code.
``` java==
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
//Handles the error if Google Sign in was not successful;
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
setUpGLUser(account);
// Signed in successfully
// Decide what to do next
logInSuccessful();
} catch (ApiException e) {
Toast.makeText(LoginActivity.this, "GOOGLE SIGN IN FAILED", Toast.LENGTH_SHORT).show();
}
}
```
### 7. Get User's information
We use GoogleSignInAccount, we obtained from ++*handleSignInResult*++ to get the information we need.
>Note:
>Some information like photoUrl may not be available if the user did not set any.[color=#55a6e8]
``` java==
private void setUpGLUser(GoogleSignInAccount account){
String img_url = "NULL";
if(account.getPhotoUrl() != null){
img_url = account.getPhotoUrl().toString();
}
userInfo.userProfile(account.getId(),account.getDisplayName(),img_url, account.getEmail());
}
```
## Google Log in Buttons
1. Default Button
2. Large rectangular round button
3. Rounded Buttons
Remember no matter which type you make, you must setOnClickListener to it.
### Large rectangular rounded corners button:
Preview:

Code:
``` java==
<FrameLayout
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_marginTop="5dp"
android:layout_gravity="center">
<Button
android:id="@+id/gl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/button_rounded_gl"
android:drawableStart="@drawable/gl_logo_25px"
android:drawableLeft="@drawable/gl_logo_25px"
android:paddingEnd="20dp"
android:paddingStart="20dp"
android:text="@string/gl"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="14sp"
android:textStyle="bold">
</Button>
</FrameLayout>
```
### Rounded Button:

>Note:
>You can make it with or without FrameLayout. It is all up to you.[color=#55a6e8]
``` java==
<FrameLayout
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center">
<ImageButton
android:id="@+id/gl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/button_rounded_fb"
android:src="@drawable/gl_logo_25px"
android:padding="8dp"
android:scaleType="centerInside"/>
</FrameLayout>
```
## Google Account Sign-Out
``` java==
private void signOut() {
mGoogleSignInClient.signOut().addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
// Code you want to do when sign-out
//Ex: Finish activity or intent, etc...
finish();
}
});
}
```
## Google Account Revoke from App
Users will have the ability to remove their information from app.
``` java==
private void revokeAccess() {
mGoogleSignInClient.revokeAccess()
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
// ...
}
});
}
```