เริ่มลิงก์บัญชี Google โดยตรงจากแพลตฟอร์ม

การลิงก์บัญชีจะเสร็จสิ้นในแอปบนอุปกรณ์เคลื่อนที่ได้โดยตรง ซึ่งช่วยให้ผู้ใช้ลิงก์บัญชีของคุณกับบริการกับบัญชี Google ของตนได้ ลิงก์ที่สร้างขึ้นให้สิทธิ์แก่ Google ในการเข้าถึงข้อมูลที่ผู้ใช้ยินยอมที่จะแชร์

แนวทางนี้ช่วยปรับปรุงประสิทธิภาพการลิงก์บัญชีด้วยการดึงดูดผู้ใช้ภายในบริบทที่คุ้นเคยของแอปแทนการสนทนากับ Assistant โดยสามารถผสานรวมเข้ากับการเริ่มต้นใช้งานของผู้ใช้ การตั้งค่า และแพลตฟอร์มอื่นๆ ของแอป ซึ่งจะสร้างโอกาสในการค้นพบและการมีส่วนร่วมกับ Google Assistant Action ของคุณ เช่น หลังจากลิงก์แล้ว คุณอาจเสนอที่จะนำผู้ใช้ไปยังแอ็กชันของคุณโดยตรง

ข้อดีสําหรับผู้ใช้มีดังนี้

  • ผู้ใช้จะเริ่มทําตามขั้นตอนการลิงก์บัญชีในแอปรวมถึงสภาพแวดล้อมที่ผู้ใช้คุ้นเคยอยู่แล้วได้
  • ผู้ใช้ไม่จําเป็นต้องมีข้อมูลเข้าสู่ระบบเนื่องจากผ่านการตรวจสอบสิทธิ์ในอุปกรณ์และในแอปบนอุปกรณ์เคลื่อนที่ของคุณแล้ว

สิทธิประโยชน์สําหรับนักพัฒนาซอฟต์แวร์มีดังนี้

  • ควบคุมตําแหน่งที่จะโปรโมตและเริ่มต้นการลิงก์บัญชีในแอปบนอุปกรณ์เคลื่อนที่ เช่น ในการตั้งค่าของผู้ใช้ โฆษณาคั่นระหว่างหน้า หรือหลังจากที่ผู้ใช้ลงชื่อเข้าใช้แอปบนอุปกรณ์เคลื่อนที่ การเพิ่มจุดเข้าหลายๆ จุดเพื่อเริ่มการลิงก์บัญชีจะช่วยให้ลิงก์บัญชีได้ง่ายขึ้น ส่งผลให้เกิดการมีส่วนร่วมเพิ่มขึ้นและจํานวนบัญชีที่ลิงก์
  • การเพิ่มขึ้นของอัตรา Conversion เนื่องจากผู้ใช้สามารถทําการลิงก์ให้เสร็จได้น้อยกว่าขั้นตอนOAuth แบบเว็บมาตรฐาน
  • ต้องใช้ความพยายามด้านวิศวกรรมเล็กน้อยในการนําลิงก์จากแพลตฟอร์ม (Android) ไปใช้ เนื่องจากขั้นตอนนี้ใช้ประโยชน์จากการใช้งาน OAuth2.0 ที่มีอยู่ หากคุณติดตั้งใช้งานแล้ว
  • ลดอัตราการออกจากเว็บไซต์เนื่องจากผู้ใช้ไม่จําเป็นต้องป้อนข้อมูลเข้าสู่ระบบอีกครั้ง และดําเนินการตามขั้นตอนให้เสร็จสมบูรณ์ได้ภายในขั้นตอนน้อยลง อัตราการออกจากแพลตฟอร์มอาจสูงถึง 80% ในกระบวนการที่ผู้ใช้ต้องเรียกคืนและป้อนข้อมูลเข้าสู่ระบบ

วิธีการทำงาน

การลิงก์จากแพลตฟอร์มของคุณจะเสร็จสมบูรณ์ในขั้นตอนต่อไปนี้

  1. ผู้ใช้จะคลิก / เปิดใช้ทริกเกอร์การลิงก์ในแอปบนอุปกรณ์เคลื่อนที่
  2. ผู้ใช้เลือกบัญชี Google ที่จะลิงก์
    1. ผู้ใช้เลือกบัญชี Google ที่มีอยู่ในอุปกรณ์เพื่อลิงก์ หรือลงชื่อเข้าใช้ด้วยบัญชีใหม่
  3. ระบบจะแสดงหน้าจอความยินยอมที่ Google โฮสต์ให้ผู้ใช้ และผู้ใช้ต้องยอมรับเพื่อดำเนินการต่อหรือยกเลิกเพื่อหยุดกระบวนการลิงก์
  4. ระบบจะแสดงหน้าจอขอความยินยอมให้ผู้ใช้ และผู้ใช้ต้องยอมรับเพื่อดำเนินการต่อหรือยกเลิกเพื่อหยุดกระบวนการลิงก์
  5. ระบบจะสร้างลิงก์ระหว่างบัญชีของผู้ใช้ในบริการของคุณกับบัญชี Google ของผู้ใช้

