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

รูปที่ 1 ลิงก์จากขั้นตอนในแพลตฟอร์ม
ข้อกำหนด
หากต้องการใช้ Link จากแพลตฟอร์ม คุณจะต้องมีสิ่งต่อไปนี้
- แอป Android
- เป็นเจ้าของ จัดการ และดูแลรักษาเซิร์ฟเวอร์ OAuth 2.0 ที่รองรับขั้นตอนการใช้รหัสการให้สิทธิ์ OAuth 2.0
ตั้งค่า
ก่อนดำเนินการตามขั้นตอนด้านล่าง คุณต้องลงทะเบียนการลิงก์บัญชีให้เสร็จสมบูรณ์
ตั้งค่าสภาพแวดล้อมในการพัฒนาซอฟต์แวร์
รับบริการ Google Play เวอร์ชันล่าสุดในโฮสต์การพัฒนาของคุณโดยทำดังนี้
ค้นหา Google Play Services ในส่วน SDK Tools
หากสถานะของแพ็กเกจเหล่านี้ไม่ใช่ "ติดตั้งแล้ว" ให้เลือกทั้ง 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.3.0' }
เพิ่มการรองรับลิงก์จากแพลตฟอร์มของคุณ
ขั้นตอนการลิงก์จากแพลตฟอร์มของคุณจะทำให้ Google บันทึกโทเค็นการเข้าถึงที่บริการของคุณให้ไว้ ต้องได้รับความยินยอมก่อนส่งโทเค็นให้ผู้ใช้
ทำตามขั้นตอนด้านล่างเพื่อขอความยินยอมจากผู้ใช้และแสดงโทเค็นรหัสการให้สิทธิ์ผ่าน Google Play Services SDK
สร้าง 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();
สร้างกิจกรรมที่เกี่ยวข้องเพื่อจัดการความตั้งใจให้ความยินยอม
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