公告:所有在
2025 年 4 月 15 日之前注册使用 Earth Engine 的非商业项目都必须
验证是否符合非商业性质的资格条件,才能继续使用 Earth Engine。
ee.data.authenticateViaOauth
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
通过适用于 JavaScript 的 Google API 客户端库配置 EE API 调用的客户端身份验证。如果库尚未加载到网页上,系统会自动加载该库。如果用户之前未曾授予 clientId 所标识的应用对其 EE 数据的访问权限,系统会要求用户授予该权限。
应在 ee.initialize() 之前调用此方法或其他身份验证方法。
请注意,如果用户之前未向客户端 ID 所标识的应用授予访问权限,则默认情况下,此方法会尝试弹出一个对话框,提示用户授予所需权限。不过,此弹出式窗口可能会被浏览器屏蔽。为避免这种情况,请指定 opt_onImmediateFailed 回调,并在其中呈现一个网页内登录按钮,然后从该按钮的点击事件处理脚本中调用 ee.data.authenticateViaPopup()。这样一来,浏览器就不会再阻止弹出式窗口,因为该窗口现在是用户操作的直接结果。
系统会在可能的情况下自动刷新身份验证令牌。您可以放心地假设所有异步调用都将使用适当的凭据发送。不过,对于同步调用,您应使用 ee.data.getAuthToken() 检查是否存在身份验证令牌,如果没有,则手动调用 ee.data.refreshAuthToken()。令牌刷新操作是异步的,无法在同步调用之前按需在后台执行。
用法 | 返回 |
---|
ee.data.authenticateViaOauth(clientId, success, error, extraScopes, onImmediateFailed, suppressDefaultScopes) | |
参数 | 类型 | 详细信息 |
---|
clientId | 字符串 | 应用的 OAuth 客户端 ID,如果为 null,则停用经过身份验证的调用。您可以通过 Google Developers Console 获取此信息。项目必须具有与脚本运行的网域对应的 JavaScript 来源。 |
success | 函数 | 如果身份验证成功,要调用的函数。 |
error | 函数(可选) | 如果身份验证失败,要调用的函数,并传递错误消息。如果立即(后台)模式下的身份验证失败,并且指定了 opt_onImmediateFailed,则会调用该函数,而不是 opt_error。 |
extraScopes | List<String>,可选 | 要请求的额外 OAuth 范围。 |
onImmediateFailed | 函数(可选) | 如果自动后台身份验证失败,则要调用的函数。默认为 ee.data.authenticateViaPopup(),绑定到传递的回调。 |
suppressDefaultScopes | 布尔值,可选 | 如果为 true,则仅请求 opt_extraScopes 中指定的作用域;除非在 opt_extraScopes 中明确指定,否则不会请求默认作用域。 |
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-25。
[null,null,["最后更新时间 (UTC):2025-07-25。"],[[["\u003cp\u003eEnables authenticated access to Earth Engine data using your Google account via the Google APIs Client Library for JavaScript.\u003c/p\u003e\n"],["\u003cp\u003eRequires initialization before using Earth Engine functionalities and might prompt users for permission to access their Earth Engine data.\u003c/p\u003e\n"],["\u003cp\u003eAutomatically refreshes authentication tokens for asynchronous calls, but synchronous calls may require manual token refresh using \u003ccode\u003eee.data.getAuthToken()\u003c/code\u003e and \u003ccode\u003eee.data.refreshAuthToken()\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eProvides options for handling authentication failures, including specifying custom error callbacks and displaying an in-page login button to avoid browser popup blockers.\u003c/p\u003e\n"],["\u003cp\u003eAllows customization of OAuth scopes and client ID for specific application requirements.\u003c/p\u003e\n"]]],[],null,["# ee.data.authenticateViaOauth\n\n\u003cbr /\u003e\n\nConfigures client-side authentication of EE API calls through the Google APIs Client Library for JavaScript. The library will be loaded automatically if it is not already loaded on the page. The user will be asked to grant the application identified by clientId access to their EE data if they have not done so previously.\n\n\u003cbr /\u003e\n\nThis or another authentication method should be called before ee.initialize().\n\nNote that if the user has not previously granted access to the application identified by the client ID, by default this will try to pop up a dialog window prompting the user to grant the required permission. However, this popup can be blocked by the browser. To avoid this, specify the opt_onImmediateFailed callback, and in it render an in-page login button, then call ee.data.authenticateViaPopup() from the click event handler of this button. This stops the browser from blocking the popup, as it is now the direct result of a user action.\n\nThe auth token will be refreshed automatically when possible. You can safely assume that all async calls will be sent with the appropriate credentials. For synchronous calls, however, you should check for an auth token with ee.data.getAuthToken() and call ee.data.refreshAuthToken() manually if there is none. The token refresh operation is asynchronous and cannot be performed behind-the-scenes on-demand prior to synchronous calls.\n\n| Usage | Returns |\n|------------------------------------------------------------------------------------------------------------------------------------|---------|\n| `ee.data.authenticateViaOauth(clientId, success, `*error* `, `*extraScopes* `, `*onImmediateFailed* `, `*suppressDefaultScopes*`)` | |\n\n| Argument | Type | Details |\n|-------------------------|--------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `clientId` | String | The application's OAuth client ID, or null to disable authenticated calls. This can be obtained through the Google Developers Console. The project must have a JavaScript origin that corresponds to the domain where the script is running. |\n| `success` | Function | The function to call if authentication succeeded. |\n| `error` | Function, optional | The function to call if authentication failed, passed the error message. If authentication in immediate (behind-the-scenes) mode fails and opt_onImmediateFailed is specified, that function is called instead of opt_error. |\n| `extraScopes` | List\\\u003cString\\\u003e, optional | Extra OAuth scopes to request. |\n| `onImmediateFailed` | Function, optional | The function to call if automatic behind-the-scenes authentication fails. Defaults to ee.data.authenticateViaPopup(), bound to the passed callbacks. |\n| `suppressDefaultScopes` | Boolean, optional | When true, only scopes specified in opt_extraScopes are requested; the default scopes are not requested unless explicitly specified in opt_extraScopes. |"]]