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

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

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

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

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

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

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

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

ลิงก์จากแพลตฟอร์มของคุณดำเนินการตามขั้นตอนต่อไปนี้

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

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

ข้อกำหนด

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

ตั้งค่า

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

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

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

  1. เปิด Android SDK Manager
  1. ค้นหาบริการ Google Play ในส่วนเครื่องมือ SDK

  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.0.0'
    }
    

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

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

  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. สร้างกิจกรรมที่เกี่ยวข้องเพื่อจัดการความตั้งใจในการขอความยินยอม

    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