Thử nghiệm với nhiều loại thông tin xác thực và phạm vi
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.
Phần thông tin đăng nhập của Trình khám phá API cho phép bạn kiểm thử bằng nhiều thông tin đăng nhập và phạm vi. Tính năng này hữu ích trong việc đảm bảo bạn đang sử dụng thông tin đăng nhập và phạm vi tối thiểu cần thiết để gọi một điểm cuối API.
Thử nghiệm bằng nhiều thông tin đăng nhập
Theo mặc định, cả hai loại thông tin xác thực OAuth 2 và Khoá API đều được đánh dấu.
Nếu bạn chỉ muốn kiểm thử bằng một thông tin đăng nhập, hãy đánh dấu vào loại thông tin đăng nhập cần sử dụng (và bỏ đánh dấu loại thông tin đăng nhập còn lại). Sau đây là các quy tắc chung để chọn thông tin đăng nhập để kiểm thử:
Nếu đang sử dụng một phương thức để truy cập vào dữ liệu công khai (chẳng hạn như dữ liệu trên Google Maps), bạn có thể sử dụng khoá API làm thông tin đăng nhập. Chọn Khoá API và bỏ chọn Google OAuth 2.0.
Nếu đang sử dụng một phương thức để truy cập vào dữ liệu riêng tư, chẳng hạn như dữ liệu trên Google Drive của người dùng, thì bạn phải sử dụng thông tin xác thực OAuth 2. Bỏ chọn Khoá API và chọn Google OAuth 2.0.
Nếu bạn nhận được kết quả là 200, thì thông tin đăng nhập là đủ để truy cập vào dữ liệu. Nếu nhận được kết quả là 401, bạn cần sử dụng thông tin đăng nhập OAuth 2 để truy cập vào dữ liệu.
Thử nghiệm với nhiều phạm vi
Phạm vi là một chuỗi, chẳng hạn như https://www.googleapis.com/auth/drive.metadata.readonly, xác định cấp độ truy cập vào các tài nguyên mà ứng dụng của bạn yêu cầu, chẳng hạn như cấp độ truy cập vào dữ liệu thuộc sở hữu của người dùng. Tốt nhất là bạn nên dùng các phạm vi có mức truy cập hẹp nhất vào dữ liệu của người dùng. Các phương thức khác nhau có thể yêu cầu các phạm vi khác nhau. Ví dụ: một phương thức trong API có thể yêu cầu ít nhất một phạm vi chỉ đọc, trong khi các phương thức khác có thể yêu cầu một phạm vi đọc-ghi.
Theo mặc định, tất cả các phạm vi OAuth 2.0 của Google cho một API đều được bật trong Trình khám phá API. Cách kiểm thử với một phạm vi cụ thể:
Trong phần Thông tin xác thực Google OAuth 2.0, hãy nhấp vào hiện phạm vi.
Kiểm tra phạm vi cần sử dụng khi gọi phương thức.
Nhấp vào Execute (Thực thi) để thực thi API chỉ bằng phạm vi đã chọn. Trình khám phá API sẽ hiển thị một màn hình uỷ quyền yêu cầu bạn cấp quyền truy cập để sử dụng phạm vi.
Nhấp vào Cho phép. Kết quả sẽ xuất hiện bên dưới nút Thực thi.
Nếu Trình khám phá API cho thấy kết quả là 200 trong "application/json", thì phạm vi này đủ để gọi phương thức. Nếu Trình khám phá API cho thấy kết quả là 403, thì tức là bạn đã chọn một phạm vi không đủ để gọi phương thức. Hãy thử một phạm vi rộng hơn.
Để biết thêm thông tin về cách chọn phạm vi, hãy tham khảo phần xác thực và uỷ quyền trong tài liệu về API cụ thể của Google mà bạn sử dụng.
Thu hồi quyền truy cập được uỷ quyền để kiểm thử bằng các phạm vi mới
Sau khi bạn sử dụng màn hình uỷ quyền của Google để phê duyệt việc uỷ quyền, Trình khám phá API vẫn được phép thực hiện các yêu cầu đã xác thực miễn là thông tin đăng nhập chưa hết hạn. Việc bỏ chọn các phạm vi sẽ không đặt lại thông tin đăng nhập và phạm vi đã được phê duyệt trước đó. Nếu muốn kiểm thử bằng cấu hình phạm vi mới, bạn phải thu hồi thông tin đăng nhập đã được phê duyệt trước đó.
Cách thu hồi thông tin đăng nhập mà Trình khám phá API sử dụng:
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-07-26 UTC."],[[["\u003cp\u003eThe API Explorer's credentials section allows testing with different credentials (API key or Google OAuth 2.0) and scopes to ensure minimal access requirements.\u003c/p\u003e\n"],["\u003cp\u003eAPI keys are suitable for accessing public data, while OAuth 2.0 credentials are necessary for private data access.\u003c/p\u003e\n"],["\u003cp\u003eUsing the scopes feature, you can select specific access levels (read-only, read-write, etc.) and verify their sufficiency for invoking API methods.\u003c/p\u003e\n"],["\u003cp\u003eTo test with new scopes, you may need to revoke previously approved credentials through your Google account settings.\u003c/p\u003e\n"]]],[],null,["# Test with different credential types and scopes\n\nThe APIs Explorer's credentials section lets you test with different\ncredentials and scopes. This feature is useful in ensuring you're using the\nminimal required credentials and scope to invoke an API endpoint.\n\nTest with different credentials\n-------------------------------\n\nBy default, both the OAuth 2 and API Key credential types are checked.\nIf you want to test with only one credential, check on the credential type to\nuse (and uncheck the other credential type). Following are the general rules for\npicking a credential to test with:\n\n- If you're using a method to access public data, such as Google Maps data, you\n can use an API key as your credential. Check **API key** and uncheck\n **Google OAuth 2.0**.\n\n- If you're using a method to access private data, such as the user's Google\n Drive data, you must use an OAuth 2 credential. Uncheck **API key** and check\n **Google OAuth 2.0**.\n\nIf you receive a result of 200, the credential is sufficient\nfor accessing the data. If you receive a result of 401, you need\nto use OAuth 2 credential to access the data.\n\nTest with different scopes\n--------------------------\n\nA scope is a string, such as\n`https://www.googleapis.com/auth/drive.metadata.readonly`, defining a level of\naccess to resources required by your app, such as the level of access to data\nowned by the users. Ideally, you want your app to use scopes with\nthe narrowest level of access to the user's data. Different methods might\nrequire different scopes. For example, a method in an API might require\nat least a read-only scope, while other methods might require a read-write\nscope.\n| **Note:** Scopes are only applicable to Google OAuth 2.0. If you're testing with an API key, you won't test with different scopes.\n\nBy default, all Google OAuth 2.0 scopes for an API are enabled in the APIs\nExplorer. To test with a specific scope:\n\n1. In the Credentials Google OAuth 2.0 section, click **show scopes**.\n2. Check the scope to use when invoking the method.\n3. Click **Execute** to execute the API using only the selected scope. The APIs Explorer shows an authorization screen asking you to grant access to use the scope.\n4. Click **Allow**. The results appear below the Execute button.\n\nIf the APIs Explorer shows a result of 200 under an \"application/json\", then the\nscope is sufficient to invoke the method. If the APIs Explorer shows an\nresult of 403, you have chosen an insufficient scope to invoke the method. Try\na broader scope.\n\nFor additional information on scope selection, refer to the authentication and\nauthorization section of documentation for your specific Google API.\n\n### Revoke authorized access to test with new scopes\n\nAfter you have used the Google authorization screen to approve authorization,\nthe APIs Explorer remains authorized to make authenticated requests as long\nas the credentials aren't expired. Unchecking scopes doesn't reset the\npreviously approved credentials and scopes. If you want to test with a new\nscope configuration, you must revoke your previously approved credentials.\n\nTo revoke the credentials used by the APIs Explorer:\n\n1. Navigate to [Apps with access to your account](https://www.google.com/accounts/IssuedAuthSubTokens).\n2. Under **Third-party apps with account access** , click **Google APIs Explorer**.\n3. Click **Remove Access**.\n4. (optional) Reload the documentation page to reset the APIs Explorer."]]