اگر مقادیر نامعتبر را روی پارامترهای OAuth تنظیم کنید، مانند شناسه مشتری نامعتبر، شناسه های محدوده یا مقادیر نوع پاسخ، صفحه خطای OAuth را مشاهده خواهید کرد.
پاسخ های خطای OAuth
OAuth ممکن است یک پاسخ خطا را برگرداند، در این صورت تابع پاسخ callback شما با پاسخ خطا به عنوان پارامتر فعال می شود. در زیر نمونه ای از پاسخ خطای OAuth است.
{
"error":"access_denied"
}
چند نمونه عبارتند از:
کاربر درخواست OAuth را رد می کند.
برای یک درخواست OAuth با پارامتر prompt=none ، کاربر قبلاً احراز هویت نشده است و رضایت از قبل برای محدوده های درخواستی پیکربندی نکرده است.
این مثال نحوه رسیدگی به پاسخ های موفقیت آمیز و خطای OAuth را نشان می دهد:
پنجره بازشو قبل از بازگشت یک پاسخ OAuth بسته می شود.
این کتابخانه این خطاها را ضبط می کند و در صورت تنظیم error_callback را راه اندازی می کند. حتما نوع خطا را بررسی کنید. در غیر این صورت، زمانی که این کتابخانه بعداً از انواع خطاهای جدید پشتیبانی کند، منطق کد شما ممکن است تحت تأثیر قرار گیرد.
functionmyErrorCallback(err){if(err.type=='popup_failed_to_open'){// The popup window is failed to open......}elseif(err.type=='popup_closed'){// The popup window is closed before an OAuth response is returned......}}constclient=google.accounts.oauth2.initCodeClient({client_id:'YOUR_GOOGLE_CLIENT_ID',scope:'https://www.googleapis.com/auth/calendar.readonly',ux_mode:'popup',callback:myCallback,error_callback:myErrorCallback});
تاریخ آخرین بهروزرسانی 2025-08-08 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-08-08 بهوقت ساعت هماهنگ جهانی."],[[["\u003cp\u003eErrors can occur at different stages of the OAuth process and may have varying notifications.\u003c/p\u003e\n"],["\u003cp\u003eMissing or invalid OAuth parameters should be addressed during development using the Google API console.\u003c/p\u003e\n"],["\u003cp\u003eOAuth error responses, such as user denial or missing consent, are handled within your defined \u003ccode\u003ecallback\u003c/code\u003e function.\u003c/p\u003e\n"],["\u003cp\u003eNon-OAuth errors, like popup window issues, are captured by the \u003ccode\u003eerror_callback\u003c/code\u003e function for specific handling.\u003c/p\u003e\n"]]],[],null,["# Handle Errors\n\nErrors may happen in different layers. You may get notified in different ways\ndependent on where the error happens.\n\nRequired OAuth Parameters\n-------------------------\n\nIf you forget to set the required OAuth parameters, such as the client_id or\nscope, you'll see an error message in your browser's JavaScript Console.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| **Note:** Missing required OAuth parameter issues should be fixed in the development phase.\n\n#### Fix OAuth Configuration Errors\n\nChanges in the [Google APIs console](https://console.developers.google.com/apis) may be required to resolve some errors.\n\n- [Creates a client ID](/identity/oauth2/web/guides/get-google-api-clientid) if not yet.\n- For popup UX, add all domains that may trigger the current flow to `Authorized JavaScript origins`.\n- For redirect UX, add all URLs that may receive authorization responses to `Authorized redirect URIs`.\n- Properly [configure your OAuth Consent screen](/identity/protocols/oauth2/production-readiness/brand-verification#oauth-consent-screen).\n - [Submit your app for verification](/identity/protocols/oauth2/production-readiness/brand-verification#submit-app-for-verification) if needed.\n- You might need to take additional steps to [comply with Google's OAuth 2.0 Policies](/identity/protocols/oauth2/production-readiness/policy-compliance).\n\nInvalid OAuth Parameter Values\n------------------------------\n\nIf you set the invalid values to OAuth parameters, such as the invalid\nclient ID, scope identifiers, or response type values, you'll see the OAuth\nerror page.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| **Note:** Invalid OAuth value issues should be fixed in the development phase.\n| **Note:** Changes in the Developer Console may be required to resolve some errors. Check the [`Fix OAuth Configuration Errors`](/identity/oauth2/web/guides/error#fix_oauth_configuration_errors) section for more details.\n\nOAuth Error Responses\n---------------------\n\nOAuth may return an error response, in which case your [`callback`](/identity/oauth2/web/reference/js-reference#google.accounts.oauth2.initCodeClient) function\nwill be triggered with the error response as the parameter. The following is an\nexample OAuth error response. \n\n```scdoc\n {\n \"error\":\"access_denied\"\n }\n```\n\nSome examples are:\n\n1. The user denies the OAuth request.\n2. For an OAuth request with [`prompt=none`](/identity/protocols/oauth2/openid-connect#authenticationuriparameters) parameter, the user is not already authenticated and has not pre-configured consent for the requested scopes.\n\nThis example shows how to handle the success and error OAuth responses: \n\n function myCallback(response) {\n if (response.error) {\n // Handle error response\n ... ...\n } else if (response.code) {\n // Handle success code response\n ... ...\n }\n }\n\n| **Note:** OAuth error responses should be handled by your `callback` function.\n\nNon-OAuth Errors\n----------------\n\nOAuth doesn't define the behaviors when:\n\n1. the popup window fails to open.\n2. the popup window is closed before an OAuth response is returned.\n\nThis library captures these errors, and triggers the [`error_callback`](/identity/oauth2/web/reference/js-reference#google.accounts.oauth2.initCodeClient) if\nset. Be sure to check the error type. Otherwise, your code logic may\nbe affected when this library support new error types later. \n\n function myErrorCallback(err) {\n if (err.type == 'popup_failed_to_open') {\n // The popup window is failed to open\n ... ...\n } else if (err.type == 'popup_closed') {\n // The popup window is closed before an OAuth response is returned\n ... ...\n }\n }\n\n const client = google.accounts.oauth2.initCodeClient({\n client_id: 'YOUR_GOOGLE_CLIENT_ID',\n scope: 'https://www.googleapis.com/auth/calendar.readonly',\n ux_mode: 'popup',\n callback: myCallback,\n error_callback: myErrorCallback\n });\n\n| **Note:** Use `error_callback` to capture errors like popup windows are failed to open or closed directly."]]