รูปที่ 1 ลิงก์จากโฟลว์ของแพลตฟอร์ม

ข้อกำหนด

หากต้องการใช้ลิงก์จากแพลตฟอร์ม คุณจะต้องมีสิ่งต่อไปนี้

ตั้งค่า

ก่อนดำเนินการตามขั้นตอนด้านล่าง คุณต้องลงทะเบียนการลิงก์บัญชีให้เสร็จสมบูรณ์

ตั้งค่าสภาพแวดล้อมในการพัฒนา

ดาวน์โหลดบริการ Google Play เวอร์ชันล่าสุดในโฮสต์การพัฒนา

  1. เปิด Android SDK Manager
  1. ในส่วน SDK Tools ให้ค้นหา Google Play services

  2. หากสถานะของแพ็กเกจเหล่านี้เป็น "ไม่ได้ติดตั้ง" ให้เลือกทั้ง 2 แพ็กเกจ แล้วคลิกติดตั้งแพ็กเกจ

กำหนดค่าแอป

  1. ในไฟล์ build.gradle ระดับโปรเจ็กต์ ให้รวมที่เก็บ Maven ของ Google ไว้ทั้งในส่วน buildscript และ allprojects

    buildscript {
        repositories {
            google()
        }
    }
    
    allprojects {
        repositories {
            google()
        }
    }
    
  2. เพิ่มทรัพยากร Dependency สำหรับ API "ลิงก์กับ Google" ลงในไฟล์ Gradle ระดับแอปของโมดูล ซึ่งโดยปกติคือ app/build.gradle

    dependencies {
      implementation 'com.google.android.gms:play-services-auth:21.4.0'
    }
    

ลิงก์จากโฟลว์แพลตฟอร์มจะส่งผลให้ Google บันทึกโทเค็นเพื่อการเข้าถึงที่บริการของคุณให้ไว้ ต้องได้รับความยินยอมก่อนจึงจะส่งคืนโทเค็นสำหรับผู้ใช้ได้

