We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All

Google Play Games

Last updated: 2023-04-28 11:32
  • Unusable
  • Poor
  • OK
  • Good
  • Excellent
Last updated: 2023-04-28 11:32

You can integrate Google Play Games account sign-in into your app so that your users can be identified by AppGallery Connect using their Google Play Games accounts.

NOTE
  • Google Play Games sign-in is supported only when your data processing location is Germany, Singapore, or Russia.
  • If your app allows users to sign in using Google Play Games accounts, GMS must be available on user devices.

Before You Start

Development Process

Auth Service allows you to integrate Google Play Games account sign-in in either of the following methods:

Unified Sign-in

  1. Add the SDK dependency.
    • Android Studio

      Open the app-level build.gradle file (usually in the app directory) and add dependencies on Huawei's Auth Service SDK and Google Play Games' SDK to the dependencies block.

      1. dependencies {
      2. implementation "com.huawei.agconnect:agconnect-auth-googlegame:1.9.0.300"
      3. implementation "com.google.android.gms:play-services-auth:{version}"
      4. }
    • Eclipse
      Open the build.gradle file in the aar2eclipse/aar directory and add dependencies on Huawei's Auth Service SDK and Google Play Games' SDK to the dependencies block. The procedure is similar to that for integrating the AppGallery Connect SDK in Eclipse.
      1. dependencies {
      2. embed "com.huawei.agconnect:agconnect-auth-googlegame:1.9.0.300"
      3. embed "com.google.android.gms:play-services-auth:{version}"
      4. }
    NOTE
    • Replace {version} in the sample code with the latest version number of Google Play Games's SDK. Huawei is not responsible for the update.
    • To add Google Play Games's SDK dependency to your project, download the latest SDK dependency from the Google Play Games Open Platform and integrate it into your project.
  2. Set parameters required for users to sign in with a Google Play Games account.
    1. Set google_app_id and google_client_id in the /app/res/values/string.xml file.
      NOTE

      Set google_client_id to the client ID that you have configured when enabling the Google authentication mode. The number (typically 12 or more digits) at the beginning of the client ID is the value of google_app_id.

      1. <string name="google_app_id">5758********</string>
      2. <string name="google_client_id">5758********-lp1cdv4uu2k1jjm1ksi5n59tta0mdkg9.apps.googleusercontent.com</string>
    2. Configure the metadata information in the application element in the AndroidManifest.xml file.
      1. <meta-data
      2. android:name="com.google.android.gms.games.APP_ID"
      3. android:value="@string/google_app_id" />
      4. <meta-data
      5. android:name="com.google.android.gms.version"
      6. android:value="@integer/google_play_services_version" />
  3. Add the lifecycle callback.
  4. Call AGConnectAuth.signIn for a user to sign in with a Google Play Games account.
    Java
    Kotlin
    1. AGConnectAuth.getInstance().signIn(this,AGConnectAuthCredential.GoogleGame_Provider).addOnSuccessListener(new OnSuccessListener<SignInResult>() {
    2. @Override
    3. public void onSuccess(SignInResult signInResult) {
    4. // onSuccess
    5. AGConnectUser user = signInResult.getUser();
    6. }
    7. })
    8. .addOnFailureListener(new OnFailureListener() {
    9. @Override
    10. public void onFailure(Exception e) {
    11. // onFail
    12. }
    13. });
    1. AGConnectAuth.getInstance().signIn(this, AGConnectAuthCredential.GoogleGame_Provider).addOnSuccessListener {
    2. // updateUI
    3. }.addOnFailureListener {
    4. // onFailure
    5. }

Traditional Sign-in

  1. Initialize the AGConnectAuth instance on the app sign-in page, obtain user information from AppGallery Connect, and check whether a user is currently signed in. If so, display the user's home page. If not, display the sign-in page.
    Java
    Kotlin
    1. AGConnectUser user = AGConnectAuth.getInstance().getCurrentUser();
    1. val user = AGConnectAuth.getInstance().currentUser
  2. After a user requests to sign in using a Google Play Games account, obtain the user's authorization.
    NOTE

    This step is for reference only. For details, please refer to Google Play Games documentation.

    1. Obtain the Google Play Games sign-in authorization.
      Java
      1. GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN)
      2. .requestServerAuthCode(CLIENT_ID)
      3. .build();
      4. client = GoogleSignIn.getClient(this, gso);
      5. Intent signInIntent = client.getSignInIntent();
      6. startActivityForResult(signInIntent, RC_SIGN_IN);
    1. Add a callback to obtain the server authentication code upon successful authorization.
      Java
      1. @Override
      2. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      3. super.onActivityResult(requestCode, resultCode, data);
      4. if (requestCode == RC_SIGN_IN) {
      5. Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
      6. task.addOnSuccessListener(new OnSuccessListener<GoogleSignInAccount>(){
      7. @Override
      8. public void onSuccess(GoogleSignInAccount googleSignInAccount) {
      9. String serverAuthCode = googleSignInAccount.getServerAuthCode();
      10. }
      11. }).addOnFailureListener(new OnFailureListener(){
      12. @Override
      13. public void onFailure(@NonNull Exception e) {
      14. }
      15. });
      16. }
      17. }
  3. Call GoogleGameAuthProvider.credentialWithToken to generate a credential using the obtained server authentication code, and then call AGConnectAuth.signIn to implement sign-in.
    Java
    Kotlin
    1. AGConnectAuthCredential credential = GoogleGameAuthProvider.credentialWithToken(serverAuthCode);
    2. AGConnectAuth.getInstance().signIn(credential)
    3. .addOnSuccessListener(new OnSuccessListener<SignInResult>() {
    4. @Override
    5. public void onSuccess(SignInResult signInResult) {
    6. // onSuccess
    7. AGConnectUser user = signInResult.getUser();
    8. }
    9. })
    10. .addOnFailureListener(new OnFailureListener() {
    11. @Override
    12. public void onFailure(Exception e) {
    13. // onFail
    14. }
    15. });
    1. val credential = GoogleGameAuthProvider.credentialWithToken(serverAuthCode)
    2. AGConnectAuth.getInstance().signIn(credential).addOnSuccessListener {
    3. // onSuccess
    4. val user = it.user
    5. }.addOnFailureListener {
    6. // onFail
    7. }

More You May Need to Know

  • If you want to allow a user to use multiple accounts to sign in to your app, you can link these accounts.
  • If a user wants to exit the current account or switch to another account, you need to sign this user out.
  • You can deregister an account as required.
  • Users can perform sensitive operations, such as deregistration, password change, account linking, and mobile number and email address resetting, only within 5 minutes after sign-in. If a user signed in 5 minutes before and tries to perform such operations, you need to reauthenticate the account first.
  • You can implement your own exception handling logic to provide a better experience for users.
  • You can use Cloud Functions triggers to receive key events, such as user registration, sign-in, and deregistration, to extend the capabilities of Auth Service.

Was this page helpful?

  • Unusable
  • Poor
  • OK
  • Good
  • Excellent
Send feedback
You can go to the community forum for more help. If you need other help, use Intelligent Assistant.
Search Guides
  • All
  • Auth Service
Search results: 0
  • Intelligent Assistant

    Chat with our virtual assistant to get answers promptly.

  • Quick start

    Helps you find desired resources with ease.