Though already many upvoted answers exist in this question, I struggled to understand the logic.
So, I come up with my research.
To get SHA-1, run this in terminal:
keytool -exportcert -keystore path-to-debug-or-production-keystore -list -v
About OAuth 2.0 client IDs
- OAuth for the web (In app this is used as
server_client_id
)
- OAuth for android (This needs to be created using correct package name & Signing-certificate fingerprint SHA-1).
If you are using the different keystore for debug & release, you need to create separate OAuth 2.0 client IDs
using respective package name & SHA-1.
You can create or edit your OAuth 2.0 client IDs
here https://console.developers.google.com/apis/credentials?project=
- Navigating to your app.
- If you already have a OAuth for Android, click in its name & check the package name & SHA-1.
We can use the same keystore for both debug & release by saving the keystore details in global(local, not inside project) gradle.properties
& getting it in build.gradle
as below.
def key_alias = ""
def key_password = ""
def store_file = ""
def store_password = ""
try {
key_alias = YOUR_KEY_ALIAS
key_password = YOUR_KEY_PASSWORD
store_file = YOUR_KEYSTORE_PATH
store_password = YOUR_KEYSTORE_PASSWORD
} catch (Exception exception) {
throw new IllegalStateException('Failed to find key store details. Social Login is disabled');
}
android {
signingConfigs {
config {
keyAlias key_alias
keyPassword key_password
storeFile file(store_file)
storePassword store_password
}
}
buildTypes {
debug {
signingConfig signingConfigs.config
// ...
}
release {
signingConfig signingConfigs.config
// ...
}
}
}
You can use below snippet
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_GOOGLE_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
// ...
} else if (result.getStatus().getStatusCode() == CommonStatusCodes.NETWORK_ERROR) {
// No internet connection
} else if (result.getStatus().getStatusCode() == CommonStatusCodes.DEVELOPER_ERROR) {
// Wrong server OAuth key(server_client_id)
} else if (result.getStatus().getStatusCode() == 12501) {
// Wrong app signing-certificate fingerprint SHA1
} else {
Log.e("Google sign in error", result.getStatus().toString());
}
}
}
Note: If you enabled only Google Sign-In when you generated the configuration file, you need not to add the google-servies.json
file in your project.(generating the file performs the necessary configuration steps).
resultCode == Activity.RESULT_CANCELED
). Using GMS 8.4.0. – Eugen Pechanec