แนวทางปฏิบัติแนะนำ
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
หน้านี้จะกล่าวถึงแนวทางปฏิบัติแนะนำทั่วไปบางส่วนสำหรับการผสานรวมกับ OAuth 2.0 โปรดพิจารณาแนวทางปฏิบัติแนะนำเหล่านี้เพิ่มเติมจากคำแนะนำเฉพาะสำหรับประเภทแอปพลิเคชันและแพลตฟอร์มการพัฒนาของคุณ โปรดดูคำแนะนำในการเตรียมแอปให้พร้อมสำหรับเวอร์ชันที่ใช้งานจริงและนโยบาย OAuth 2.0 ของ Google ด้วย
จัดการข้อมูลเข้าสู่ระบบของไคลเอ็นต์อย่างปลอดภัย
ข้อมูลเข้าสู่ระบบไคลเอ็นต์ OAuth จะระบุตัวตนของแอปและควรได้รับการจัดการอย่างระมัดระวัง จัดเก็บข้อมูลเข้าสู่ระบบเหล่านี้ในที่เก็บข้อมูลที่ปลอดภัยเท่านั้น เช่น ใช้เครื่องมือจัดการข้อมูลลับอย่าง Google Cloud Secret Manager
อย่าฮาร์ดโค้ดข้อมูลเข้าสู่ระบบ ส่งไปยังที่เก็บโค้ด หรือเผยแพร่ต่อสาธารณะ
จัดการโทเค็นผู้ใช้อย่างปลอดภัย
โทเค็นผู้ใช้ประกอบด้วยทั้งโทเค็นการรีเฟรชและโทเค็นเพื่อการเข้าถึงที่แอปพลิเคชันของคุณใช้ จัดเก็บโทเค็นอย่างปลอดภัยในขณะที่ไม่มีการใช้งาน
และห้ามส่งโทเค็นเป็นข้อความธรรมดา ใช้ระบบจัดเก็บข้อมูลที่ปลอดภัยซึ่งเหมาะกับแพลตฟอร์มของคุณ เช่น
Keystore ใน Android
บริการพวงกุญแจใน iOS และ macOS หรือ Credential Locker ใน Windows
เพิกถอนโทเค็นทันทีที่
ไม่จำเป็นต้องใช้แล้ว และลบโทเค็นออกจากระบบอย่างถาวร
นอกจากนี้ โปรดพิจารณาแนวทางปฏิบัติแนะนำต่อไปนี้สำหรับแพลตฟอร์มของคุณด้วย
- สำหรับแอปพลิเคชันฝั่งเซิร์ฟเวอร์ที่จัดเก็บโทเค็นสำหรับผู้ใช้จำนวนมาก ให้เข้ารหัสโทเค็นที่พักไว้และตรวจสอบว่าที่เก็บข้อมูลของคุณไม่ได้เปิดให้เข้าถึงได้แบบสาธารณะบนอินเทอร์เน็ต
- สำหรับแอปเดสก์ท็อปเนทีฟ เราขอแนะนำอย่างยิ่งให้ใช้โปรโตคอล Proof Key for Code Exchange (PKCE) เพื่อรับรหัสการให้สิทธิ์ที่สามารถแลกเปลี่ยนเป็นโทเค็นเพื่อการเข้าถึงได้
จัดการการเพิกถอนและการหมดอายุของโทเค็นการรีเฟรช
หากแอปขอโทเค็นการรีเฟรช
สำหรับการเข้าถึงแบบออฟไลน์ คุณต้องจัดการการทำให้โทเค็นดังกล่าวไม่ถูกต้องหรือการหมดอายุด้วย โทเค็น
อาจใช้ไม่ได้ด้วยเหตุผลต่างๆ
เช่น โทเค็นอาจหมดอายุ หรือผู้ใช้หรือกระบวนการอัตโนมัติอาจเพิกถอนสิทธิ์เข้าถึงของแอป ในกรณีนี้ ให้พิจารณาอย่างรอบคอบว่าแอปพลิเคชันควรตอบสนองอย่างไร
รวมถึงการแจ้งให้ผู้ใช้ทราบเมื่อลงชื่อเข้าใช้ครั้งถัดไปหรือการล้างข้อมูลของผู้ใช้ หากต้องการรับการแจ้งเตือนเกี่ยวกับการเพิกถอนโทเค็น ให้ผสานรวมกับบริการการป้องกันแบบครอบคลุมหลายบัญชี
ใช้การให้สิทธิ์แบบเพิ่มทีละส่วน
ใช้การให้สิทธิ์
แบบเพิ่มทีละรายการเพื่อขอขอบเขต OAuth ที่เหมาะสมเมื่อแอปพลิเคชันของคุณต้องการฟังก์ชันการทำงาน
คุณไม่ควรร้องขอสิทธิ์เข้าถึงข้อมูลเมื่อผู้ใช้ตรวจสอบสิทธิ์ครั้งแรก เว้นแต่จะจำเป็น
สำหรับฟังก์ชันหลักของแอป ให้ร้องขอเฉพาะขอบเขตที่จำเป็น
สำหรับงาน โดยทำตามหลักการเลือกขอบเขตที่เล็กที่สุดและจำกัดที่สุดเท่าที่จะเป็นไปได้
ขอขอบเขตในบริบทเสมอเพื่อช่วยให้ผู้ใช้เข้าใจเหตุผลที่แอปของคุณขอสิทธิ์เข้าถึง
และวิธีใช้ข้อมูล
ตัวอย่างเช่น แอปพลิเคชันของคุณอาจใช้รูปแบบต่อไปนี้
- ผู้ใช้ตรวจสอบสิทธิ์ด้วยแอปของคุณ
- โดยไม่มีการขอขอบเขตเพิ่มเติม แอปมีฟังก์ชันพื้นฐานเพื่อให้ผู้ใช้
สำรวจและใช้ฟีเจอร์ที่ไม่ต้องใช้ข้อมูลหรือสิทธิ์เข้าถึงเพิ่มเติม
- ผู้ใช้เลือกฟีเจอร์ที่ต้องมีสิทธิ์เข้าถึงข้อมูลเพิ่มเติม
- แอปพลิเคชันของคุณส่งคำขอให้สิทธิ์สำหรับขอบเขต OAuth ที่เจาะจงนี้ ซึ่งจำเป็นสำหรับฟีเจอร์นี้
หากฟีเจอร์นี้ต้องใช้ขอบเขตหลายรายการ ให้ทำตามแนวทางปฏิบัติแนะนำด้านล่าง
- หากผู้ใช้ปฏิเสธคำขอ แอปจะปิดใช้ฟีเจอร์และให้บริบทเพิ่มเติมแก่ผู้ใช้เพื่อขอสิทธิ์เข้าถึงอีกครั้ง
จัดการความยินยอมสำหรับขอบเขตหลายรายการ
เมื่อขอหลายขอบเขตพร้อมกัน ผู้ใช้อาจไม่ให้สิทธิ์ขอบเขต OAuth ทั้งหมดที่คุณขอ
แอปของคุณควรจัดการการปฏิเสธขอบเขตโดยการปิดใช้ฟังก์ชันที่เกี่ยวข้อง
หากฟังก์ชันพื้นฐานของแอปต้องใช้ขอบเขตหลายรายการ โปรดอธิบายให้ผู้ใช้ทราบก่อน
ขอความยินยอม
คุณจะแจ้งให้ผู้ใช้ทราบอีกครั้งได้ก็ต่อเมื่อผู้ใช้แสดงเจตนาอย่างชัดเจนที่จะใช้
ฟีเจอร์ที่เฉพาะเจาะจงซึ่งต้องใช้ขอบเขต แอปของคุณควรให้บริบทที่เกี่ยวข้องแก่ผู้ใช้
และให้เหตุผลก่อนขอขอบเขต OAuth
คุณควรลดจำนวนขอบเขตที่แอปขอในครั้งเดียว แต่ให้ใช้การให้สิทธิ์ทีละส่วนเพื่อขอขอบเขต
ในบริบทของฟีเจอร์และฟังก์ชันการทำงานแทน
ใช้เบราว์เซอร์ที่ปลอดภัย
ในเว็บ คำขอการให้สิทธิ์ OAuth 2.0 ต้องมาจากเว็บเบราว์เซอร์ที่มีฟีเจอร์ครบถ้วนเท่านั้น
ในแพลตฟอร์มอื่นๆ โปรดเลือกประเภทไคลเอ็นต์ OAuth ที่ถูกต้องและผสานรวม OAuth ตามความเหมาะสมกับแพลตฟอร์มของคุณ ห้ามเปลี่ยนเส้นทางคำขอผ่านสภาพแวดล้อมการท่องเว็บแบบฝัง
รวมถึง WebView ในแพลตฟอร์มอุปกรณ์เคลื่อนที่ เช่น WebView ใน Android หรือ WKWebView ใน
iOS แต่ให้ใช้ไลบรารี OAuth ดั้งเดิม
หรือ Google Sign-In สำหรับแพลตฟอร์มของคุณแทน
การสร้างและการกำหนดค่าไคลเอ็นต์ OAuth ด้วยตนเอง
เพื่อป้องกันการละเมิด คุณไม่สามารถสร้างหรือแก้ไขไคลเอ็นต์ OAuth โดยใช้โปรแกรมได้ คุณ
ต้องใช้คอนโซลนักพัฒนาแอป Google เพื่อรับทราบข้อกำหนดในการให้บริการอย่างชัดเจน กำหนดค่า
ไคลเอ็นต์ OAuth และเตรียมพร้อมสำหรับการยืนยัน OAuth
สำหรับเวิร์กโฟลว์อัตโนมัติ ให้พิจารณาใช้บัญชีบริการแทน
นำไคลเอ็นต์ OAuth ที่ไม่ได้ใช้ออก
ตรวจสอบไคลเอ็นต์ OAuth 2.0 เป็นประจำ และลบไคลเอ็นต์ที่แอปพลิเคชันของคุณไม่ต้องการอีกต่อไปหรือล้าสมัยไปแล้วออกโดยทันที
การปล่อยให้ไคลเอ็นต์ที่ไม่ได้ใช้มีการกำหนดค่าไว้อาจ
เป็นความเสี่ยงด้านความปลอดภัย เนื่องจากไคลเอ็นต์อาจถูกนำไปใช้ในทางที่ผิดหากข้อมูลเข้าสู่ระบบของไคลเอ็นต์
ถูกบุกรุก
เพื่อลดความเสี่ยงจากไคลเอ็นต์ที่ไม่ได้ใช้เพิ่มเติม ไคลเอ็นต์ OAuth 2.0 ที่ไม่ได้ใช้งานเป็นเวลา 6 เดือนจะ
ถูกลบโดยอัตโนมัติ
แนวทางปฏิบัติแนะนำคืออย่ารอการลบอัตโนมัติ แต่ให้ลบไคลเอ็นต์ที่ไม่ได้ใช้ออก
อย่างสม่ำเสมอ แนวทางปฏิบัตินี้จะช่วยลดพื้นที่ในการโจมตีของแอปพลิเคชันและรับประกัน
สุขอนามัยด้านความปลอดภัยที่ดี
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-31 UTC
[null,null,["อัปเดตล่าสุด 2025-08-31 UTC"],[[["\u003cp\u003eSecurely store and manage OAuth client credentials, avoiding hardcoding or public exposure.\u003c/p\u003e\n"],["\u003cp\u003eProtect user tokens (refresh and access) by storing them securely and revoking them when no longer needed.\u003c/p\u003e\n"],["\u003cp\u003eImplement proper handling of refresh token revocation and expiration scenarios, including user notification and data cleanup.\u003c/p\u003e\n"],["\u003cp\u003eUtilize incremental authorization to request only necessary OAuth scopes in context, minimizing initial requests and enhancing user privacy.\u003c/p\u003e\n"],["\u003cp\u003eEmploy secure browsing environments for OAuth authorization requests, avoiding embedded browsers like webviews and opting for native libraries or Google Sign-in.\u003c/p\u003e\n"]]],[],null,["This page covers some general best practices for integrating with OAuth 2.0. Consider these best\npractices in addition to any specific guidance for your type of application and development\nplatform. Also refer to the\n[advice for getting\nyour app ready for production](/identity/protocols/oauth2/production-readiness/policy-compliance) and [Google's\nOAuth 2.0 policies](/identity/protocols/oauth2/policies).\n\nHandle client credentials securely\n\n\nThe OAuth client credentials identify your app's identity and should be handled carefully. Only\nstore these credentials in secure storage, for example using a secret manager such as\n[Google Cloud Secret Manager](https://cloud.google.com/secret-manager/docs/overview).\nDo not hardcode the credentials, commit them to a code repository or publish them publicly.\n\nHandle user tokens securely\n\n\nUser tokens include both refresh tokens and access tokens used by your application. Store\ntokens securely [at rest](https://wikipedia.org/wiki/Data_at_rest)\nand never transmit them in plain text. Use a secure storage system appropriate for your\nplatform, such as\n[Keystore](https://developer.android.com/training/articles/keystore) on Android,\nKeychain Services on iOS and macOS, or Credential Locker on Windows.\n\n\n[Revoke tokens](/identity/protocols/oauth2/web-server#tokenrevoke) as soon as they\nare no longer needed and delete them permanently from your systems.\n\n\nIn addition, also consider these best practices for your platform:\n\n- For [server-side](/identity/protocols/oauth2/web-server) applications that store tokens for many users, encrypt them at rest and ensure that your data store is not publicly accessible to the Internet.\n- For native desktop apps, using the [Proof Key for Code\n Exchange (PKCE) protocol](/identity/protocols/oauth2/native-app#obtainingaccesstokens) is strongly recommended to obtain authorization codes that can be exchanged for access tokens.\n\nHandle refresh token revocation and expiration\n\n\nIf your app has requested a [refresh\ntoken for offline access](/identity/protocols/oauth2/web-server#offline), you must also handle their invalidation or expiration. Tokens\ncould be [invalidated for different reasons](/identity/protocols/oauth2#expiration),\nfor example it could have expired or your apps' access could have been revoked by the user or\nan automated process. In this case, consider carefully how your application should respond,\nincluding prompting the user at their next log in or cleaning up their data. To be notified of\ntoken revocation, integrate with the [Cross-Account\nProtection](/identity/protocols/risc) service.\n\nUse incremental authorization\n\n\nUse [incremental\nauthorization](/identity/protocols/oauth2/web-server#incrementalAuth) to request appropriate OAuth scopes when the functionality is needed by your\napplication.\n\n\nYou should not request access to data when the user first authenticates, unless it is essential\nfor the core functionality of your app. Instead, request only the specific scopes that are\nneeded for a task, following the principle to\n[select the smallest, most limited scopes possible](/identity/protocols/oauth2/production-readiness/policy-compliance#only-request-needed-scopes).\n\n\nAlways request scopes in context to help your users understand why your app is requesting access\nand how the data will be used.\n\n\nFor example, your application may follow this model:\n\n1. The user authenticates with your app\n 1. No additional scopes are requested. The app provides basic functionality to let the user explore and use features that do not require any additional data or access.\n2. The user selects a feature that requires access to additional data\n 1. Your application makes an authorization request for this specific OAuth scope required for this feature. If this feature requires multiple scopes, follow [the best practices below](#multiple-scopes).\n 2. If the user denies the request, the app disables the feature and gives the user additional context to request access again.\n\nHandle consent for multiple scopes\n\n\nWhen requesting multiple scopes at once, users may not grant all OAuth scopes you have\nrequested. Your app should handle the denial of scopes by disabling relevant functionality.\n\n\nIf your app's basic functionality requires multiple scopes, explain this to the user before\nprompting for consent.\n\n\nYou may only prompt the user again once they have clearly indicated an intent to use the\nspecific feature that requires the scope. Your app should provide the user with relevant context\nand justification before requesting OAuth scopes.\n\n\nYou should minimize the number of scopes your app requests at once. Instead,\n[utilize incremental authorization](#use-incremental-authorization) to request scopes\nin context of features and functionality.\n\nUse secure browsers\n\n\nOn the web, OAuth 2.0 authorization requests must only be made from full-featured web browsers.\nOn other platforms, make sure to select the\n[correct OAuth client type](/identity/protocols/oauth2#basicsteps) and integrate\nOAuth as appropriate for your platform. Do not redirect the request through embedded browsing\nenvironments, including webviews on mobile platforms, such as WebView on Android or WKWebView on\niOS. Instead, utilize [native OAuth libraries](/identity/protocols/oauth2/native-app)\nor [Google Sign-in](/identity/authorization) for your platform.\n\nManual creation and configuration of OAuth clients\n\n\nIn order to prevent abuse, OAuth clients cannot be created or modified programmatically. You\nmust use the Google Developers console to explicitly acknowledge the terms of service, configure\nyour OAuth client and prepare for OAuth verification.\n\n\nFor automated workflows, consider using\n[service accounts](/identity/protocols/oauth2/service-account) instead.\n\nRemove unused OAuth clients\n\n\nRegularly audit your OAuth 2.0 clients and proactively delete any that are no longer required by\nyour application or have become obsolete. Leaving unused clients configured represents a\npotential security risk as the client can be misused if your client credentials are ever\ncompromised.\n\n\nTo further mitigate risks from unused clients, OAuth 2.0 clients that have been inactive for six\nmonths are [automatically deleted](https://support.google.com/cloud/answer/15549257#unused-client-deletion).\n\n\nThe recommended best practice is to not wait for automatic deletion but rather proactively\nremove unused clients. This practice minimizes your application's attack surface and ensures\ngood security hygiene."]]