Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Ứng dụng web phải lấy mã truy cập để gọi API Google một cách an toàn.
Thư viện JavaScript của Dịch vụ nhận dạng của Google hỗ trợ cả việc xác thực thông tin đăng nhập của người dùng và việc uỷ quyền để lấy mã truy cập để sử dụng với các API của Google. Thư viện này chỉ dành để sử dụng trong các trình duyệt.
Xác thực xác định danh tính của một người và thường được gọi là hoạt động đăng ký hoặc đăng nhập của người dùng. Uỷ quyền là quá trình cấp hoặc từ chối quyền truy cập vào dữ liệu hoặc tài nguyên. Quy trình này bao gồm việc thu thập và quản lý sự đồng ý của người dùng, giới hạn lượng dữ liệu hoặc tài nguyên được chia sẻ cùng với phạm vi và truy xuất mã truy cập để sử dụng với các API của Google.
Những hướng dẫn này đề cập đến các chủ đề về việc uỷ quyền và chia sẻ dữ liệu.
Nếu bạn đang cần trợ giúp về việc xác thực và cách triển khai tính năng đăng ký và đăng nhập của người dùng, hãy xem phần Đăng nhập bằng Google.
Thư viện này không dành cho mục đích sử dụng với các khung JavaScript phía máy chủ như Node.js, thay vào đó, hãy sử dụng thư viện ứng dụng Node.js của Google.
Những điểm thay đổi
Đối với người dùng, thư viện Dịch vụ nhận dạng của Google cung cấp nhiều điểm cải tiến về khả năng hữu dụng so với các thư viện JavaScript trước đây, bao gồm:
Xác thực người dùng đăng nhập và uỷ quyền lấy mã truy cập để gọi API Google hiện có hai luồng người dùng riêng biệt; một luồng cho đăng nhập và một luồng cho sự đồng ý trong quá trình cho phép, với các luồng người dùng riêng biệt để phân biệt rõ ràng bạn là ai với những việc ứng dụng có thể làm.
Cải thiện khả năng hiển thị và khả năng kiểm soát chi tiết đối với việc chia sẻ dữ liệu trong quá trình đồng ý của người dùng.
Hộp thoại bật lên dựa trên trình duyệt giúp giảm sự phiền hà và không yêu cầu người dùng phải rời khỏi trang web của bạn để:
lấy mã truy cập của Google, hoặc
gửi mã uỷ quyền đến nền tảng phụ trợ của bạn.
Đối với các nhà phát triển, trọng tâm của chúng tôi là giảm độ phức tạp, cải thiện khả năng bảo mật cũng như thực hiện quá trình tích hợp nhanh chóng và dễ dàng nhất có thể. Một số thay đổi đó là:
Quy trình xác thực người dùng để đăng nhập và uỷ quyền dùng để lấy mã truy cập để gọi API Google (là hai tập hợp đối tượng JavaScript và phương thức riêng biệt và riêng biệt). Điều này làm giảm sự phức tạp và lượng chi tiết cần thiết để triển khai quy trình xác thực hoặc uỷ quyền.
Một thư viện JavaScript duy nhất hiện hỗ trợ cả:
Luồng ngầm ẩn OAuth 2.0, dùng để lấy mã truy cập để sử dụng trong trình duyệt
Quy trình mã uỷ quyền OAuth 2.0 (còn gọi là truy cập ngoại tuyến) sẽ bắt đầu phân phối mã uỷ quyền đến nền tảng phụ trợ một cách an toàn. Tại đây, bạn có thể đổi mã đó lấy mã truy cập và mã thông báo làm mới. Trước đây, các quy trình này chỉ có sẵn bằng cách sử dụng nhiều thư viện và thông qua các lệnh gọi trực tiếp đến điểm cuối OAuth 2.0. Một thư viện duy nhất giúp bạn giảm thời gian và công sức tích hợp. Thay vì đưa vào và tìm hiểu nhiều thư viện cũng như các khái niệm về OAuth 2.0, bạn có thể tập trung vào một giao diện hợp nhất duy nhất.
Chúng tôi đã xoá lệnh chuyển hướng thông qua các hàm kiểu phương thức getter để đơn giản và dễ đọc hơn.
Khi xử lý các phản hồi uỷ quyền, bạn chọn sử dụng Promise để thực hiện yêu cầu hay không, thay vì quyết định đó.
mô-đun gapi.auth2 cũng như các đối tượng và phương thức liên kết không còn tự động được tải ngầm cho bạn mà đã được thay thế bằng các đối tượng và phương thức thư viện Dịch vụ nhận dạng của Google rõ ràng hơn.
Chúng tôi đã xoá tính năng tự động làm mới mã truy cập đã hết hạn để nâng cao khả năng bảo mật và nhận biết của người dùng. Sau khi mã truy cập hết hạn, ứng dụng của bạn phải xử lý các phản hồi lỗi API của Google, yêu cầu và nhận mã truy cập mới, hợp lệ.
Để hỗ trợ phân tách rõ ràng các thời điểm xác thực và uỷ quyền, chúng tôi sẽ ngừng hỗ trợ việc đăng nhập đồng thời người dùng vào ứng dụng và Tài khoản Google của họ, đồng thời cấp mã truy cập. Trước đây, việc yêu cầu mã truy cập cũng đã đăng nhập người dùng vào Tài khoản Google của họ và trả về thông tin xác thực mã thông báo mã truy cập JWT để xác thực người dùng.
Nhằm tăng tính bảo mật và quyền riêng tư của người dùng, thông tin xác thực của mỗi người dùng được cấp để được uỷ quyền phải tuân theo nguyên tắc về đặc quyền tối thiểu bằng cách chỉ cung cấp một mã truy cập và thông tin cần thiết để quản lý mã đó.
[null,null,["Cập nhật lần gần đây nhất: 2023-12-01 UTC."],[[["\u003cp\u003eGoogle Identity Services JavaScript library enables secure access to Google APIs via access tokens and supports user authentication.\u003c/p\u003e\n"],["\u003cp\u003eThe library offers separate, streamlined user flows for sign-in (authentication) and consent (authorization) for enhanced user experience and control.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers benefit from reduced complexity, improved security, and easier integration with features like a unified library for both implicit and authorization code flows.\u003c/p\u003e\n"],["\u003cp\u003eGoogle Identity Services prioritizes user privacy and security through measures like least privilege credentials and explicit access token refresh handling.\u003c/p\u003e\n"],["\u003cp\u003eThe library is exclusively for browser-based applications and should not be used with server-side JavaScript frameworks like Node.js.\u003c/p\u003e\n"]]],[],null,["# Authorizing for Web\n\nWeb apps must obtain an access token to securely call Google APIs.\n\nThe Google Identity Services JavaScript library supports both authentication for\nuser sign-in and authorization to obtain an access token for use with Google\nAPIs. The library is intended only for use in browsers.\n\nAuthentication establishes who someone is, and is commonly referred to as user\nsign-up or sign-in. Authorization is the process of granting or rejecting access\nto data or resources. It includes obtaining and managing user consent, limiting\nthe amount of data or resources shared with scopes, and retrieving an access\ntoken for use with Google APIs.\n\nThese guides cover authorization and data sharing topics.\n\n[How user authorization works](/identity/oauth2/web/guides/how-user-authz-works) describes the individual steps of user\nauthorization in detail and includes user dialog examples.\n\nIf you are looking for help with authentication and how to implement user\nsign-up and sign-in see [Sign In With Google](/identity/gsi/web/guides/overview).\n| **Note:** The `email`, `profile`, and `openid` scopes are used for user authentication. If your app only uses these scopes [Sign In With Google](/identity/gsi/web) is recommended instead.\n\nThis library is not intended for use with server-side JavaScript frameworks such\nas Node.js, instead use Google's [Node.js](https://github.com/googleapis/google-api-nodejs-client)\nclient library.\n\nWhat's changed\n--------------\n\nFor users, the Google Identity Services library offers numerous usability\nimprovements over earlier JavaScript libraries, including:\n\n- Authentication for user sign-in, and authorization to obtain an access token to call Google APIs, now have two separate and distinct user flows; one for [sign-in](/identity/gsi/web/guides/overview#how_it_works) and another for [consent](/identity/oauth2/web/guides/how-user-authz-works#user_consent) during authorization, with separate user flows to clearly differentiate who you are, from what an app can do.\n- Improved visibility and granular control of data sharing during [user\n consent](/identity/oauth2/web/guides/how-user-authz-works#user_consent).\n- Browser based pop-up dialogs to reduce friction, and which do not require users to leave your site to:\n - obtain an access token from Google, or\n - send an authorization code to your backend platform.\n\nFor developers, our focus has been to reduce complexity, improve security, and\nmake your integration as quick and easy as possible. Some of these changes are:\n\n- User [authentication](/identity/gsi/web/reference/js-reference) for sign-in, and [authorization](/identity/oauth2/web/reference/js-reference) used to obtain an access token to call Google APIs, are two separate and distinct sets of JavaScript objects, and methods. This reduces the complexity and amount of detail required to implement authentication or authorization.\n- A single JavaScript library now supports both the:\n - OAuth 2.0 implicit flow, used to obtain an access token for use in-browser\n - OAuth 2.0 authorization code flow, also known as offline access, and initiates securely delivering an authorization code to your backend platform, where it can be exchanged for an access token and refresh token. Previously, these flows were only available by using multiple libraries and through direct calls to OAuth 2.0 endpoints. A single library decreases your integration time and effort, instead of including and learning multiple libraries and OAuth 2.0 concepts you can focus on a single, unified interface.\n- Indirection through getter style functions has been removed for simplicity and readability.\n- When handling authorization responses you choose whether or not to use a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) to fulfill requests, instead of that decision being made for you.\n- The [Google API Client Library for JavaScript](https://github.com/google/google-api-javascript-client) has been updated with these changes:\n - the `gapi.auth2` module and associated objects and methods are no longer automatically loaded for you behind the scenes, and have been replaced with more explicit Google Identity Services library objects and methods.\n - Automatic refresh of expired access tokens has been removed to improve user security and awareness. After an access token expires your app must handle Google API error responses, request, and obtain a new, valid access token.\n - To support a clear separation of authentication and authorization moments, simultaneously signing a user in to your app and to their Google Account while also issuing an access token is no longer supported. Previously, requesting an access token also signed users into their Google Account and returned a JWT ID token credential for user authentication.\n- To increase user security and privacy, per user [credentials](/identity/oauth2/web/guides/migration-to-gis#example_credentials) issued for authorization follow the principle of least privilege by including only an access token and information required to manage it."]]