Huawei Auth Service usage with Google Account — Method 2
Hello everyone,
In this article, we will talk about HMS Auth service usage with Google Account — Method 2.
HMS Auth Service: Most apps need to identify and authenticate users. You can fulfill these requirements quickly and securely using HMS Auth Service.
You can choose to provide your users with one or more of the authentication modes. In this article, we use Google Account — Method 2.
Note: Google Authentication mode has a GMS dependency. You cannot use it on HMS phones. We used to be able to do this using webviews, but now this usage has been removed due to some security problems. If you wonder details, you can check this post.
To use Huawei Auth Service, firstly you must finish HMS Core integration part. You can use the document in the link to easily integrate HMS Core.
Configurations
We need to enable Auth Service on AGC Console. You can find Auth Service in Build part. Click Enable now button to enable Auth Service.
After enabling Auth Service, we need to enable authentication mode we will use.
When Google Authentication mode is enabled, the page in the screenshot below will appear. Using the Google API Console, we should create Client ID and Client Secret but you can create Client ID and Client Secret with using Firebase project. We are using Firebase in this article project.
Note: The ‘com.google.android.gms.common.api.ApiException: 10’ displays, when I did it using Google API Console. I also found the Firebase solution and used it to solve this.
Now, please create a Firebase project and go to project settings. We need to add our app in this project.
Then, on the add app page, we just need to fill in the package name of our app. In the other steps, what we do through the AGC Console is enough. For example we do not need to use Firebase config file.
Now, let’s add the fingerprint information required for the app from the project settings section. Actually, adding one of SHA-1 or SHA-256 is enough here, but let’s add both fingerprints for avoiding errors.
We can easily access SHA-1 and SHA-256 information via Android Studio. You can access the fingerprint information of your project by clicking the signingReport section from the Gradle section on the right.
Then we enable Google from the Authentication sign-in method section.
After Google is enabled, we copy the Client ID and Client Secret parts in the screenshot and add them to the AGC console.
We have completed our configuration steps on the console, we can move on to the code part.
Coding
First, we have to add the implementation part for HMS Auth Service and Auth Service-Google Account.
After that, we need to add Google Sign-In button. As with the Huawei Sign-In button, Google Sign-In button has a design feature that we need to pay attention. In this project, we can use this code for button.
When the user clicks this button we will call the signInGoogle method. We need to add our client id value to the requestIdToken. We have just obtained the client id value from Firebase and added it to the AGC Console.
If we use more than one sign-in method in our application, we must call the required sign-in method here with requestCode.
After completing the coding part, we can test our application. In the screenshots below, you can see the screens that you need to accept to use your Google account for sign-in after clicking the Google Sign-in button.
Output
Tips & Tricks
- Please do not forget to add fingerprint information in Firebase Project.
- Please do not forget to use Web Client ID and Web Client Secret from Firebase.
- Please do not forget to add Client ID information in code. You can see the code line you need to add in the signInGoogle.kt in the above Gist.
Conclusion
In this article, I explained Google Account usage for HMS Auth Service. I hope you will like it. Thank you for reading. If you have any questions, you can leave a comment or ask via Huawei Developer Forum.