ทำตามขั้นตอนด้านล่างเพื่อขอความยินยอมจากผู้ใช้และส่งคืนโทเค็นรหัสการให้สิทธิ์ผ่าน SDK ของ Google Play Services

  1. สร้าง PendingIntent ที่เปิดใช้กิจกรรมความยินยอมได้ - Play Services API จะเปิดใช้ความยินยอม คุณจะต้องระบุ PendingIntent (ซึ่งจะเรียกว่า consentPendingIntent เพื่อความชัดเจน) เมื่อเรียกใช้ API

    Kotlin

    // Build a PendingIntent that can launch the consent activity
    val consentPendingIntent = buildConsentPendingIntent()
    

    Java

    // Build a PendingIntent that can launch your consent activity
    PendingIntent consentPendingIntent =
              buildConsentPendingIntent();
    
  2. สร้างกิจกรรมที่เกี่ยวข้องเพื่อจัดการ Intent ความยินยอม

    Kotlin

      class ConsentActivity : AppCompatActivity
    
      private fun onConsentAccepted() {
          // Obtain a token (for simplicity, we’ll ignore the async nature
          // of the following call)
          val token = getToken()
          val intent = Intent()
                      .putExtra(SaveAccountLinkingTokenRequest.EXTRA_TOKEN,
                                token)
          setResult(Activity.RESULT_OK, intent)
          finish()
      }
    
      private fun onConsentRejectedOrCanceled() {
          setResult(Activity.RESULT_CANCELED)
          finish()
      }
    

    Java

      public class ConsentActivity extends AppCompatActivity {
        ...
        private void onConsentAccepted() {
          // Obtain a token (for simplicity, we’ll ignore the async nature of
          // the following call
          String token = getToken();
          Intent intent = new Intent();
          intent.putExtra(SaveAccountLinkingTokenRequest.EXTRA_TOKEN, token);
          setResult(Activity.RESULT_OK, intent);
          finish();
        }
    
        private void onConsentRejectedOrCanceled() {
          setResult(Activity.RESULT_CANCELED, null);
          finish();
        }
     }
    
    

    เราถือว่ามีการเรียกใช้เมธอด onConsentAccpeted() และ onConsentRejectedOrCanceled() หากผู้ใช้ยอมรับหรือปฏิเสธ/ยกเลิกความยินยอมของคุณตามลำดับ

  3. สร้างคำขอเพื่อบันทึกโทเค็น และส่ง PendingIntent ที่สร้างในขั้นตอนที่ 1 ด้านบนพร้อมกับพารามิเตอร์การกำหนดค่าอื่นๆ

    Kotlin

      // Create an ActivityResultLauncher which registers a callback for the
      // Activity result contract
      val activityResultLauncher = registerForActivityResult(
        ActivityResultContracts.StartIntentSenderForResult())
        { result ->
          if (result.resultCode == RESULT_OK) {
            // Successfully finished the flow and saved the token
          } else {
            // Flow failed, for example the user may have canceled the flow
          }
        }
    
      // Build token save request
      val request = SaveAccountLinkingTokenRequest.builder()
        .setTokenType(SaveAccountLinkingTokenRequest.TOKEN_TYPE_AUTH_CODE)
        .setConsentPendingIntent(consentPendingIntent)
        .setServiceId("service-id-of-and-defined-by-developer")
        //Set the scopes that the token is valid for on your platform
        .setScopes(scopes)
        .build()
    
       // Launch consent activity and retrieve token
       Identity.getCredentialSavingClient(this)
         .saveAccountLinkingToken(request)
         .addOnSuccessListener( saveAccountLinkingTokenResult -> {
            if (saveAccountLinkingTokenResult.hasResolution()) {
              val pendingIntent = saveAccountLinkingTokenResult
                                  .getPendingIntent()
              val intentSenderRequest = IntentSenderRequest
                                        .Builder(pendingIntent).build()
              activityResultLauncher.launch(intentSenderRequest)
            } else {
               // This should not happen, let’s log this
               Log.e(TAG, "Failed to save token");
            }
          })
          .addOnFailureListener(e -> Log.e(TAG, Failed to save token, e))
    

    Java

      // Create an ActivityResultLauncher which registers a callback for the
      // Activity result contract
      ActivityResultLauncher<IntentSenderRequest>
          activityResultLauncher =
          registerForActivityResult(new ActivityResultContracts
                                        .StartIntentSenderForResult(),
                                    result -> {
          if (result.getResultCode() == RESULT_OK) {
              // Successfully finished the flow and saved the token
          } else {
              // Flow failed, for example the user may have canceled the flow
          }
      });
    
     // Build token save request
     SaveAccountLinkingTokenRequest request =
        SaveAccountLinkingTokenRequest.builder()
            .setTokenType(
                SaveAccountLinkingTokenRequest.TOKEN_TYPE_AUTH_CODE)
            .setConsentPendingIntent(consentPendingIntent)
            .setServiceId("service-id-of-and-defined-by-developer")
            //Set the scopes that the token is valid for on your platform
            .setScopes(scopes)
            .build();
    
      // Launch consent activity and retrieve token
      Identity.getCredentialSavingClient(this)
          .saveAccountLinkingToken(request)
          .addOnSuccessListener(
              saveAccountLinkingTokenResult -> {
                if (saveAccountLinkingTokenResult.hasResolution()) {
                  // Launch the resolution intent
                  PendingIntent pendingIntent =
                      saveAccountLinkingTokenResult.getPendingIntent();
                  IntentSenderRequest intentSenderRequest =
                      new IntentSenderRequest.Builder(pendingIntent).build();
                  activityResultLauncher.launch(intentSenderRequest);
                } else {
                  // This should not happen, let’s log this
                  Log.e(TAG, "Failed to save token");
                }
              })
          .addOnFailureListener(e -> Log.e(TAG, "Failed to save token", e));
      ```
    

ขั้นตอนข้างต้นจะแจ้งให้ผู้ใช้ขอความยินยอมและส่งคืนรหัสการให้สิทธิ์ไปยัง Google

แนวทางปฏิบัติแนะนำ

  • แอปของคุณควรระบุสถานะลิงก์ให้ผู้ใช้ทราบผ่านปุ่ม สวิตช์ หรือองค์ประกอบภาพที่คล้ายกัน

    รูปที่ 1 รูปภาพสถานะลิงก์ตัวอย่าง

  • คุณควรแจ้งให้ผู้ใช้ทราบหลังจากลิงก์สำเร็จ เช่น แสดงข้อความป๊อปอัป ทริกเกอร์การเปลี่ยนสถานะเปิด/ปิด หรือเปลี่ยนเส้นทางผู้ใช้ไปยังหน้าลิงก์สำเร็จที่แยกต่างหาก

  • คุณควรพิจารณาแจ้งให้ผู้ใช้ในแอปลิงก์บัญชี โดยอิงตามสัญญาณที่ชัดเจนว่าการลิงก์จะเป็นประโยชน์ต่อผู้ใช้ดังกล่าว

  • หลังจากลิงก์สำเร็จแล้ว คุณควรให้ตัวอย่างแก่ผู้ใช้เกี่ยวกับสิ่งที่ควรทำกับบัญชีที่ลิงก์ เช่น หากเพิ่งลิงก์บริการสตรีมมิงเพลง ให้ขอให้ Google Assistant เปิดเพลง

  • ช่วยให้ผู้ใช้จัดการบัญชีที่ลิงก์ได้ รวมถึงตัวเลือกในการยกเลิกการลิงก์ แนะนำให้ผู้ใช้ไปที่หน้าการจัดการบัญชี Google ที่ลิงก์ไว้ ซึ่งก็คือ https://myaccount.google.com/accountlinking

ข้อมูลอ้างอิง

เอกสารอ้างอิง API การตรวจสอบสิทธิ์ของ Android