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

รูปที่ 1 ลิงก์จากโฟลว์ของแพลตฟอร์ม
ข้อกำหนด
หากต้องการใช้ลิงก์จากแพลตฟอร์ม คุณจะต้องมีสิ่งต่อไปนี้
- แอป Android
- เป็นเจ้าของ จัดการ และบำรุงรักษาเซิร์ฟเวอร์ OAuth 2.0 ที่รองรับขั้นตอนการใช้รหัสการให้สิทธิ์ของ OAuth 2.0
ตั้งค่า
ก่อนดำเนินการตามขั้นตอนด้านล่าง คุณต้องลงทะเบียนการลิงก์บัญชีให้เสร็จสมบูรณ์
ตั้งค่าสภาพแวดล้อมในการพัฒนา
ดาวน์โหลดบริการ Google Play เวอร์ชันล่าสุดในโฮสต์การพัฒนา
- เปิด Android SDK Manager
ในส่วน SDK Tools ให้ค้นหา Google Play services
หากสถานะของแพ็กเกจเหล่านี้เป็น "ไม่ได้ติดตั้ง" ให้เลือกทั้ง 2 แพ็กเกจ แล้วคลิกติดตั้งแพ็กเกจ
กำหนดค่าแอป
ในไฟล์
build.gradle
ระดับโปรเจ็กต์ ให้รวมที่เก็บ Maven ของ Google ไว้ทั้งในส่วนbuildscript
และallprojects
buildscript { repositories { google() } } allprojects { repositories { google() } }
เพิ่มทรัพยากร Dependency สำหรับ API "ลิงก์กับ Google" ลงในไฟล์ Gradle ระดับแอปของโมดูล ซึ่งโดยปกติคือ
app/build.gradle
dependencies { implementation 'com.google.android.gms:play-services-auth:21.4.0' }
เพิ่มการรองรับลิงก์จากแพลตฟอร์มของคุณ
ลิงก์จากโฟลว์แพลตฟอร์มจะส่งผลให้ Google บันทึกโทเค็นเพื่อการเข้าถึงที่บริการของคุณให้ไว้ ต้องได้รับความยินยอมก่อนจึงจะส่งคืนโทเค็นสำหรับผู้ใช้ได้
ทำตามขั้นตอนด้านล่างเพื่อขอความยินยอมจากผู้ใช้และส่งคืนโทเค็นรหัสการให้สิทธิ์ผ่าน SDK ของ Google Play Services
สร้าง PendingIntent ที่เปิดใช้กิจกรรมความยินยอมได้ - Play Services API จะเปิดใช้ความยินยอม คุณจะต้องระบุ
PendingIntent
(ซึ่งจะเรียกว่าconsentPendingIntent
เพื่อความชัดเจน) เมื่อเรียกใช้ APIKotlin
// 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();
สร้างกิจกรรมที่เกี่ยวข้องเพื่อจัดการ 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()
หากผู้ใช้ยอมรับหรือปฏิเสธ/ยกเลิกความยินยอมของคุณตามลำดับสร้างคำขอเพื่อบันทึกโทเค็น และส่ง
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