این سند توضیح میدهد که چگونه برنامههای نصبشده در دستگاههایی مانند تلفنها، تبلتها و رایانهها از نقاط پایانی OAuth 2.0 Google برای اجازه دسترسی به APIهای Google استفاده میکنند.
OAuth 2.0 به کاربران اجازه می دهد تا داده های خاصی را با یک برنامه به اشتراک بگذارند در حالی که نام کاربری، رمز عبور و سایر اطلاعات خود را خصوصی نگه می دارند. به عنوان مثال، یک برنامه می تواند از OAuth 2.0 برای دریافت مجوز از کاربران برای ذخیره فایل ها در Google Drives خود استفاده کند.
برنامههای نصبشده در دستگاههای جداگانه توزیع میشوند و فرض بر این است که این برنامهها نمیتوانند اسرار را حفظ کنند. وقتی کاربر در برنامه حضور دارد یا زمانی که برنامه در پسزمینه اجرا میشود، میتوانند به Google API دسترسی داشته باشند.
این جریان مجوز مشابه جریان مورد استفاده برای برنامه های کاربردی وب سرور است. تفاوت اصلی این است که برنامه های نصب شده باید مرورگر سیستم را باز کنند و یک URI تغییر مسیر محلی برای رسیدگی به پاسخ ها از سرور مجوز Google ارائه دهند.
جایگزین ها
برای برنامههای تلفن همراه، ممکن است ترجیح دهید از Google Sign-in برای Android یا iOS استفاده کنید. کتابخانههای سرویس گیرنده Google Sign-in، احراز هویت و مجوز کاربر را کنترل میکنند، و ممکن است اجرای آنها سادهتر از پروتکل سطح پایینتر توضیح داده شده در اینجا باشد.
برای برنامههایی که روی دستگاههایی اجرا میشوند که از مرورگر سیستم پشتیبانی نمیکنند یا دارای قابلیتهای ورودی محدودی هستند، مانند تلویزیونها، کنسولهای بازی، دوربینها یا چاپگرها، به OAuth 2.0 برای تلویزیونها و دستگاهها یا ورود به سیستم در تلویزیونها و دستگاههای ورودی محدود مراجعه کنید.
کتابخانه ها و نمونه ها
ما کتابخانه ها و نمونه های زیر را برای کمک به پیاده سازی جریان OAuth 2.0 که در این سند توضیح داده شده است، توصیه می کنیم:
پیش نیازها
API ها را برای پروژه خود فعال کنید
هر برنامهای که Google API را فراخوانی میکند، باید آن APIها را در آن فعال کند API Console.
برای فعال کردن یک API برای پروژه خود:
- Open the API Library در Google API Console.
- If prompted, select a project, or create a new one.
- را API Library همه API های موجود را فهرست می کند که بر اساس خانواده محصول و محبوبیت گروه بندی شده اند. اگر API که میخواهید فعال کنید در لیست قابل مشاهده نیست، از جستجو برای پیدا کردن آن استفاده کنید یا روی مشاهده همه در خانواده محصولی که به آن تعلق دارد کلیک کنید.
- API را که می خواهید فعال کنید انتخاب کنید، سپس روی دکمه Enable کلیک کنید.
- If prompted, enable billing.
- If prompted, read and accept the API's Terms of Service.
اعتبارنامه مجوز ایجاد کنید
هر برنامهای که از OAuth 2.0 برای دسترسی به APIهای Google استفاده میکند، باید دارای اعتبارنامه مجوز باشد که برنامه را در سرور OAuth 2.0 Google شناسایی کند. مراحل زیر نحوه ایجاد اعتبار برای پروژه خود را توضیح می دهد. سپس برنامه های شما می توانند از اعتبارنامه ها برای دسترسی به API هایی که برای آن پروژه فعال کرده اید استفاده کنند.
- Go to the Credentials page.
- روی ایجاد مشتری کلیک کنید.
- بخشهای زیر انواع سرویس گیرندههایی را که سرور مجوز Google پشتیبانی میکند، توضیح میدهد. نوع سرویس گیرنده ای را که برای برنامه شما توصیه می شود انتخاب کنید، کلاینت OAuth خود را نام ببرید و فیلدهای دیگر را در فرم مناسب تنظیم کنید.
اندروید
- نوع برنامه اندروید را انتخاب کنید.
- یک نام برای مشتری OAuth وارد کنید. این نام در پروژه شما نمایش داده می شود برای شناسایی مشتری
- نام بسته برنامه اندروید خود را وارد کنید. این مقدار در ویژگی
package
عنصر<manifest>
در فایل مانیفست برنامه شما تعریف شده است. - اثر انگشت گواهی امضای SHA-1 توزیع برنامه را وارد کنید.
- اگر برنامه شما از امضای برنامه توسط Google Play استفاده میکند، اثر انگشت SHA-1 را از صفحه امضای برنامه کنسول Play کپی کنید.
- اگر ذخیره کلید و کلیدهای امضای خود را مدیریت میکنید، از ابزار ابزار کلید موجود در جاوا برای چاپ اطلاعات گواهی در قالبی قابل خواندن برای انسان استفاده کنید. مقدار
SHA1
را در بخشCertificate fingerprints
در خروجی ابزار کلید کپی کنید. برای اطلاعات بیشتر به تأیید اعتبار مشتری خود در اسناد Google APIs for Android مراجعه کنید.
- (اختیاری) مالکیت برنامه Android خود را تأیید کنید .
- روی ایجاد کلیک کنید.
iOS
- نوع برنامه iOS را انتخاب کنید.
- یک نام برای مشتری OAuth وارد کنید. این نام در پروژه شما نمایش داده می شود برای شناسایی مشتری
- شناسه بسته نرم افزاری خود را وارد کنید. شناسه بسته مقدار کلید CFBundleIdentifier در فایل منبع فهرست دارایی اطلاعات برنامه شما ( info.plist ) است. این مقدار معمولاً در قسمت General یا پانل Signing & Capabilities در ویرایشگر پروژه Xcode نمایش داده می شود. شناسه بسته نرم افزاری همچنین در بخش اطلاعات عمومی صفحه اطلاعات برنامه برای برنامه در سایت App Store Connect Apple نمایش داده می شود.
تأیید کنید که از شناسه بسته نرم افزاری درستی برای برنامه خود استفاده می کنید، زیرا اگر از ویژگی بررسی برنامه استفاده می کنید، نمی توانید آن را تغییر دهید.
- (اختیاری)
اگر برنامه در اپ استور اپل منتشر شده است، شناسه App Store برنامه خود را وارد کنید. شناسه فروشگاه یک رشته عددی است که در هر URL اپ استور اپل وجود دارد.
- برنامه Apple App Store را در دستگاه iOS یا iPadOS خود باز کنید.
- برنامه خود را جستجو کنید.
- دکمه اشتراک گذاری (نماد مربع و فلش رو به بالا) را انتخاب کنید.
- کپی پیوند را انتخاب کنید.
- پیوند را در یک ویرایشگر متن قرار دهید. شناسه فروشگاه App آخرین قسمت URL است.
مثال:
https://apps.apple.com/app/google/id 284815942
- (اختیاری)
شناسه تیم خود را وارد کنید. برای اطلاعات بیشتر به شناسه تیم خود در مستندات حساب توسعه دهنده اپل مراجعه کنید.
توجه: اگر برنامه بررسی را برای مشتری خود فعال می کنید، فیلد Team ID ضروری است. - (اختیاری)
بررسی برنامه را برای برنامه iOS خود فعال کنید. وقتی App Check را فعال میکنید، از سرویس App Attest Apple برای تأیید اینکه درخواستهای OAuth 2.0 از مشتری OAuth شما واقعی هستند و از برنامه شما میآیند استفاده میشود. این به کاهش خطر جعل هویت اپلیکیشن کمک می کند. درباره فعال کردن App Check برای برنامه iOS خود بیشتر بیاموزید .
- روی ایجاد کلیک کنید.
UWP
- نوع برنامه Universal Windows Platform را انتخاب کنید.
- یک نام برای مشتری OAuth وارد کنید. این نام در پروژه شما نمایش داده می شود برای شناسایی مشتری
- شناسه فروشگاه مایکروسافت 12 کاراکتری برنامه خود را وارد کنید. می توانید این مقدار را در Microsoft Partner Center در صفحه هویت برنامه در بخش مدیریت برنامه پیدا کنید.
- روی ایجاد کلیک کنید.
برای برنامههای UWP، طرح URI سفارشی نمیتواند بیشتر از 39 کاراکتر باشد.
محدوده های دسترسی را شناسایی کنید
Scopes به برنامه شما امکان میدهد فقط درخواست دسترسی به منابع مورد نیاز خود را داشته باشد در حالی که کاربران را قادر میسازد تا میزان دسترسی را که به برنامه شما میدهند کنترل کنند. بنابراین، ممکن است بین تعداد دامنه های درخواستی و احتمال کسب رضایت کاربر رابطه معکوس وجود داشته باشد.
قبل از شروع اجرای مجوز OAuth 2.0، توصیه میکنیم محدودههایی را که برنامه شما برای دسترسی به آنها نیاز به مجوز دارد، شناسایی کنید.
سند OAuth 2.0 API Scopes حاوی فهرست کاملی از حوزههایی است که ممکن است برای دسترسی به Google API از آنها استفاده کنید.
دریافت توکن های دسترسی OAuth 2.0
مراحل زیر نشان می دهد که چگونه برنامه شما با سرور OAuth 2.0 Google برای کسب رضایت کاربر برای انجام یک درخواست API از طرف کاربر تعامل دارد. برنامه شما قبل از اینکه بتواند یک درخواست Google API را که نیاز به مجوز کاربر دارد، اجرا کند، باید این رضایت را داشته باشد.
مرحله 1: یک تأیید کننده کد و چالش ایجاد کنید
Google از پروتکل Proof Key for Code Exchange (PKCE) پشتیبانی می کند تا جریان برنامه نصب شده را ایمن تر کند. برای هر درخواست مجوز یک تأیید کننده کد منحصر به فرد ایجاد می شود و مقدار تبدیل شده آن به نام "code_challenge" برای دریافت کد مجوز به سرور مجوز ارسال می شود.
تایید کننده کد را ایجاد کنید
یک code_verifier
یک رشته رمزنگاری تصادفی با آنتروپی بالا با استفاده از کاراکترهای رزرو نشده [AZ] / [az] / [0-9] / "-" / " است. / "_" / "~"، با حداقل طول 43 کاراکتر و حداکثر طول 128 کاراکتر.
تأیید کننده کد باید آنتروپی کافی داشته باشد تا حدس زدن مقدار را غیرعملی کند.
چالش کد را ایجاد کنید
دو روش ایجاد چالش کد پشتیبانی می شود.
روشهای ایجاد چالش کد | |
---|---|
S256 (توصیه می شود) | چالش کد، هش SHA256 کدگذاری شده Base64URL (بدون بالشتک) تأییدکننده کد است.
|
ساده | چالش کد همان مقدار تأیید کننده کد تولید شده در بالا است.
|
مرحله 2: درخواستی را به سرور OAuth 2.0 Google ارسال کنید
برای دریافت مجوز کاربر، درخواستی را به سرور مجوز Google در https://accounts.google.com/o/oauth2/v2/auth
ارسال کنید. این نقطه پایانی جستجوی جلسه فعال را کنترل می کند، کاربر را احراز هویت می کند و رضایت کاربر را دریافت می کند. نقطه پایانی فقط از طریق SSL قابل دسترسی است و از اتصالات HTTP (غیر SSL) خودداری می کند.
سرور مجوز از پارامترهای رشته کوئری زیر برای برنامه های نصب شده پشتیبانی می کند:
پارامترها | |||||||
---|---|---|---|---|---|---|---|
client_id | مورد نیاز شناسه مشتری برای برنامه شما. شما می توانید این مقدار را در . | ||||||
redirect_uri | مورد نیاز نحوه ارسال پاسخ سرور مجوز Google به برنامه شما را تعیین می کند. چندین گزینه تغییر مسیر برای برنامه های نصب شده وجود دارد، و شما اعتبار مجوز خود را با روش تغییر مسیر خاصی در ذهن تنظیم خواهید کرد. مقدار باید دقیقاً با یکی از URIهای مجاز تغییر مسیر برای مشتری OAuth 2.0 مطابقت داشته باشد که در مشتری خود پیکربندی کرده اید. . اگر این مقدار با یک URI مجاز مطابقت نداشته باشد، یک خطای جدول زیر مقدار پارامتر
| ||||||
response_type | مورد نیاز تعیین می کند که آیا نقطه پایانی Google OAuth 2.0 یک کد مجوز را برمی گرداند یا خیر. مقدار پارامتر را روی | ||||||
scope | مورد نیاز فهرستی از محدودههای محدود شده با فضا که منابعی را که برنامه شما میتواند از طرف کاربر به آنها دسترسی داشته باشد، شناسایی میکند. این مقادیر صفحه رضایتی را که Google به کاربر نشان می دهد، نشان می دهد. Scopes به برنامه شما امکان میدهد فقط درخواست دسترسی به منابع مورد نیاز خود را داشته باشد در حالی که کاربران را قادر میسازد تا میزان دسترسی را که به برنامه شما میدهند کنترل کنند. بنابراین، بین تعداد دامنه های درخواستی و احتمال کسب رضایت کاربر رابطه معکوس وجود دارد. | ||||||
code_challenge | توصیه می شود یک | ||||||
code_challenge_method | توصیه می شود مشخص می کند که از چه روشی برای رمزگذاری یک | ||||||
state | توصیه می شود هر مقدار رشته ای را که برنامه شما برای حفظ وضعیت بین درخواست مجوز شما و پاسخ سرور مجوز استفاده می کند، مشخص می کند. سرور مقدار دقیقی را که شما به عنوان یک جفت شما می توانید از این پارامتر برای اهداف مختلفی مانند هدایت کاربر به منبع صحیح در برنامه خود، ارسال nonces و کاهش جعل درخواست بین سایتی استفاده کنید. از آنجایی که | ||||||
login_hint | اختیاری اگر برنامه شما بداند که کدام کاربر در حال تلاش برای احراز هویت است، میتواند از این پارامتر برای ارائه راهنمایی به سرور احراز هویت Google استفاده کند. سرور از راهنمایی برای ساده سازی جریان ورود استفاده می کند یا با پر کردن فیلد ایمیل در فرم ورود به سیستم یا با انتخاب جلسه چند ورود مناسب. مقدار پارامتر را به آدرس ایمیل یا شناسه |
نمونه URL های مجوز
برگه های زیر نمونه URL های مجوز را برای گزینه های مختلف URI تغییر مسیر نشان می دهد.
URL ها به جز مقدار پارامتر redirect_uri
یکسان هستند. URL ها همچنین حاوی پارامترهای response_type
و client_id
مورد نیاز و همچنین پارامتر state
اختیاری هستند. هر URL حاوی خطوط شکسته و فاصله برای خوانایی است.
طرح URI سفارشی
https://accounts.google.com/o/oauth2/v2/auth? scope=email%20profile& response_type=code& state=security_token%3D138r5719ru3e1%26url%3Dhttps%3A%2F%2Foauth2.example.com%2Ftoken& redirect_uri=com.example.app%3A/oauth2redirect& client_id=client_id
آدرس IP Loopback
https://accounts.google.com/o/oauth2/v2/auth? scope=email%20profile& response_type=code& state=security_token%3D138r5719ru3e1%26url%3Dhttps%3A%2F%2Foauth2.example.com%2Ftoken& redirect_uri=http%3A//127.0.0.1%3A9004& client_id=client_id
مرحله 3: گوگل رضایت کاربر را درخواست می کند
در این مرحله، کاربر تصمیم می گیرد که آیا به اپلیکیشن شما دسترسی درخواستی را بدهد یا خیر. در این مرحله، گوگل یک پنجره رضایت را نمایش می دهد که نام برنامه شما و سرویس های Google API را نشان می دهد که درخواست اجازه دسترسی به آن ها را با اعتبار مجوز کاربر و خلاصه ای از محدوده های دسترسی که باید اعطا شود را نشان می دهد. سپس کاربر می تواند با اعطای دسترسی به یک یا چند حوزه درخواست شده توسط برنامه شما موافقت کند یا درخواست را رد کند.
برنامه شما در این مرحله نیازی به انجام هیچ کاری ندارد زیرا منتظر پاسخ سرور OAuth 2.0 Google است که نشان می دهد آیا دسترسی اعطا شده است یا خیر. این پاسخ در مرحله زیر توضیح داده شده است.
خطاها
درخواستها به نقطه پایانی مجوز OAuth 2.0 Google ممکن است پیامهای خطای کاربر را بهجای جریانهای احراز هویت و مجوز مورد انتظار نمایش دهند. کدهای خطای رایج و راهکارهای پیشنهادی در زیر فهرست شدهاند.
admin_policy_enforced
حساب Google به دلیل خطمشیهای سرپرست Google Workspace نمیتواند یک یا چند محدوده درخواستی را تأیید کند. برای اطلاعات بیشتر در مورد اینکه چگونه یک سرپرست میتواند دسترسی به همه حوزهها یا محدودههای حساس و محدود را تا زمانی که صراحتاً به شناسه مشتری OAuth شما اعطا نشود، به مقاله راهنمای Google Workspace Admin مراجعه کنید.
disallowed_useragent
نقطه پایانی مجوز در داخل یک عامل کاربر تعبیهشده نمایش داده میشود که توسط خطمشیهای OAuth 2.0 Google مجاز نیست.
اندروید
برنامهنویسان Android ممکن است هنگام باز کردن درخواستهای مجوز درandroid.webkit.WebView
با این پیام خطا مواجه شوند. توسعهدهندگان باید در عوض از کتابخانههای Android مانند Google Sign-In برای Android یا OpenID Foundation's AppAuth for Android استفاده کنند.
توسعه دهندگان وب ممکن است زمانی با این خطا مواجه شوند که یک برنامه Android یک پیوند وب کلی را در یک عامل کاربر تعبیه شده باز کند و کاربر به نقطه پایانی مجوز OAuth 2.0 Google از سایت شما هدایت شود. توسعهدهندگان باید اجازه دهند پیوندهای عمومی در کنترلکننده پیوند پیشفرض سیستمعامل، که شامل کنترلکنندههای پیوندهای برنامه Android یا برنامه مرورگر پیشفرض است، باز شوند. کتابخانه Android Custom Tabs نیز یک گزینه پشتیبانی شده است.
iOS
توسعه دهندگان iOS و macOS ممکن است هنگام باز کردن درخواست های مجوز درWKWebView
با این خطا مواجه شوند. توسعهدهندگان باید در عوض از کتابخانههای iOS مانند Google Sign-In برای iOS یا OpenID Foundations AppAuth برای iOS استفاده کنند.
زمانی که یک برنامه iOS یا macOS یک پیوند وب عمومی را در یک عامل کاربر تعبیه شده باز می کند و کاربر به نقطه پایانی مجوز OAuth 2.0 Google از سایت شما می رود، ممکن است توسعه دهندگان وب با این خطا مواجه شوند. توسعهدهندگان باید اجازه دهند پیوندهای عمومی در کنترلکننده پیوند پیشفرض سیستمعامل، که شامل کنترلکنندههای پیوندهای جهانی یا برنامه مرورگر پیشفرض است، باز شوند. کتابخانهSFSafariViewController
نیز یک گزینه پشتیبانی شده است.
org_internal
شناسه مشتری OAuth در درخواست بخشی از پروژه ای است که دسترسی به حساب های Google را در یک سازمان Google Cloud خاص محدود می کند. برای اطلاعات بیشتر در مورد این گزینه پیکربندی، بخش نوع کاربر را در مقاله راهنمای تنظیم صفحه رضایت OAuth خود ببینید.
invalid_grant
اگر از تأیید کننده کد و چالش استفاده می کنید، پارامتر code_callenge
نامعتبر است یا وجود ندارد. مطمئن شوید که پارامتر code_challenge
به درستی تنظیم شده است.
هنگام بازخوانی نشانه دسترسی ، نشانه ممکن است منقضی شده باشد یا باطل شده باشد. مجدداً کاربر را احراز هویت کنید و برای دریافت توکن های جدید رضایت کاربر را بخواهید. اگر همچنان این خطا را مشاهده می کنید، مطمئن شوید که برنامه شما به درستی پیکربندی شده است و از نشانه ها و پارامترهای صحیح در درخواست خود استفاده می کنید. در غیر این صورت، حساب کاربری ممکن است حذف یا غیرفعال شده باشد.
redirect_uri_mismatch
redirect_uri
ارسال شده در درخواست مجوز با URI تغییر مسیر مجاز برای شناسه مشتری OAuth مطابقت ندارد. URIهای مجاز تغییر مسیر را در .
redirect_uri
ارسال شده ممکن است برای نوع مشتری نامعتبر باشد.
پارامتر redirect_uri
ممکن است به جریان OAuth خارج از باند (OOB) اشاره داشته باشد که منسوخ شده است و دیگر پشتیبانی نمی شود. برای به روز رسانی ادغام خود به راهنمای مهاجرت مراجعه کنید.
invalid_request
مشکلی در درخواستی که دادید وجود داشت. این می تواند به دلایل مختلفی باشد:
- درخواست به درستی قالب بندی نشده بود
- درخواست فاقد پارامترهای لازم بود
- این درخواست از روش مجوزی استفاده میکند که Google از آن پشتیبانی نمیکند. بررسی کنید که ادغام OAuth شما از یک روش ادغام توصیه شده استفاده می کند
- یک طرح سفارشی برای تغییر مسیر uri استفاده میشود: اگر پیام خطا را مشاهده کردید که طرح URI سفارشی در برنامههای Chrome پشتیبانی نمیشود یا طرح URI سفارشی برای مشتری Android شما فعال نیست ، به این معنی است که از یک طرح URI سفارشی استفاده میکنید که در برنامههای Chrome پشتیبانی نمیشود و بهطور پیشفرض در Android غیرفعال است. درباره جایگزین های طرح URI سفارشی بیشتر بیاموزید
مرحله 4: پاسخ سرور OAuth 2.0 را مدیریت کنید
روشی که برنامه شما پاسخ مجوز را دریافت می کند به طرح URI تغییر مسیری که استفاده می کند بستگی دارد. صرف نظر از طرح، پاسخ شامل یک کد مجوز ( code
) یا یک خطا ( error
) خواهد بود. برای مثال error=access_denied
نشان می دهد که کاربر درخواست را رد کرده است.
اگر کاربر اجازه دسترسی به برنامه شما را بدهد، میتوانید کد مجوز را با یک نشانه دسترسی و یک نشانه تازهسازی، همانطور که در مرحله بعد توضیح داده شد، مبادله کنید.
مرحله 5: کد مجوز را برای بهروزرسانی و دسترسی به توکنها مبادله کنید
برای تبادل کد مجوز برای یک نشانه دسترسی، با https://oauth2.googleapis.com/token
endpoint تماس بگیرید و پارامترهای زیر را تنظیم کنید:
فیلدها | |
---|---|
client_id | شناسه مشتری به دست آمده از . |
client_secret | راز مشتری به دست آمده از . |
code | کد مجوز از درخواست اولیه بازگردانده شد. |
code_verifier | تأیید کننده کدی که در مرحله 1 ایجاد کردید. |
grant_type | همانطور که در مشخصات OAuth 2.0 تعریف شده است ، مقدار این فیلد باید روی authorization_code تنظیم شود. |
redirect_uri | یکی از URI های تغییر مسیر که برای پروژه شما در فهرست فهرست شده است برای client_id داده شده. |
قطعه زیر یک نمونه درخواست را نشان می دهد:
POST /token HTTP/1.1 Host: oauth2.googleapis.com Content-Type: application/x-www-form-urlencoded code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7& client_id=your_client_id& client_secret=your_client_secret& redirect_uri=http://127.0.0.1:9004& grant_type=authorization_code
Google به این درخواست با برگرداندن یک شی JSON که حاوی یک نشانه دسترسی کوتاه مدت و یک نشانه تازهسازی است، پاسخ میدهد.
پاسخ شامل فیلدهای زیر است:
فیلدها | |
---|---|
access_token | رمزی که برنامه شما برای تأیید درخواست Google API ارسال می کند. |
expires_in | طول عمر باقیمانده رمز دسترسی در ثانیه. |
id_token | توجه: این ویژگی تنها در صورتی بازگردانده میشود که درخواست شما شامل محدوده هویتی مانند openid ، profile یا email باشد. مقدار یک رمز وب JSON (JWT) است که حاوی اطلاعات هویتی با امضای دیجیتالی درباره کاربر است. |
refresh_token | توکنی که می توانید از آن برای به دست آوردن یک نشانه دسترسی جدید استفاده کنید. نشانههای تازهسازی تا زمانی که کاربر دسترسی را لغو نکند یا نشانهی تازهسازی منقضی شود معتبر هستند. توجه داشته باشید که نشانه های تازه سازی همیشه برای برنامه های نصب شده برگردانده می شوند. |
refresh_token_expires_in | طول عمر باقیمانده نشانه رفرش در ثانیه. این مقدار فقط زمانی تنظیم میشود که کاربر دسترسی مبتنی بر زمان را اعطا کند. |
scope | دامنه دسترسی اعطا شده توسط access_token به صورت لیستی از رشته های حساس به حروف کوچک و کوچک با فاصله بیان می شود. |
token_type | نوع رمز برگشتی. در این زمان، مقدار این فیلد همیشه روی Bearer تنظیم می شود. |
قطعه زیر یک نمونه پاسخ را نشان می دهد:
{ "access_token": "1/fFAGRNJru1FTz70BzhT3Zg", "expires_in": 3920, "token_type": "Bearer", "scope": "https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/calendar.readonly", "refresh_token": "1//xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI" }
مرحله 6: بررسی کنید که کاربران چه محدوده هایی را اعطا کرده اند
هنگام درخواست چندین مجوز (محدوده)، کاربران ممکن است به برنامه شما اجازه دسترسی به همه آنها را ندهند. برنامه شما باید بررسی کند که کدام حوزهها واقعاً اعطا شدهاند و موقعیتهایی را که در آن برخی از مجوزها رد میشوند، بهخوبی مدیریت کند، معمولاً با غیرفعال کردن ویژگیهایی که به آن محدودههای رد شده متکی هستند.
با این حال، استثناهایی وجود دارد. برنامههای Google Workspace Enterprise با تفویض اختیار در سطح دامنه ، یا برنامههایی که بهعنوان «معتمد» علامتگذاری شدهاند، صفحه رضایت مجوزهای جزئی را دور میزنند. برای این برنامهها، کاربران صفحه رضایت جزئیات را نمیبینند. در عوض، برنامه شما یا همه محدوده های درخواستی را دریافت می کند یا هیچ کدام را دریافت نمی کند.
برای اطلاعات دقیق تر، به نحوه رسیدگی به مجوزهای گرانول مراجعه کنید.
برای بررسی اینکه آیا کاربر به برنامه شما اجازه دسترسی به یک محدوده خاص را داده است یا خیر، فیلد scope
را در پاسخ نشانه دسترسی بررسی کنید. دامنه دسترسی اعطا شده توسط access_token به صورت لیستی از رشته های حساس به حروف کوچک و کوچک با فاصله بیان می شود.
به عنوان مثال، نمونه پاسخ نشانه دسترسی زیر نشان می دهد که کاربر به برنامه شما اجازه دسترسی به فعالیت Drive فقط خواندنی و رویدادهای تقویم را داده است:
{ "access_token": "1/fFAGRNJru1FTz70BzhT3Zg", "expires_in": 3920, "token_type": "Bearer", "scope": "https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/calendar.readonly", "refresh_token": "1//xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI" }
فراخوانی Google API
پس از اینکه برنامه شما یک نشانه دسترسی به دست آورد، در صورتی که دامنه دسترسی مورد نیاز توسط API اعطا شده باشد، می توانید از این رمز برای برقراری تماس با Google API از طرف یک حساب کاربری خاص استفاده کنید. برای انجام این کار، توکن دسترسی را با درج یک پارامتر query access_token
یا یک مقدار Authorization
HTTP header Bearer
در درخواست API قرار دهید. در صورت امکان، هدر HTTP ترجیح داده می شود، زیرا رشته های پرس و جو در گزارش های سرور قابل مشاهده هستند. در بیشتر موارد میتوانید از کتابخانه سرویس گیرنده برای تنظیم تماسهای خود با Google API استفاده کنید (به عنوان مثال، هنگام تماس با Drive Files API ).
میتوانید همه APIهای Google را امتحان کنید و دامنه آنها را در OAuth 2.0 Playground مشاهده کنید.
نمونه های HTTP GET
تماس با نقطه پایانی drive.files
(API فایلهای Drive) با استفاده از هدر HTTP Authorization: Bearer
ممکن است به شکل زیر باشد. توجه داشته باشید که باید رمز دسترسی خود را مشخص کنید:
GET /drive/v2/files HTTP/1.1 Host: www.googleapis.com Authorization: Bearer access_token
در اینجا یک فراخوانی به همان API برای کاربر تأیید شده با استفاده از پارامتر رشته query access_token
وجود دارد:
GET https://www.googleapis.com/drive/v2/files?access_token=access_token
نمونه های curl
می توانید این دستورات را با برنامه خط فرمان curl
آزمایش کنید. در اینجا یک مثال است که از گزینه هدر HTTP (ترجیح) استفاده می کند:
curl -H "Authorization: Bearer access_token" https://www.googleapis.com/drive/v2/files
یا، گزینه پارامتر query string:
curl https://www.googleapis.com/drive/v2/files?access_token=access_token
در حال بازخوانی نشانه دسترسی
توکنهای دسترسی بهصورت دورهای منقضی میشوند و برای یک درخواست API مرتبط به اعتبارنامههای نامعتبر تبدیل میشوند. اگر درخواست دسترسی آفلاین به محدودههای مرتبط با رمز را داشته باشید، میتوانید یک نشانه دسترسی را بدون درخواست اجازه از کاربر (از جمله زمانی که کاربر حضور ندارد) بازخوانی کنید.
برای تازه کردن یک نشانه دسترسی، برنامه شما یک درخواست HTTPS POST
به سرور مجوز Google ( https://oauth2.googleapis.com/token
) ارسال می کند که شامل پارامترهای زیر است:
فیلدها | |
---|---|
client_id | شناسه مشتری به دست آمده از API Console. |
client_secret | راز مشتری به دست آمده از API Console. ( client_secret برای درخواستهای مشتریانی که به عنوان برنامههای Android، iOS یا Chrome ثبت شدهاند، قابل اجرا نیست.) |
grant_type | همانطور که در مشخصات OAuth 2.0 تعریف شده است ، مقدار این فیلد باید روی refresh_token تنظیم شود. |
refresh_token | رمز بهروزرسانی از تبادل کد مجوز بازگشت. |
قطعه زیر یک نمونه درخواست را نشان می دهد:
POST /token HTTP/1.1 Host: oauth2.googleapis.com Content-Type: application/x-www-form-urlencoded client_id=your_client_id& client_secret=your_client_secret& refresh_token=refresh_token& grant_type=refresh_token
تا زمانی که کاربر دسترسی اعطا شده به برنامه را لغو نکرده باشد، سرور توکن یک شی JSON را که حاوی یک نشانه دسترسی جدید است برمی گرداند. قطعه زیر یک نمونه پاسخ را نشان می دهد:
{ "access_token": "1/fFAGRNJru1FTz70BzhT3Zg", "expires_in": 3920, "scope": "https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/calendar.readonly", "token_type": "Bearer" }
توجه داشته باشید که محدودیتهایی در تعداد نشانههای تازهسازی صادر شده وجود دارد. یک محدودیت برای هر ترکیب کلاینت/کاربر، و دیگری برای هر کاربر در همه مشتریان. شما باید توکنهای تازهسازی را در فضای ذخیرهسازی طولانیمدت ذخیره کنید و تا زمانی که معتبر هستند به استفاده از آنها ادامه دهید. اگر برنامه شما توکنهای بهروزرسانی بیش از حد درخواست کند، ممکن است با این محدودیتها مواجه شود، در این صورت توکنهای تازهسازی قدیمیتر کار نمیکنند.
باطل کردن یک نشانه
در برخی موارد ممکن است کاربر بخواهد دسترسی داده شده به یک برنامه را لغو کند. کاربر میتواند با مراجعه به تنظیمات حساب، دسترسی را لغو کند. برای اطلاعات بیشتر به بخش حذف دسترسی به سایت یا برنامه از سایت ها و برنامه های شخص ثالث با دسترسی به سند پشتیبانی حساب خود مراجعه کنید.
همچنین این امکان وجود دارد که یک برنامه به صورت برنامه نویسی دسترسی داده شده به آن را لغو کند. لغو برنامهای در مواردی مهم است که کاربر اشتراک خود را لغو میکند، برنامهای را حذف میکند یا منابع API مورد نیاز یک برنامه به طور قابل توجهی تغییر کرده است. به عبارت دیگر، بخشی از فرآیند حذف می تواند شامل یک درخواست API برای اطمینان از حذف مجوزهایی باشد که قبلاً به برنامه اعطا شده است.
برای لغو برنامهای یک نشانه، برنامه شما درخواستی به https://oauth2.googleapis.com/revoke
میکند و توکن را به عنوان یک پارامتر شامل میشود:
curl -d -X -POST --header "Content-type:application/x-www-form-urlencoded" \ https://oauth2.googleapis.com/revoke?token={token}
توکن می تواند یک نشانه دسترسی یا یک نشانه تازه سازی باشد. اگر توکن یک نشانه دسترسی باشد و دارای یک نشانه رفرش متناظر باشد، توکن رفرش نیز باطل می شود.
اگر ابطال با موفقیت پردازش شود، کد وضعیت HTTP پاسخ 200
است. برای شرایط خطا، کد وضعیت HTTP 400
به همراه یک کد خطا برگردانده می شود.
روش های تغییر مسیر برنامه
طرح URI سفارشی (اندروید، iOS، UWP)
طرحهای URI سفارشی نوعی پیوند عمیق هستند که از یک طرح سفارشی تعریف شده برای باز کردن برنامه شما استفاده میکنند.
جایگزینی برای استفاده از طرح های URI سفارشی در اندروید
از Google Sign-In برای Android SDK استفاده کنید که پاسخ OAuth 2.0 را مستقیماً به برنامه شما ارائه می دهد و نیاز به URI تغییر مسیر را از بین می برد.
نحوه مهاجرت به Google Sign-In for Android SDK
اگر از یک طرح سفارشی برای ادغام OAuth خود در Android استفاده می کنید، باید اقدامات زیر را انجام دهید تا به طور کامل به استفاده از Google Sign-In توصیه شده برای Android SDK مهاجرت کنید:
- کد خود را برای استفاده از Google Sign-In SDK به روز کنید.
- پشتیبانی از طرح سفارشی را در Google API Console غیرفعال کنید.
مراحل زیر را برای انتقال به Google Sign-In Android SDK دنبال کنید:
- کد خود را برای استفاده از Google Sign-In Android SDK به روز کنید:
- کد خود را بررسی کنید تا مشخص کنید کجا درخواستی را به سرور OAuth 2.0 Google ارسال می کنید . اگر از یک طرح سفارشی استفاده می کنید، درخواست شما به شکل زیر خواهد بود:
https://accounts.google.com/o/oauth2/v2/auth? scope=<SCOPES>& response_type=code& &state=<STATE>& redirect_uri=com.example.app:/oauth2redirect& client_id=<CLIENT_ID>
com.example.app:/oauth2redirect
URI تغییر مسیر طرح سفارشی در مثال بالا است. برای جزئیات بیشتر در مورد قالب مقدار طرح URI سفارشی، به تعریف پارامترredirect_uri
مراجعه کنید. - پارامترهای
scope
وclient_id
درخواست را که برای پیکربندی Google Sign-In SDK نیاز دارید، یادداشت کنید. - برای راهاندازی SDK، دستورالعملهای Start Integrating Google Sign-In را در برنامه Android خود دنبال کنید. میتوانید از مرحله دریافت شناسه مشتری OAuth 2.0 سرور باطن خود صرفنظر کنید، همانطور که از
client_id
که از مرحله قبل بازیابی کردهاید دوباره استفاده میکنید. - دستورالعملهای دسترسی به API سمت سرور را دنبال کنید. این شامل مراحل زیر است:
- از روش
getServerAuthCode
برای بازیابی یک کد اعتبار برای محدوده هایی که درخواست مجوز می کنید استفاده کنید. - کد احراز هویت را به پشتیبان برنامه خود ارسال کنید تا آن را با رمز دسترسی و بازخوانی مبادله کنید.
- از نشانه دسترسی بازیابی شده برای برقراری تماس با Google API از طرف کاربر استفاده کنید.
- از روش
- کد خود را بررسی کنید تا مشخص کنید کجا درخواستی را به سرور OAuth 2.0 Google ارسال می کنید . اگر از یک طرح سفارشی استفاده می کنید، درخواست شما به شکل زیر خواهد بود:
- غیرفعال کردن پشتیبانی از طرح سفارشی در Google API Console:
- به لیست اعتبارنامه OAuth 2.0 بروید و کلاینت Android خود را انتخاب کنید.
- به بخش تنظیمات پیشرفته بروید، تیک گزینه Enable Custom URI Scheme را بردارید و برای غیرفعال کردن پشتیبانی از طرح URI سفارشی، روی Save کلیک کنید.
طرح URI سفارشی را فعال کنید
اگر جایگزین توصیه شده برای شما کار نمی کند، می توانید با دنبال کردن دستورالعمل های زیر، طرح های URI سفارشی را برای مشتری Android خود فعال کنید:- به لیست اعتبارنامه OAuth 2.0 بروید و کلاینت Android خود را انتخاب کنید.
- به بخش Advanced Settings بروید، چک باکس Enable Custom URI Scheme را علامت بزنید و Save را کلیک کنید تا پشتیبانی از طرح URI سفارشی فعال شود.
جایگزینی برای استفاده از طرحهای URI سفارشی در برنامههای Chrome
از Chrome Identity API استفاده کنید که پاسخ OAuth 2.0 را مستقیماً به برنامه شما ارائه می دهد و نیاز به URI تغییر مسیر را از بین می برد.
آدرس IP Loopback (macOS، Linux، Windows desktop)
برای دریافت کد مجوز با استفاده از این URL، برنامه شما باید در وب سرور محلی در حال گوش دادن باشد. این در بسیاری از پلتفرم ها، اما نه همه، امکان پذیر است. با این حال، اگر پلت فرم شما از آن پشتیبانی می کند، این مکانیسم توصیه شده برای دریافت کد مجوز است.
هنگامی که برنامه شما پاسخ مجوز را دریافت می کند، برای بهترین قابلیت استفاده باید با نمایش یک صفحه HTML پاسخ دهد که به کاربر دستور می دهد مرورگر را ببندد و به برنامه شما بازگردد.
استفاده توصیه شده | برنامههای دسکتاپ macOS، Linux و Windows (اما نه Universal Windows Platform). |
مقادیر فرم | نوع برنامه را روی برنامه دسکتاپ تنظیم کنید. |
کپی/پیست دستی (منسوخ شده)
از برنامه های خود محافظت کنید
تأیید مالکیت برنامه (اندروید، کروم)
برای کاهش خطر جعل هویت برنامه، می توانید مالکیت برنامه خود را تأیید کنید.
اندروید
برای تکمیل فرآیند تأیید ، در صورت داشتن یکی از حساب کاربری Google Play خود می توانید از حساب Google Play خود استفاده کنید و برنامه شما در کنسول Google Play ثبت شده است. الزامات زیر باید برای تأیید موفقیت آمیز برآورده شود:
- شما باید یک برنامه ثبت شده در کنسول Google Play با همان نام بسته بندی و اثر انگشت گواهی امضای SHA-1 به عنوان مشتری Android OAuth که در حال تکمیل تأیید هستید ، داشته باشید.
- شما باید در کنسول Google Play مجوز مدیر برنامه را داشته باشید. در مورد مدیریت دسترسی در کنسول Google Play اطلاعات بیشتری کسب کنید .
در بخش مالکیت برنامه Verify از مشتری Android ، روی دکمه تأیید مالکیت کلیک کنید تا فرآیند تأیید انجام شود.
اگر تأیید موفقیت آمیز باشد ، یک اعلان برای تأیید موفقیت فرایند تأیید نمایش داده می شود. در غیر این صورت ، یک خطای سریع نشان داده می شود.
برای رفع تأیید ناموفق ، موارد زیر را امتحان کنید:
- اطمینان حاصل کنید که برنامه ای که تأیید می کنید یک برنامه ثبت شده در کنسول Google Play است.
- اطمینان حاصل کنید که در کنسول Google Play مجوز مدیر برنامه را دارید.
کروم
برای تکمیل فرآیند تأیید ، از حساب توسعه دهنده فروشگاه وب Chrome خود استفاده می کنید. الزامات زیر باید برای تأیید موفقیت آمیز برآورده شود:
- شما باید یک مورد ثبت شده را در داشبورد توسعه دهنده فروشگاه Web Chrome با همان شناسه مورد به عنوان مشتری پسوند Chrome OAuth که در حال تکمیل تأیید هستید ، داشته باشید.
- شما باید یک ناشر برای کالای وب Chrome باشید. در مورد مدیریت دسترسی در داشبورد توسعه دهنده فروشگاه وب Chrome بیشتر بدانید .
در بخش مالکیت برنامه Verify از مشتری برنامه افزودنی Chrome ، روی دکمه تأیید مالکیت کلیک کنید تا روند تأیید انجام شود.
توجه: چند دقیقه قبل از تکمیل مراحل تأیید پس از دسترسی به حساب خود صبر کنید.
اگر تأیید موفقیت آمیز باشد ، یک اعلان برای تأیید موفقیت فرایند تأیید نمایش داده می شود. در غیر این صورت ، یک خطای سریع نشان داده می شود.
برای رفع تأیید ناموفق ، موارد زیر را امتحان کنید:
- اطمینان حاصل کنید که یک مورد ثبت شده در داشبورد توسعه دهنده Web Store Chrome با همان شناسه موردی به عنوان مشتری OAUTH CHROME OAUTH که در حال تکمیل تأیید هستید ، وجود دارد.
- اطمینان حاصل کنید که شما ناشر برنامه هستید ، یعنی شما باید ناشر فردی برنامه یا عضو ناشر گروه برنامه باشید. در مورد مدیریت دسترسی در داشبورد توسعه دهنده فروشگاه وب Chrome بیشتر بدانید .
- اگر به تازگی لیست ناشر گروه خود را به روز کرده اید ، تأیید کنید که لیست عضویت گروه ناشر در داشبورد توسعه دهنده فروشگاه وب Chrome همگام سازی شده است. در مورد همگام سازی لیست عضویت ناشر خود بیشتر بدانید .
بررسی برنامه (فقط iOS)
ویژگی چک برنامه به محافظت از برنامه های iOS شما از استفاده غیرمجاز با استفاده از برنامه تأیید شده اپل کمک می کند تا تأیید کند که درخواست های ساخته شده به نقاط پایانی Google OAUTH 2.0 از برنامه های معتبر شما سرچشمه می گیرد. این به کاهش خطر جعل هویت برنامه کمک می کند.
فعال کردن برنامه برای مشتری iOS خود را فعال کنید
برای فعال کردن موفقیت آمیز برنامه برای مشتری iOS باید موارد زیر را برآورده کنید:- شما باید شناسه تیم را برای مشتری iOS خود مشخص کنید.
- شما نباید از کارت وحشی در شناسه بسته نرم افزاری خود استفاده کنید زیرا می تواند بیش از یک برنامه حل شود. این بدان معنی است که شناسه بسته نرم افزاری نباید شامل نماد ستاره (*) باشد.
پس از فعال کردن بررسی برنامه ، شروع به دیدن معیارهای مربوط به درخواست های OAUTH از مشتری خود در نمای ویرایش مشتری OAUTH خواهید کرد. درخواست های منابع تأیید نشده تا زمانی که شما بررسی برنامه را اجرا کنید مسدود نمی شوند. اطلاعات موجود در صفحه نظارت بر معیارها می تواند به شما در تعیین زمان شروع اجرای کمک کند.
ممکن است هنگام فعال کردن برنامه برنامه iOS خود ، خطاهای مربوط به ویژگی بررسی برنامه را مشاهده کنید. برای رفع این خطاها ، موارد زیر را امتحان کنید:
- تأیید کنید که شناسه بسته نرم افزاری و شناسه تیمی که مشخص کرده اید معتبر است.
- تأیید کنید که از کارت وحشی برای شناسه بسته نرم افزاری استفاده نمی کنید.
اجرای برنامه برای مشتری iOS خود را بررسی کنید
فعال کردن برنامه برای برنامه شما به طور خودکار درخواست های ناشناخته را مسدود نمی کند. برای اجرای این محافظت ، به نمای ویرایش مشتری iOS خود بروید. در آنجا ، معیارهای بررسی برنامه را در سمت راست صفحه تحت بخش Google برای بخش iOS مشاهده خواهید کرد. معیارها شامل اطلاعات زیر است:- تعداد درخواست های تأیید شده - درخواستهایی که دارای یک برنامه بررسی معتبر هستند. پس از فعال کردن اجرای برنامه ، فقط درخواست ها در این گروه موفق می شوند.
- تعداد درخواست های تأیید نشده: درخواست های مشتری منسوخ احتمالاً - درخواست های از دست دادن یک برنامه چک برنامه. این درخواست ممکن است از نسخه قدیمی برنامه شما باشد که شامل اجرای برنامه چک برنامه نمی شود.
- تعداد درخواست های تأیید نشده: درخواست های مبدأ ناشناخته - درخواست های از دست رفته یک برنامه چک برنامه که به نظر نمی رسد مانند برنامه شما باشد.
- تعداد درخواست های تأیید نشده: درخواست های نامعتبر - درخواست هایی با نشانه چک برنامه نامعتبر ، که ممکن است از یک مشتری غیرمجاز باشد که سعی در جعل هویت برنامه شما یا از محیط های شبیه سازی شده دارد.
برای اجرای بررسی برنامه ، روی دکمه اجرای کلیک کنید و انتخاب خود را تأیید کنید. پس از فعال شدن اجرای ، تمام درخواست های تأیید نشده از مشتری شما رد می شوند.
توجه : پس از فعال کردن اجرای ، می تواند تا 15 دقیقه طول بکشد تا تغییرات عملی شود.
برنامه Unenforce برای مشتری iOS خود را بررسی کنید
بررسی برنامه های غیرقابل اجرا برای برنامه شما متوقف خواهد شد و تمام درخواست های مشتری شما را به نقاط پایانی Google OAuth 2.0 ، از جمله درخواست های تأیید نشده اجازه می دهد.
برای بررسی برنامه UneNforce برای مشتری iOS خود ، به نمای ویرایش مشتری iOS بروید و روی دکمه UnenForce کلیک کرده و انتخاب خود را تأیید کنید.
توجه : پس از بررسی برنامه های غیرقابل اجرا ، می تواند تا 15 دقیقه طول بکشد تا تغییرات عملی شود.
برنامه را برای مشتری iOS خود غیرفعال کنید
غیرفعال کردن برنامه برای برنامه شما ، تمام نظارت و اجرای برنامه را متوقف می کند. به جای آن ، بررسی برنامه های غیرقابل اجرا را در نظر بگیرید تا بتوانید معیارهای مشتری خود را ادامه دهید.
برای غیرفعال کردن برنامه برای مشتری iOS خود ، به نمای ویرایش مشتری iOS بروید و مشتری OAUTH خود را از سوءاستفاده با دکمه ضامن برنامه Firebase استفاده کنید.
توجه : پس از غیرفعال کردن بررسی برنامه ، می تواند تا 15 دقیقه طول بکشد تا تغییرات عملی شود.
دسترسی مبتنی بر زمان
دسترسی مبتنی بر زمان به کاربر اجازه می دهد تا برنامه خود را برای مدت زمان محدود به داده های خود دسترسی دهد تا یک عمل را انجام دهد. دسترسی مبتنی بر زمان در محصولات Google Select در طول جریان رضایت در دسترس است و به کاربران این امکان را می دهد که برای مدت زمان محدود دسترسی را اعطا کنند. به عنوان مثال ، API قابلیت حمل داده است که انتقال یک بار داده را امکان پذیر می کند.
هنگامی که یک کاربر دسترسی مبتنی بر زمان برنامه شما را اعطا می کند ، Token Refresh پس از مدت زمان مشخص منقضی می شود. توجه داشته باشید که نشانه های تازه سازی ممکن است قبلاً در شرایط خاص باطل شوند. برای جزئیات بیشتر به این موارد مراجعه کنید. قسمت refresh_token_expires_in
در پاسخ مبادله کد مجوز بازگشت ، زمان باقی مانده را تا زمانی که توکن تازه در چنین مواردی منقضی می شود ، نشان می دهد.
ادامه مطلب
IETF بهترین تمرین فعلی OAUTH 2.0 برای برنامه های بومی بسیاری از بهترین شیوه های مستند شده در اینجا را تعیین می کند.
اجرای حفاظت از حساب متقابل
یک قدم اضافی که باید برای محافظت از حساب کاربری کاربران خود بردارید ، اجرای حفاظت از حساب متقابل با استفاده از سرویس محافظت از حساب متقابل Google است. این سرویس به شما امکان می دهد تا در اعلان های رویداد امنیتی که اطلاعاتی را در مورد تغییرات اساسی در حساب کاربری ارائه می دهد ، مشترک شوید. سپس می توانید بسته به نحوه تصمیم گیری در پاسخ به وقایع ، از اطلاعات برای انجام اقدامات استفاده کنید.
برخی از نمونه های نوع رویداد ارسال شده توسط سرویس محافظت از حساب متقابل Google: عبارتند از:
-
https://schemas.openid.net/secevent/risc/event-type/sessions-revoked
-
https://schemas.openid.net/secevent/oauth/event-type/token-revoked
-
https://schemas.openid.net/secevent/risc/event-type/account-disabled
برای اطلاعات بیشتر در مورد نحوه اجرای حفاظت از حساب متقابل و لیست کامل رویدادهای موجود ، به حساب های کاربری Protect با صفحه حفاظت از حساب متقابل مراجعه کنید.
این سند توضیح می دهد که چگونه برنامه های نصب شده بر روی دستگاه هایی مانند تلفن ، تبلت و رایانه از نقاط پایانی OAUTH 2.0 Google برای اجازه دسترسی به API های Google استفاده می کنند.
OAuth 2.0 به کاربران اجازه می دهد تا داده های خاصی را با یک برنامه به اشتراک بگذارند در حالی که نام کاربری، رمز عبور و سایر اطلاعات خود را خصوصی نگه می دارند. به عنوان مثال ، یک برنامه می تواند از OAUTH 2.0 برای به دست آوردن مجوز کاربران برای ذخیره پرونده ها در درایوهای Google خود استفاده کند.
برنامه های نصب شده به دستگاه های جداگانه توزیع می شوند و فرض بر این است که این برنامه ها نمی توانند اسرار را حفظ کنند. آنها می توانند در حالی که کاربر در برنامه حضور دارد یا وقتی برنامه در پس زمینه اجرا می شود ، به Google API دسترسی پیدا کنند.
این جریان مجوز شبیه به نمونه ای است که برای برنامه های وب سرور استفاده می شود. تفاوت اصلی این است که برنامه های نصب شده باید مرورگر سیستم را باز کنند و یک URI تغییر مسیر محلی را برای پاسخ به پاسخ های سرور مجوز Google تأمین کنند.
جایگزین ها
برای برنامه های تلفن همراه ، ممکن است ترجیح دهید از Google Sign Sign برای Android یا iOS استفاده کنید. کتابخانه های مشتری ورود به سیستم Google تأیید اعتبار و مجوز کاربر را اداره می کنند و ممکن است اجرای آنها ساده تر از پروتکل سطح پایین که در اینجا شرح داده شده است.
برای برنامه هایی که در دستگاه هایی که از یک مرورگر سیستم پشتیبانی نمی کنند یا قابلیت ورودی محدودی دارند ، مانند تلویزیون ، کنسول بازی ، دوربین یا چاپگر ، به OAuth 2.0 برای تلویزیون و دستگاه ها یا ورود به سیستم در تلویزیون و دستگاه های ورودی محدود مراجعه کنید.
کتابخانه ها و نمونه ها
ما کتابخانه ها و نمونه های زیر را توصیه می کنیم تا به شما در اجرای جریان OAUTH 2.0 که در این سند شرح داده شده است کمک کند:
پیش نیازها
API را برای پروژه خود فعال کنید
هر برنامه ای که Google API را فراخوانی کند ، باید آن API ها را در آن فعال کند API Console.
برای فعال کردن یک API برای پروژه خود:
- Open the API Library در Google API Console.
- If prompted, select a project, or create a new one.
- را API Library لیست کلیه API های موجود ، گروه بندی شده بر اساس خانواده محصول و محبوبیت. اگر API که می خواهید فعال کنید در لیست قابل مشاهده نیست ، از جستجو برای یافتن آن استفاده کنید ، یا روی مشاهده همه در خانواده محصولی که متعلق به آن است کلیک کنید.
- API را که می خواهید فعال کنید انتخاب کنید ، سپس بر روی دکمه Enable کلیک کنید.
- If prompted, enable billing.
- If prompted, read and accept the API's Terms of Service.
اعتبارنامه های مجوز ایجاد کنید
هر برنامه ای که از OAuth 2.0 برای دسترسی به API های Google استفاده می کند ، باید دارای اعتبار مجوز باشد که برنامه را به سرور OAUTH 2.0 Google شناسایی می کند. مراحل زیر نحوه ایجاد اعتبار برای پروژه خود را توضیح می دهد. سپس برنامه های شما می توانند از اعتبارنامه برای دسترسی به API هایی که برای آن پروژه فعال کرده اید استفاده کنند.
- Go to the Credentials page.
- روی ایجاد مشتری کلیک کنید.
- بخش های زیر انواع مشتری را که سرور مجوز Google از آن پشتیبانی می کند ، توصیف می کند. نوع مشتری را که برای برنامه خود توصیه می شود ، انتخاب کنید ، مشتری OAUTH خود را نامگذاری کنید و قسمت های دیگر را در صورت لزوم تنظیم کنید.
اندروید
- نوع برنامه Android را انتخاب کنید.
- نامی را برای مشتری OAUTH وارد کنید. این نام در پروژه شما نمایش داده می شود برای شناسایی مشتری
- نام بسته برنامه Android خود را وارد کنید. این مقدار در ویژگی
package
عنصر<manifest>
در پرونده مانیفست برنامه شما تعریف شده است. - اثر انگشت گواهی نامه SHA-1 را از توزیع برنامه وارد کنید.
- اگر برنامه شما از امضای برنامه توسط Google Play استفاده می کند ، اثر انگشت SHA-1 را از صفحه امضای برنامه کنسول Play کپی کنید.
- اگر کلیدهای اصلی و کلیدهای امضاء خود را مدیریت می کنید ، از ابزار KeyTool موجود در جاوا استفاده کنید تا اطلاعات گواهینامه را با فرمت قابل خواندن انسانی چاپ کنید. مقدار
SHA1
را در بخشCertificate fingerprints
خروجی KeyTool کپی کنید. برای اطلاعات بیشتر به تأیید اعتبار مشتری خود در API های Google برای اسناد Android مراجعه کنید.
- (اختیاری) مالکیت برنامه Android خود را تأیید کنید .
- روی ایجاد کلیک کنید.
iOS
- نوع برنامه iOS را انتخاب کنید.
- نامی را برای مشتری OAUTH وارد کنید. این نام در پروژه شما نمایش داده می شود برای شناسایی مشتری
- شناسه بسته نرم افزاری را برای برنامه خود وارد کنید. شناسه بسته نرم افزاری مقدار کلید cfbundleidentifier در پرونده منابع اطلاعاتی برنامه اطلاعاتی برنامه شما ( info.plist ) است. این مقدار معمولاً در صفحه عمومی یا صفحه امضای و قابلیت ویرایشگر پروژه Xcode نمایش داده می شود. شناسه بسته نرم افزاری همچنین در بخش اطلاعات عمومی صفحه اطلاعات برنامه برای برنامه در سایت App Store Connect Apple نمایش داده می شود.
تأیید کنید که شما از شناسه بسته نرم افزاری برای برنامه خود استفاده می کنید ، زیرا در صورت استفاده از ویژگی برنامه چک ، قادر به تغییر آن نخواهید بود.
- (اختیاری)
اگر برنامه در فروشگاه App Apple منتشر شده است ، شناسه فروشگاه برنامه برنامه خود را وارد کنید. شناسه فروشگاه یک رشته عددی است که در هر آدرس اینترنتی فروشگاه اپل قرار دارد.
- برنامه Apple App Store را در دستگاه iOS یا iPados خود باز کنید.
- برنامه خود را جستجو کنید.
- دکمه اشتراک (Square و Arrow Up Symbol) را انتخاب کنید.
- کپی پیوند را انتخاب کنید.
- پیوند را در یک ویرایشگر متن بچسبانید. شناسه فروشگاه App قسمت نهایی URL است.
مثال:
https://apps.apple.com/app/google/id 284815942
- (اختیاری)
شناسه تیم خود را وارد کنید. برای کسب اطلاعات بیشتر ، شناسه تیم خود را در مستندات حساب توسعه دهنده Apple پیدا کنید.
توجه: اگر در حال فعال کردن برنامه برای مشتری خود هستید ، قسمت شناسه تیم لازم است. - (اختیاری)
برنامه برنامه iOS خود را فعال کنید. هنگامی که شما برنامه را فعال می کنید ، از برنامه تأیید برنامه اپل استفاده می شود تا تأیید کند که درخواست های OAUTH 2.0 که از مشتری OAUTH شما سرچشمه گرفته است ، واقعی هستند و از برنامه شما آمده اند. این به کاهش خطر جعل هویت برنامه کمک می کند. در مورد فعال کردن برنامه برای برنامه iOS خود بیشتر بدانید .
- روی ایجاد کلیک کنید.
UWP
- نوع برنامه Universal Windows Platform را انتخاب کنید.
- نامی را برای مشتری OAUTH وارد کنید. این نام در پروژه شما نمایش داده می شود برای شناسایی مشتری
- شناسه فروشگاه مایکروسافت 12 کاراکتر برنامه خود را وارد کنید. می توانید این مقدار را در مرکز شریک مایکروسافت در صفحه هویت برنامه در بخش مدیریت برنامه پیدا کنید.
- روی ایجاد کلیک کنید.
برای برنامه های UWP ، طرح URI سفارشی نمی تواند بیش از 39 کاراکتر باشد.
حوزه های دسترسی را شناسایی کنید
Scopes درخواست شما را قادر می سازد تا فقط درخواست دسترسی به منابعی را که مورد نیاز خود دارد ، در عین حال کاربران را قادر می سازد میزان دسترسی را که به برنامه شما اعطا می کنند ، کنترل کنند. بنابراین ، ممکن است بین تعداد دامنه های درخواست شده و احتمال به دست آوردن رضایت کاربر رابطه معکوس وجود داشته باشد.
قبل از شروع اجرای مجوز OAUTH 2.0 ، توصیه می کنیم دامنه هایی را که برنامه شما برای دسترسی به مجوز نیاز دارد ، شناسایی کنید.
سند OAUTH 2.0 API Scopes شامل لیست کاملی از Scopes است که ممکن است برای دسترسی به Google API استفاده کنید.
به دست آوردن نشانه های دسترسی OAUTH 2.0
مراحل زیر نحوه تعامل برنامه شما با سرور OAUTH 2.0 Google را نشان می دهد تا رضایت کاربر برای انجام درخواست API از طرف کاربر را بدست آورد. برنامه شما قبل از اجرای درخواست API Google که نیاز به مجوز کاربر دارد ، باید این رضایت را داشته باشد.
مرحله 1: یک تأیید کننده کد و چالش ایجاد کنید
Google از کلید اثبات پروتکل Code Exchange (PKCE) پشتیبانی می کند تا برنامه نصب شده ایمن تر شود. یک تأیید کننده کد منحصر به فرد برای هر درخواست مجوز ایجاد می شود و مقدار تبدیل شده آن با نام "code_challenge" به سرور مجوز ارسال می شود تا کد مجوز را بدست آورد.
تأیید کننده کد را ایجاد کنید
code_verifier
یک رشته تصادفی رمزنگاری آنتروپی بالا با استفاده از شخصیت های بدون محدودیت [AZ] / [AZ] / [0-9] / "-" / "است. / "_" / "~" ، با حداقل طول 43 نویسه و حداکثر طول 128 نویسه.
تأیید کننده کد باید آنتروپی کافی داشته باشد تا حدس زدن مقدار آن غیر عملی باشد.
Code Challenge را ایجاد کنید
دو روش ایجاد چالش کد پشتیبانی می شود.
روش های تولید چالش کد | |
---|---|
S256 (توصیه شده) | Code Challenge Base64URL (بدون بالشتک) هش رمزگذاری شده Sha256 از تأیید کننده کد است.
|
ساده | Code Challenge همان مقدار تأیید کننده کد ایجاد شده در بالا است.
|
مرحله 2: درخواست را به سرور OAUTH 2.0 Google ارسال کنید
برای به دست آوردن مجوز کاربر ، درخواست را به سرور مجوز Google در https://accounts.google.com/o/oauth2/v2/auth
ارسال کنید. این نقطه پایانی به جستجوی جلسه فعال می پردازد ، کاربر را تأیید می کند و رضایت کاربر را به دست می آورد. نقطه پایانی فقط از طریق SSL قابل دسترسی است و از اتصالات HTTP (غیر SSL) امتناع می ورزد.
سرور مجوز از پارامترهای رشته پرس و جو زیر برای برنامه های نصب شده پشتیبانی می کند:
پارامترها | |||||||
---|---|---|---|---|---|---|---|
client_id | مورد نیاز شناسه مشتری برای برنامه شما. شما می توانید این مقدار را در . | ||||||
redirect_uri | مورد نیاز تعیین می کند که چگونه سرور مجوز Google پاسخی به برنامه شما ارسال می کند. چندین گزینه تغییر مسیر در دسترس برای برنامه های نصب شده وجود دارد ، و شما می توانید اعتبار مجوز خود را با یک روش تغییر مسیر خاص در ذهن تنظیم کنید. این مقدار دقیقاً باید با یکی از URI های تغییر مسیر مجاز برای مشتری OAUTH 2.0 ، که در مشتری خود پیکربندی کرده اید مطابقت داشته باشد . اگر این مقدار با URI مجاز مطابقت نداشته باشد ، خطای جدول زیر مقدار پارامتر
| ||||||
response_type | مورد نیاز تعیین می کند که آیا نقطه پایانی Google OAUTH 2.0 یک کد مجوز را برمی گرداند. مقدار پارامتر را روی | ||||||
scope | مورد نیاز لیستی از دامنه های با نفوذ فضا که منابعی را که برنامه شما می تواند از طرف کاربر به آن دسترسی پیدا کند ، شناسایی می کند. این مقادیر صفحه رضایت را که Google به کاربر نشان می دهد آگاه می کنند. Scopes درخواست شما را قادر می سازد تا فقط درخواست دسترسی به منابعی را که مورد نیاز خود دارد ، در عین حال کاربران را قادر می سازد میزان دسترسی را که به برنامه شما اعطا می کنند ، کنترل کنند. بنابراین ، بین تعداد دامنه های درخواست شده و احتمال به دست آوردن رضایت کاربر ، رابطه معکوس وجود دارد. | ||||||
code_challenge | توصیه می شود یک | ||||||
code_challenge_method | توصیه می شود مشخص می کند که از چه روشی برای رمزگذاری | ||||||
state | توصیه می شود هر مقدار رشته ای را که برنامه شما برای حفظ حالت بین درخواست مجوز شما و پاسخ سرور مجوز استفاده می کند ، مشخص می کند. سرور پس از رضایت کاربر به درخواست دسترسی یا درخواست دسترسی برنامه شما ، مقدار دقیقی را که ارسال می کنید به عنوان شما می توانید از این پارامتر برای چندین هدف استفاده کنید ، مانند هدایت کاربر به منبع صحیح در برنامه خود ، ارسال افراد غیر CACE و کاهش جعل درخواست متقابل سایت. از آنجا که می توان | ||||||
login_hint | اختیاری اگر برنامه شما می داند کدام کاربر در تلاش برای تأیید اعتبار است ، می تواند از این پارامتر برای ارائه اشاره به سرور احراز هویت Google استفاده کند. سرور از اشاره برای ساده سازی جریان ورود یا با ترجیح دادن قسمت ایمیل در فرم ورود به سیستم یا با انتخاب جلسه مناسب چند لوژین استفاده می کند. مقدار پارامتر را روی یک آدرس ایمیل یا شناسه |
نمونه URL های مجوز
برگه های زیر URL های مجوز نمونه را برای گزینه های مختلف URI تغییر دهنده نشان می دهد.
URL ها به جز مقدار پارامتر redirect_uri
یکسان هستند. URL ها همچنین حاوی پارامترهای مورد نیاز response_type
و client_id
و همچنین پارامتر state
اختیاری هستند. هر URL شامل استراحت خط و فضاهایی برای خوانایی است.
طرح URI سفارشی
https://accounts.google.com/o/oauth2/v2/auth? scope=email%20profile& response_type=code& state=security_token%3D138r5719ru3e1%26url%3Dhttps%3A%2F%2Foauth2.example.com%2Ftoken& redirect_uri=com.example.app%3A/oauth2redirect& client_id=client_id
آدرس IP Loopback
https://accounts.google.com/o/oauth2/v2/auth? scope=email%20profile& response_type=code& state=security_token%3D138r5719ru3e1%26url%3Dhttps%3A%2F%2Foauth2.example.com%2Ftoken& redirect_uri=http%3A//127.0.0.1%3A9004& client_id=client_id
مرحله 3: Google کاربر را برای رضایت ترغیب می کند
در این مرحله ، کاربر تصمیم می گیرد که آیا دسترسی به درخواست شما را به درخواست شما اعطا کند یا خیر. در این مرحله ، Google یک پنجره رضایت بخش را نشان می دهد که نام برنامه شما و خدمات Google API را نشان می دهد که درخواست اجازه دسترسی به اعتبار مجوز کاربر و خلاصه ای از دامنه های دسترسی را می دهد. سپس کاربر می تواند برای دسترسی به یک یا چند دامنه درخواست شده توسط درخواست شما موافقت کند یا از درخواست خودداری کند.
برنامه شما نیازی به انجام کاری در این مرحله ندارد زیرا منتظر پاسخ سرور OAUTH 2.0 Google است که نشان می دهد آیا دسترسی به آن اعطا شده است یا خیر. این پاسخ در مرحله زیر توضیح داده شده است.
خطاها
درخواست ها به نقطه پایان مجوز OAUTH 2.0 Google ممکن است به جای جریان احراز هویت و مجوز مورد انتظار ، پیام های خطای کاربر را نشان دهند. کدهای خطای رایج و وضوح پیشنهادی در زیر ذکر شده است.
admin_policy_enforced
حساب Google به دلیل سیاست های مدیر فضای کاری Google خود قادر به اجازه یک یا چند دامنه درخواست شده نیست. برای اطلاعات بیشتر در مورد چگونگی محدود کردن دسترسی به همه دامنه ها یا حسابرسی های حساس و محدود ، تا زمانی که دسترسی صریح به شناسه مشتری OAUTH شما دسترسی پیدا کند ، به کنترل مقاله کمک می کند که برنامه های شخص ثالث و برنامه های داخلی به داده های فضای کاری Google دسترسی پیدا می کنند.
disallowed_useragent
نقطه پایانی مجوز در داخل یک عامل تعبیه شده کاربر تعبیه شده توسط سیاست های OAUTH 2.0 Google که مجاز نیست نمایش داده می شود.
اندروید
توسعه دهندگان Android ممکن است هنگام باز کردن درخواست های مجوز درandroid.webkit.WebView
با این پیام خطا روبرو شوند. در عوض توسعه دهندگان باید از کتابخانه های Android مانند Google Sign-Ind برای Android یا AppAuth Foundation OpenID برای Android استفاده کنند.
توسعه دهندگان وب ممکن است با این خطا روبرو شوند وقتی یک برنامه Android یک لینک وب کلی را در یک عامل کاربر جاسازی شده باز می کند و یک کاربر به نقطه پایانی مجوز OAUTH 2.0 Google از سایت شما می رود. توسعه دهندگان باید به پیوندهای عمومی اجازه دهند در کنترل پیش فرض سیستم عامل باز شوند ، که شامل هر دو لینک برنامه Android Links یا برنامه مرورگر پیش فرض است. کتابخانه زبانه های Android Custom نیز یک گزینه پشتیبانی شده است.
iOS
توسعه دهندگان iOS و MACOS ممکن است هنگام باز کردن درخواست های مجوز درWKWebView
با این خطا روبرو شوند. در عوض توسعه دهندگان باید از کتابخانه های iOS مانند Google Sign-In-IN برای iOS یا AppAuth بنیاد OpenID برای iOS استفاده کنند.
توسعه دهندگان وب ممکن است با این خطا روبرو شوند وقتی یک برنامه iOS یا MACOS یک لینک وب کلی را در یک عامل کاربر تعبیه شده باز می کند و کاربر به نقطه پایانی مجوز OAUTH 2.0 Google از سایت شما می رود. توسعه دهندگان باید به پیوندهای عمومی اجازه دهند تا در کنترل پیش فرض سیستم عامل ، که شامل هر دو کنترل کننده پیوندهای جهانی یا برنامه مرورگر پیش فرض است ، باز شود. کتابخانهSFSafariViewController
نیز گزینه پشتیبانی است.
org_internal
شناسه مشتری OAuth در درخواست بخشی از یک پروژه است که دسترسی به حساب های Google را در یک سازمان خاص Google Cloud محدود می کند. برای اطلاعات بیشتر در مورد این گزینه پیکربندی ، به بخش نوع کاربر در تنظیم مقاله راهنمای صفحه رضایت OAUTH خود مراجعه کنید.
invalid_grant
اگر از یک تأیید کننده کد و چالش استفاده می کنید ، پارامتر code_callenge
نامعتبر یا از دست رفته است. اطمینان حاصل کنید که پارامتر code_challenge
به درستی تنظیم شده است.
هنگام طراوت یک نشانه دسترسی ، ممکن است این نشانه منقضی شده یا مورد ارزیابی قرار گرفته باشد. دوباره کاربر را تأیید کنید و از رضایت کاربر برای به دست آوردن نشانه های جدید بخواهید. اگر همچنان به دیدن این خطا ادامه می دهید ، اطمینان حاصل کنید که برنامه شما به درستی پیکربندی شده است و در درخواست خود از نشانه ها و پارامترهای صحیح استفاده می کنید. در غیر این صورت ، حساب کاربری ممکن است حذف یا غیرفعال شده باشد.
redirect_uri_mismatch
redirect_uri
که در درخواست مجوز گذشت ، با URI تغییر مسیر مجاز برای شناسه مشتری OAuth مطابقت ندارد. بررسی URI های تغییر مسیر مجاز در .
redirect_uri
منتقل شده ممکن است برای نوع مشتری نامعتبر باشد.
پارامتر redirect_uri
ممکن است به جریان OAUTH خارج از باند (OOB) اشاره کند که از بین رفته است و دیگر پشتیبانی نمی شود. برای به روزرسانی ادغام خود به راهنمای مهاجرت مراجعه کنید.
invalid_request
در درخواستی که ایجاد کردید مشکلی وجود داشت. این می تواند به دلایل مختلفی باشد:
- درخواست به درستی قالب بندی نشده است
- درخواست پارامترهای مورد نیاز از دست رفته بود
- این درخواست از یک روش مجوز استفاده می کند که Google از آن پشتیبانی نمی کند. تأیید کنید که ادغام OAUTH خود از یک روش ادغام توصیه شده استفاده می کند
- از یک طرح سفارشی برای تغییر مسیر URI استفاده می شود: اگر می بینید که خطای خطای طرح URI در برنامه های Chrome پشتیبانی نمی شود یا طرح URI سفارشی برای مشتری Android شما فعال نمی شود ، به این معنی است که شما از یک طرح URI سفارشی استفاده می کنید که در برنامه های کروم پشتیبانی نمی شود و به طور پیش فرض در Android غیرفعال می شود. در مورد گزینه های طرح URI سفارشی بیشتر بدانید
مرحله 4: پاسخ سرور OAUTH 2.0 را کنترل کنید
نحوه دریافت درخواست شما پاسخ مجوز بستگی به طرح تغییر مسیر URI است که از آن استفاده می کند. صرف نظر از این طرح ، پاسخ شامل کد مجوز ( code
) یا خطا ( error
) خواهد بود. به عنوان مثال ، error=access_denied
نشان می دهد که کاربر درخواست را رد کرده است.
اگر کاربر دسترسی به برنامه شما را اعطا کند ، می توانید کد مجوز را برای یک نشانه دسترسی و یک نشانه تازه ای که در مرحله بعدی توضیح داده شده است ، مبادله کنید.
مرحله 5: کد مجوز تبادل برای تازه کردن و دسترسی به نشانه ها
برای تبادل یک کد مجوز برای یک نشانه دسترسی ، با https://oauth2.googleapis.com/token
تماس بگیرید و پارامترهای زیر را تنظیم کنید:
فیلدها | |
---|---|
client_id | شناسه مشتری به دست آمده از . |
client_secret | راز مشتری به دست آمده از . |
code | کد مجوز از درخواست اولیه بازگشت. |
code_verifier | تأیید کننده کد که در مرحله 1 ایجاد کرده اید. |
grant_type | همانطور که در مشخصات OAUTH 2.0 تعریف شده است ، مقدار این قسمت باید روی authorization_code تنظیم شود. |
redirect_uri | یکی از URI های تغییر مسیر ذکر شده برای پروژه شما در برای client_id داده شده. |
قطعه زیر درخواست نمونه را نشان می دهد:
POST /token HTTP/1.1 Host: oauth2.googleapis.com Content-Type: application/x-www-form-urlencoded code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7& client_id=your_client_id& client_secret=your_client_secret& redirect_uri=http://127.0.0.1:9004& grant_type=authorization_code
Google با بازگشت یک شیء JSON که حاوی یک نشانه دسترسی کوتاه مدت و یک نشانه تازه است ، به این درخواست پاسخ می دهد.
پاسخ شامل زمینه های زیر است:
فیلدها | |
---|---|
access_token | نشانه ای که برنامه شما برای مجوز درخواست API Google ارسال می کند. |
expires_in | طول عمر باقی مانده از نشانه دسترسی در ثانیه. |
id_token | توجه: این ویژگی فقط در صورتی بازگردانده می شود که درخواست شما شامل دامنه هویت مانند openid ، profile یا email باشد. مقدار یک توکن وب JSON (JWT) است که حاوی اطلاعات هویت امضا شده دیجیتالی در مورد کاربر است. |
refresh_token | نشانه ای که می توانید برای به دست آوردن یک نشانه دسترسی جدید از آن استفاده کنید. نشانه های تازه سازی تا زمانی که کاربر دسترسی را باطل کند یا توکن تازه سازی منقضی شود ، معتبر هستند. توجه داشته باشید که نشانه های تازه سازی همیشه برای برنامه های نصب شده بازگردانده می شوند. |
refresh_token_expires_in | طول عمر باقی مانده از نشانه های تازه در چند ثانیه. این مقدار فقط زمانی تنظیم می شود که کاربر دسترسی مبتنی بر زمان را اعطا کند. |
scope | دامنه های دسترسی اعطا شده توسط access_token به عنوان لیستی از رشته های حساس به فضا و حساس بیان شده است. |
token_type | نوع نشانه برگشت. در این زمان ، مقدار این قسمت همیشه به Bearer تنظیم می شود. |
قطعه زیر پاسخ نمونه را نشان می دهد:
{ "access_token": "1/fFAGRNJru1FTz70BzhT3Zg", "expires_in": 3920, "token_type": "Bearer", "scope": "https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/calendar.readonly", "refresh_token": "1//xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI" }
مرحله ششم: بررسی کنید که کاربران Scopes اعطا کرده اند
هنگام درخواست چندین مجوز (SCOPES) ، کاربران ممکن است برنامه شما را به همه آنها دسترسی نداشته باشند. برنامه شما باید تأیید کند که در واقع به کدام یک از اسکوپ ها اعطا شده اند و موقعیت هایی را که برخی از مجوزها در آن رد می شوند ، به طور معمول با غیرفعال کردن ویژگی هایی که به آن حوزه های انکار تکیه می کنند ، رسیدگی می کنند.
با این حال، استثناهایی وجود دارد. برنامه های سازمانی Google WorksPace با نمایندگی در سطح دامنه یا برنامه هایی که به عنوان مورد اعتماد مشخص شده اند ، صفحه رضایت مجوزهای گرانول را دور بزنید. برای این برنامه ها ، کاربران صفحه رضایت مجوز گرانول را مشاهده نمی کنند. در عوض ، برنامه شما یا تمام دامنه های درخواست شده یا هیچ یک را دریافت می کند.
برای کسب اطلاعات بیشتر ، به نحوه رسیدگی به مجوزهای گرانول مراجعه کنید.
برای بررسی اینکه آیا کاربر به برنامه شما دسترسی به دامنه خاص داده است ، قسمت scope
را در پاسخ نشانه دسترسی بررسی کنید. دامنه های دسترسی اعطا شده توسط Access_Token به عنوان لیستی از رشته های حساس به فضا و حساس بیان شده است.
به عنوان مثال ، پاسخ نشانه دسترسی به نمونه زیر نشان می دهد که کاربر به برنامه شما دسترسی به فعالیت های درایو فقط خواندنی و مجوزهای وقایع تقویم داده است:
{ "access_token": "1/fFAGRNJru1FTz70BzhT3Zg", "expires_in": 3920, "token_type": "Bearer", "scope": "https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/calendar.readonly", "refresh_token": "1//xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI" }
تماس با Google API
پس از به دست آوردن برنامه دسترسی ، می توانید در صورت اعطای دامنه (های) دسترسی مورد نیاز API ، از Token برای برقراری تماس با Google API به نمایندگی از یک حساب کاربری خاص استفاده کنید. برای انجام این کار ، نشانه دسترسی را در درخواست API با استفاده از یک پارامتر پرس و جو access_token
یا یک Authorization
HTTP Bearer
Header درج کنید. در صورت امکان ، هدر HTTP ارجح است ، زیرا رشته های پرس و جو در سیاهههای مربوط به سرور قابل مشاهده هستند. در بیشتر موارد می توانید از یک کتابخانه مشتری برای تنظیم تماس های خود به Google API استفاده کنید (به عنوان مثال هنگام تماس با API Files Drive Files ).
می توانید تمام API های Google را امتحان کرده و دامنه آنها را در زمین بازی OAUTH 2.0 مشاهده کنید.
http نمونه هایی دریافت کنید
A call to the drive.files
endpoint (the Drive Files API) using the Authorization: Bearer
HTTP header might look like the following. Note that you need to specify your own access token:
GET /drive/v2/files HTTP/1.1 Host: www.googleapis.com Authorization: Bearer access_token
Here is a call to the same API for the authenticated user using the access_token
query string parameter:
GET https://www.googleapis.com/drive/v2/files?access_token=access_token
curl
examples
You can test these commands with the curl
command-line application. Here's an example that uses the HTTP header option (preferred):
curl -H "Authorization: Bearer access_token" https://www.googleapis.com/drive/v2/files
Or, alternatively, the query string parameter option:
curl https://www.googleapis.com/drive/v2/files?access_token=access_token
Refreshing an access token
Access tokens periodically expire and become invalid credentials for a related API request. You can refresh an access token without prompting the user for permission (including when the user is not present) if you requested offline access to the scopes associated with the token.
To refresh an access token, your application sends an HTTPS POST
request to Google's authorization server ( https://oauth2.googleapis.com/token
) that includes the following parameters:
فیلدها | |
---|---|
client_id | The client ID obtained from the API Console. |
client_secret | The client secret obtained from the API Console. (The client_secret is not applicable to requests from clients registered as Android, iOS, or Chrome applications.) |
grant_type | As defined in the OAuth 2.0 specification , this field's value must be set to refresh_token . |
refresh_token | The refresh token returned from the authorization code exchange. |
The following snippet shows a sample request:
POST /token HTTP/1.1 Host: oauth2.googleapis.com Content-Type: application/x-www-form-urlencoded client_id=your_client_id& client_secret=your_client_secret& refresh_token=refresh_token& grant_type=refresh_token
As long as the user has not revoked the access granted to the application, the token server returns a JSON object that contains a new access token. The following snippet shows a sample response:
{ "access_token": "1/fFAGRNJru1FTz70BzhT3Zg", "expires_in": 3920, "scope": "https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/calendar.readonly", "token_type": "Bearer" }
Note that there are limits on the number of refresh tokens that will be issued; one limit per client/user combination, and another per user across all clients. You should save refresh tokens in long-term storage and continue to use them as long as they remain valid. If your application requests too many refresh tokens, it may run into these limits, in which case older refresh tokens will stop working.
Revoking a token
In some cases a user may wish to revoke access given to an application. A user can revoke access by visiting Account Settings . See the Remove site or app access section of the Third-party sites & apps with access to your account support document for more information.
It is also possible for an application to programmatically revoke the access given to it. Programmatic revocation is important in instances where a user unsubscribes, removes an application, or the API resources required by an app have significantly changed. In other words, part of the removal process can include an API request to ensure the permissions previously granted to the application are removed.
To programmatically revoke a token, your application makes a request to https://oauth2.googleapis.com/revoke
and includes the token as a parameter:
curl -d -X -POST --header "Content-type:application/x-www-form-urlencoded" \ https://oauth2.googleapis.com/revoke?token={token}
The token can be an access token or a refresh token. If the token is an access token and it has a corresponding refresh token, the refresh token will also be revoked.
If the revocation is successfully processed, then the HTTP status code of the response is 200
. For error conditions, an HTTP status code 400
is returned along with an error code.
App redirect methods
Custom URI scheme (Android, iOS, UWP)
Custom URI schemes are a form of deeplinking that use a custom-defined scheme to open your app.
Alternative to using custom URI schemes on Android
Use the Google Sign-In for Android SDK which delivers the OAuth 2.0 response directly to your app, eliminating the need for a redirect URI.
How to migrate to the Google Sign-In for Android SDK
If you use a custom scheme for your OAuth integration on Android, you would need to complete the following actions to fully migrate to using the recommended Google Sign-In for Android SDK:
- Update your code to use the Google Sign-In SDK.
- Disable support for custom scheme in the Google API Console.
Follow the below steps to migrate to the Google Sign-In Android SDK:
- Update your code to use the Google Sign-In Android SDK:
- Examine your code to identify where you are sending a request to Google's OAuth 2.0 server ; if using a custom scheme, your request would look like the below:
https://accounts.google.com/o/oauth2/v2/auth? scope=<SCOPES>& response_type=code& &state=<STATE>& redirect_uri=com.example.app:/oauth2redirect& client_id=<CLIENT_ID>
com.example.app:/oauth2redirect
is the custom scheme redirect URI in the above example. See theredirect_uri
parameter definition for more details about the format of the custom URI scheme value. - Make note of the
scope
andclient_id
request parameters which you would need to configure the Google Sign-In SDK. - Follow the Start Integrating Google Sign-In into Your Android App instructions to set up the SDK. You can skip the Get your backend server's OAuth 2.0 client ID step as you would re-use the
client_id
you retrieved from the previous step. - Follow the Enabling Server-Side API access instructions. این شامل مراحل زیر است:
- Use the
getServerAuthCode
method to retrieve an auth code for the scopes you are requesting permission for. - Send the auth code to your app's backend to exchange it for an access & refresh token.
- Use the retrieved access token to make calls to Google APIs on behalf of the user.
- Use the
- Examine your code to identify where you are sending a request to Google's OAuth 2.0 server ; if using a custom scheme, your request would look like the below:
- Disable support for custom scheme in the Google API Console:
- Go to your OAuth 2.0 credentials list and select your Android client.
- Navigate to the Advanced Settings section, uncheck the Enable Custom URI Scheme checkbox, and click Save to disable custom URI scheme support.
Enable custom URI scheme
If the recommended alternative does not work for you, you can enable custom URI schemes for your Android client by following the below instructions:- Go to your OAuth 2.0 credentials list and select your Android client.
- Navigate to the Advanced Settings section, check the Enable Custom URI Scheme checkbox, and click Save to enable custom URI scheme support.
Alternative to using custom URI schemes on Chrome apps
Use the Chrome Identity API which delivers the OAuth 2.0 response directly to your app, eliminating the need for a redirect URI.
Loopback IP address (macOS, Linux, Windows desktop)
To receive the authorization code using this URL, your application must be listening on the local web server. That is possible on many, but not all, platforms. However, if your platform supports it, this is the recommended mechanism for obtaining the authorization code.
When your app receives the authorization response, for best usability it should respond by displaying an HTML page that instructs the user to close the browser and return to your app.
استفاده توصیه شده | macOS, Linux, and Windows desktop (but not Universal Windows Platform) apps |
Form values | Set the application type to Desktop app . |
Manual copy/paste (Deprecated)
Protect your apps
Verify app ownership (Android, Chrome)
You can verify ownership of your application to reduce the risk of app impersonation.
اندروید
To complete the verification process, you can use your Google Play Developer Account if you have one and your app is registered on the Google Play Console . The following requirements must be met for a successful verification:
- You must have a registered application in the Google Play Console with the same package name and SHA-1 signing certificate fingerprint as the Android OAuth client you are completing the verification for.
- You must have Admin permission for the app in the Google Play Console. Learn more about access management in the Google Play Console.
In the Verify App Ownership section of the Android client, click the Verify Ownership button to complete the verification process.
If the verification is successful, a notification will be displayed to confirm the success of the verification process. Otherwise, an error prompt will be shown.
To fix a failed verification, try the following:
- Make sure the app you are verifying is a registered app in the Google Play Console.
- Make sure you have Admin permission for the app in the Google Play Console.
کروم
To complete the verification process, you would use your Chrome Web Store Developer account. The following requirements must be met for a successful verification:
- You must have a registered item in the Chrome Web Store Developer Dashboard with the same item ID as the Chrome Extension OAuth client you are completing the verification for.
- You must be a publisher for the Chrome Web Store item. Learn more about access management in the Chrome Web Store Developer Dashboard.
In the Verify App Ownership section of the Chrome Extension client, click the Verify Ownership button to complete the verification process.
Note: Wait a few minutes before completing the verification process after granting access to your account.
If the verification is successful, a notification will be displayed to confirm the success of the verification process. Otherwise, an error prompt will be shown.
To fix a failed verification, try the following:
- Make sure there is a registered item in the Chrome Web Store Developer Dashboard with the same item ID as the Chrome Extension OAuth client you are completing the verification for.
- Make sure you are a publisher for the app, that is, you must either be the individual publisher of the app or a member of the group publisher of the app. Learn more about access management in the Chrome Web Store Developer Dashboard.
- If you just updated your group publisher list, verify that the group publisher membership list is synced in the Chrome Web Store Developer Dashboard. Learn more about syncing your publisher membership list.
App Check (iOS only)
The App Check feature helps safeguard your iOS applications from unauthorized usage by using Apple's App Attest service to verify that requests made to Google OAuth 2.0 endpoints originate from your authentic applications. This helps to reduce the risk of app impersonation.
Enable App Check for your iOS Client
The following requirements must be met to successfully enable App Check for your iOS client:- You must specify a team ID for your iOS client.
- You must not use a wildcard in your bundle ID since it can resolve to more than one app. This means that the bundle ID must not include the asterisk (*) symbol.
After enabling App Check, you will start seeing metrics related to OAuth requests from your client in the edit view of the OAuth client. Requests from unverified sources won't be blocked until you enforce App Check . The information in the metrics monitoring page can help you determine when to start enforcement.
You might see errors related to the App Check feature when enabling App Check for your iOS app. To fix these errors, try the following:
- Verify that the bundle ID and team ID you specified are valid.
- Verify that you are not using a wildcard for the bundle ID.
Enforce App Check for your iOS Client
Enabling App Check for your app does not automatically block unrecognized requests. To enforce this protection, go to the edit view of your iOS client. There, you will see App Check metrics to the right of the page under the Google Identity for iOS section. The metrics include the following information:- Number of verified requests - requests that have a valid App Check token. After you enable App Check enforcement, only requests in this category will succeed.
- Number of unverified requests: likely outdated client requests - requests missing an App Check token; these request may be from an older version of your app that doesn't include an App Check implementation.
- Number of unverified requests: unknown origin requests - requests missing an App Check token that don't look like they are coming from your app.
- Number of unverified requests: invalid requests - requests with an invalid App Check token, which may be from an inauthentic client attempting to impersonate your app, or from emulated environments.
To enforce App Check, click the ENFORCE button and confirm your choice. Once enforcement is active, all unverified requests from your client will be rejected.
Note : after you enable enforcement, it can take up to 15 minutes for the changes to take effect.
Unenforce App Check for your iOS Client
Unenforcing App Check for your app will stop enforcement and will allow all requests from your client to Google OAuth 2.0 endpoints, including unverified requests.
To unenforce App Check for your iOS client, navigate to the edit view of the iOS client and click the UNENFORCE button and confirm your choice.
Note : after unenforcing App Check, it can take up to 15 minutes for the changes to take effect.
Disable App Check for your iOS Client
Disabling App Check for your app will stop all App Check monitoring and enforcement . Consider unenforcing App Check instead so you can continue monitoring metrics for your client.
To disable App Check for your iOS client, navigate to the edit view of the iOS client and turn off the Protect your OAuth client from abuse with Firebase App Check toggle button.
Note : after disabling App Check, it can take up to 15 minutes for the changes to take effect.
Time-based access
Time-based access allows a user to grant your app access to their data for a limited duration to complete an action. Time-based access is available in select Google products during the consent flow, giving users the option to grant access for a limited period of time. An example is the Data Portability API which enables a one-time transfer of data.
When a user grants your application time-based access, the refresh token will expire after the specified duration. Note that refresh tokens may be invalidated earlier under specific circumstances; see these cases for details. The refresh_token_expires_in
field returned in the authorization code exchange response represents the time remaining until the refresh token expires in such cases.
ادامه مطلب
The IETF Best Current Practice OAuth 2.0 for Native Apps establishes many of the best practices documented here.
Implementing Cross-Account Protection
An additional step you should take to protect your users' accounts is implementing Cross-Account Protection by utilizing Google's Cross-Account Protection Service. This service lets you subscribe to security event notifications which provide information to your application about major changes to the user account. You can then use the information to take action depending on how you decide to respond to events.
Some examples of the event types sent to your app by Google's Cross-Account Protection Service are:
-
https://schemas.openid.net/secevent/risc/event-type/sessions-revoked
-
https://schemas.openid.net/secevent/oauth/event-type/token-revoked
-
https://schemas.openid.net/secevent/risc/event-type/account-disabled
See the Protect user accounts with Cross-Account Protection page for more information on how to implement Cross Account Protection and for the full list of available events.
This document explains how applications installed on devices like phones, tablets, and computers use Google's OAuth 2.0 endpoints to authorize access to Google APIs.
OAuth 2.0 به کاربران اجازه می دهد تا داده های خاصی را با یک برنامه به اشتراک بگذارند در حالی که نام کاربری، رمز عبور و سایر اطلاعات خود را خصوصی نگه می دارند. For example, an application can use OAuth 2.0 to obtain permission from users to store files in their Google Drives.
Installed apps are distributed to individual devices, and it is assumed that these apps cannot keep secrets. They can access Google APIs while the user is present at the app or when the app is running in the background.
This authorization flow is similar to the one used for web server applications . The main difference is that installed apps must open the system browser and supply a local redirect URI to handle responses from Google's authorization server.
جایگزین ها
For mobile apps, you may prefer to use Google Sign-in for Android or iOS . The Google Sign-in client libraries handle authentication and user authorization, and they may be simpler to implement than the lower-level protocol described here.
For apps running on devices that do not support a system browser or that have limited input capabilities, such as TVs, game consoles, cameras, or printers, see OAuth 2.0 for TVs & Devices or Sign-In on TVs and Limited Input Devices .
Libraries and samples
We recommend the following libraries and samples to help you implement the OAuth 2.0 flow described in this document:
- AppAuth for Android library
- AppAuth for iOS library
- OAuth for Apps: Windows Samples
پیش نیازها
Enable APIs for your project
Any application that calls Google APIs needs to enable those APIs in the API Console.
برای فعال کردن یک API برای پروژه خود:
- Open the API Library در Google API Console.
- If prompted, select a project, or create a new one.
- را API Library lists all available APIs, grouped by product family and popularity. If the API you want to enable isn't visible in the list, use search to find it, or click View All in the product family it belongs to.
- Select the API you want to enable, then click the Enable button.
- If prompted, enable billing.
- If prompted, read and accept the API's Terms of Service.
Create authorization credentials
Any application that uses OAuth 2.0 to access Google APIs must have authorization credentials that identify the application to Google's OAuth 2.0 server. The following steps explain how to create credentials for your project. Your applications can then use the credentials to access APIs that you have enabled for that project.
- Go to the Credentials page.
- Click Create client .
- The following sections describe the client types that Google's authorization server supports. Choose the client type that is recommended for your application, name your OAuth client, and set the other fields in the form as appropriate.
اندروید
- Select the Android application type.
- Enter a name for the OAuth client. This name is displayed on your project's to identify the client.
- Enter the package name of your Android app. This value is defined in the
package
attribute of the<manifest>
element in your app manifest file. - Enter the SHA-1 signing certificate fingerprint of the app distribution.
- If your app uses app signing by Google Play , copy the SHA-1 fingerprint from the app signing page of the Play Console.
- If you manage your own keystore and signing keys, use the keytool utility included with Java to print certificate information in a human-readable format. Copy the
SHA1
value in theCertificate fingerprints
section of the keytool output. See Authenticating Your Client in the Google APIs for Android documentation for more information.
- (Optional) Verify ownership of your Android application.
- روی ایجاد کلیک کنید.
iOS
- Select the iOS application type.
- Enter a name for the OAuth client. This name is displayed on your project's to identify the client.
- Enter the bundle identifier for your app. The bundle ID is the value of the CFBundleIdentifier key in your app's information property list resource file ( info.plist ). The value is most commonly displayed in the General pane or the Signing & Capabilities pane of the Xcode project editor. The bundle ID is also displayed in the General Information section of the App Information page for the app on Apple's App Store Connect site .
Confirm that you are using the correct bundle ID for your app, as you won't be able to change it if you are using the App Check feature.
- (اختیاری)
Enter your app's App Store ID if the app is published in Apple's App Store. The Store ID is a numeric string included in every Apple App Store URL.
- Open the Apple App Store app on your iOS or iPadOS device.
- برنامه خود را جستجو کنید.
- Select the Share button (square and arrow up symbol).
- کپی پیوند را انتخاب کنید.
- Paste the link into a text editor. The App Store ID is the final part of the URL.
Example:
https://apps.apple.com/app/google/id 284815942
- (اختیاری)
Enter your Team ID. See Locate your Team ID in the Apple Developer Account documentation for more information.
Note: The Team ID field is required if you are enabling App Check for your client. - (اختیاری)
Enable App Check for your iOS app. When you enable App Check, Apple's App Attest service is used to verify that OAuth 2.0 requests originating from your OAuth client are genuine and come from your app. This helps to reduce the risk of app impersonation. Learn more about enabling App Check for your iOS app .
- روی ایجاد کلیک کنید.
UWP
- Select the Universal Windows Platform application type.
- Enter a name for the OAuth client. This name is displayed on your project's to identify the client.
- Enter your app's 12-character Microsoft Store ID. You can find this value in Microsoft Partner Center on the App identity page in the App management section.
- روی ایجاد کلیک کنید.
For UWP apps, the custom URI scheme cannot be longer than 39 characters.
Identify access scopes
Scopes enable your application to only request access to the resources that it needs while also enabling users to control the amount of access that they grant to your application. Thus, there may be an inverse relationship between the number of scopes requested and the likelihood of obtaining user consent.
Before you start implementing OAuth 2.0 authorization, we recommend that you identify the scopes that your app will need permission to access.
The OAuth 2.0 API Scopes document contains a full list of scopes that you might use to access Google APIs.
Obtaining OAuth 2.0 access tokens
The following steps show how your application interacts with Google's OAuth 2.0 server to obtain a user's consent to perform an API request on the user's behalf. Your application must have that consent before it can execute a Google API request that requires user authorization.
Step 1: Generate a code verifier and challenge
Google supports the Proof Key for Code Exchange (PKCE) protocol to make the installed app flow more secure. A unique code verifier is created for every authorization request, and its transformed value, called "code_challenge", is sent to the authorization server to obtain the authorization code.
Create the code verifier
A code_verifier
is a high-entropy cryptographic random string using the unreserved characters [AZ] / [az] / [0-9] / "-" / "." / "_" / "~", with a minimum length of 43 characters and a maximum length of 128 characters.
The code verifier should have enough entropy to make it impractical to guess the value.
Create the code challenge
Two methods of creating the code challenge are supported.
Code Challenge Generation Methods | |
---|---|
S256 (recommended) | The code challenge is the Base64URL (with no padding) encoded SHA256 hash of the code verifier.
|
ساده | The code challenge is the same value as the code verifier generated above.
|
Step 2: Send a request to Google's OAuth 2.0 server
To obtain user authorization, send a request to Google's authorization server at https://accounts.google.com/o/oauth2/v2/auth
. This endpoint handles active session lookup, authenticates the user, and obtains user consent. The endpoint is only accessible over SSL, and it refuses HTTP (non-SSL) connections.
The authorization server supports the following query string parameters for installed applications:
پارامترها | |||||||
---|---|---|---|---|---|---|---|
client_id | مورد نیاز The client ID for your application. You can find this value in the . | ||||||
redirect_uri | مورد نیاز Determines how Google's authorization server sends a response to your app. There are several redirect options available to installed apps, and you will have set up your authorization credentials with a particular redirect method in mind. The value must exactly match one of the authorized redirect URIs for the OAuth 2.0 client, which you configured in your client's . If this value doesn't match an authorized URI, you will get a The table below shows the appropriate
| ||||||
response_type | مورد نیاز Determines whether the Google OAuth 2.0 endpoint returns an authorization code. Set the parameter value to | ||||||
scope | مورد نیاز A space-delimited list of scopes that identify the resources that your application could access on the user's behalf. These values inform the consent screen that Google displays to the user. Scopes enable your application to only request access to the resources that it needs while also enabling users to control the amount of access that they grant to your application. Thus, there is an inverse relationship between the number of scopes requested and the likelihood of obtaining user consent. | ||||||
code_challenge | توصیه می شود Specifies an encoded | ||||||
code_challenge_method | توصیه می شود Specifies what method was used to encode a | ||||||
state | توصیه می شود Specifies any string value that your application uses to maintain state between your authorization request and the authorization server's response. The server returns the exact value that you send as a You can use this parameter for several purposes, such as directing the user to the correct resource in your application, sending nonces, and mitigating cross-site request forgery. Since your | ||||||
login_hint | اختیاری If your application knows which user is trying to authenticate, it can use this parameter to provide a hint to the Google Authentication Server. The server uses the hint to simplify the login flow either by prefilling the email field in the sign-in form or by selecting the appropriate multi-login session. Set the parameter value to an email address or |
Sample authorization URLs
The tabs below show sample authorization URLs for the different redirect URI options.
The URLs are identical except for the value of the redirect_uri
parameter. The URLs also contain the required response_type
and client_id
parameters as well as the optional state
parameter. Each URL contains line breaks and spaces for readability.
Custom URI scheme
https://accounts.google.com/o/oauth2/v2/auth? scope=email%20profile& response_type=code& state=security_token%3D138r5719ru3e1%26url%3Dhttps%3A%2F%2Foauth2.example.com%2Ftoken& redirect_uri=com.example.app%3A/oauth2redirect& client_id=client_id
Loopback IP address
https://accounts.google.com/o/oauth2/v2/auth? scope=email%20profile& response_type=code& state=security_token%3D138r5719ru3e1%26url%3Dhttps%3A%2F%2Foauth2.example.com%2Ftoken& redirect_uri=http%3A//127.0.0.1%3A9004& client_id=client_id
Step 3: Google prompts user for consent
In this step, the user decides whether to grant your application the requested access. At this stage, Google displays a consent window that shows the name of your application and the Google API services that it is requesting permission to access with the user's authorization credentials and a summary of the scopes of access to be granted. The user can then consent to grant access to one or more scopes requested by your application or refuse the request.
Your application doesn't need to do anything at this stage as it waits for the response from Google's OAuth 2.0 server indicating whether any access was granted. That response is explained in the following step.
خطاها
Requests to Google's OAuth 2.0 authorization endpoint may display user-facing error messages instead of the expected authentication and authorization flows. Common error codes and suggested resolutions are listed below.
admin_policy_enforced
The Google Account is unable to authorize one or more scopes requested due to the policies of their Google Workspace administrator. See the Google Workspace Admin help article Control which third-party & internal apps access Google Workspace data for more information about how an administrator may restrict access to all scopes or sensitive and restricted scopes until access is explicitly granted to your OAuth client ID.
disallowed_useragent
The authorization endpoint is displayed inside an embedded user-agent disallowed by Google's OAuth 2.0 Policies .
اندروید
Android developers may encounter this error message when opening authorization requests inandroid.webkit.WebView
. Developers should instead use Android libraries such as Google Sign-In for Android or OpenID Foundation's AppAuth for Android .
Web developers may encounter this error when an Android app opens a general web link in an embedded user-agent and a user navigates to Google's OAuth 2.0 authorization endpoint from your site. Developers should allow general links to open in the default link handler of the operating system, which includes both Android App Links handlers or the default browser app. The Android Custom Tabs library is also a supported option.
iOS
iOS and macOS developers may encounter this error when opening authorization requests inWKWebView
. Developers should instead use iOS libraries such as Google Sign-In for iOS or OpenID Foundation's AppAuth for iOS .
Web developers may encounter this error when an iOS or macOS app opens a general web link in an embedded user-agent and a user navigates to Google's OAuth 2.0 authorization endpoint from your site. Developers should allow general links to open in the default link handler of the operating system, which includes both Universal Links handlers or the default browser app. TheSFSafariViewController
library is also a supported option.
org_internal
The OAuth client ID in the request is part of a project limiting access to Google Accounts in a specific Google Cloud Organization . For more information about this configuration option see the User type section in the Setting up your OAuth consent screen help article.
invalid_grant
If you are using a code verifier and challenge , the code_callenge
parameter is invalid or missing. Ensure that the code_challenge
parameter is set correctly.
When refreshing an access token , the token may have expired or has beeninvalidated. Authenticate the user again and ask for user consent to obtain new tokens. If you are continuing to see this error, ensure that your application has been configured correctly and that you are using the correct tokens and parameters in your request. Otherwise, the user account may have been deleted or disabled.
redirect_uri_mismatch
The redirect_uri
passed in the authorization request does not match an authorized redirect URI for the OAuth client ID. Review authorized redirect URIs in the .
The passed redirect_uri
may be invalid for the client type.
The redirect_uri
parameter may refer to the OAuth out-of-band (OOB) flow that has been deprecated and is no longer supported. Refer to the migration guide to update your integration.
invalid_request
There was something wrong with the request you made. این می تواند به دلایل مختلفی باشد:
- The request was not properly formatted
- The request was missing required parameters
- The request uses an authorization method that Google doesn't support. Verify your OAuth integration uses a recommended integration method
- A custom scheme is used for the redirect uri : If you see the error message Custom URI scheme is not supported on Chrome apps or Custom URI scheme is not enabled for your Android client , it means you are using a custom URI scheme which isn't supported on Chrome apps and is disabled by default on Android. Learn more about custom URI scheme alternatives
Step 4: Handle the OAuth 2.0 server response
The manner in which your application receives the authorization response depends on the redirect URI scheme that it uses. Regardless of the scheme, the response will either contain an authorization code ( code
) or an error ( error
). For example, error=access_denied
indicates that the user declined the request.
If the user grants access to your application, you can exchange the authorization code for an access token and a refresh token as described in the next step.
Step 5: Exchange authorization code for refresh and access tokens
To exchange an authorization code for an access token, call the https://oauth2.googleapis.com/token
endpoint and set the following parameters:
فیلدها | |
---|---|
client_id | The client ID obtained from the . |
client_secret | The client secret obtained from the . |
code | The authorization code returned from the initial request. |
code_verifier | The code verifier you created in Step 1 . |
grant_type | As defined in the OAuth 2.0 specification , this field's value must be set to authorization_code . |
redirect_uri | One of the redirect URIs listed for your project in the for the given client_id . |
The following snippet shows a sample request:
POST /token HTTP/1.1 Host: oauth2.googleapis.com Content-Type: application/x-www-form-urlencoded code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7& client_id=your_client_id& client_secret=your_client_secret& redirect_uri=http://127.0.0.1:9004& grant_type=authorization_code
Google responds to this request by returning a JSON object that contains a short-lived access token and a refresh token.
The response contains the following fields:
فیلدها | |
---|---|
access_token | The token that your application sends to authorize a Google API request. |
expires_in | The remaining lifetime of the access token in seconds. |
id_token | Note: This property is only returned if your request included an identity scope, such as openid , profile , or email . The value is a JSON Web Token (JWT) that contains digitally signed identity information about the user. |
refresh_token | A token that you can use to obtain a new access token. Refresh tokens are valid until the user revokes access or the refresh token expires. Note that refresh tokens are always returned for installed applications. |
refresh_token_expires_in | The remaining lifetime of the refresh token in seconds. This value is only set when the user grants time-based access . |
scope | The scopes of access granted by the access_token expressed as a list of space-delimited, case-sensitive strings. |
token_type | The type of token returned. At this time, this field's value is always set to Bearer . |
The following snippet shows a sample response:
{ "access_token": "1/fFAGRNJru1FTz70BzhT3Zg", "expires_in": 3920, "token_type": "Bearer", "scope": "https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/calendar.readonly", "refresh_token": "1//xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI" }
Step 6: Check which scopes users granted
When requesting multiple permissions (scopes), users may not grant your app access to all of them. Your app must verify which scopes were actually granted and gracefully handle situations where some permissions are denied, typically by disabling the features that rely on those denied scopes.
با این حال، استثناهایی وجود دارد. Google Workspace Enterprise apps with domain-wide delegation of authority , or apps marked as Trusted , bypass the granular permissions consent screen. For these apps, users won't see the granular permission consent screen. Instead, your app will either receive all requested scopes or none.
For more detailed information, see How to handle granular permissions .
To check whether the user has granted your application access to a particular scope, exam the scope
field in the access token response. The scopes of access granted by the access_token expressed as a list of space-delimited, case-sensitive strings.
For example, the following sample access token response indicates that the user has granted your application access to the read-only Drive activity and Calendar events permissions:
{ "access_token": "1/fFAGRNJru1FTz70BzhT3Zg", "expires_in": 3920, "token_type": "Bearer", "scope": "https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/calendar.readonly", "refresh_token": "1//xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI" }
Calling Google APIs
After your application obtains an access token, you can use the token to make calls to a Google API on behalf of a given user account if the scope(s) of access required by the API have been granted. To do this, include the access token in a request to the API by including either an access_token
query parameter or an Authorization
HTTP header Bearer
value. When possible, the HTTP header is preferable, because query strings tend to be visible in server logs. In most cases you can use a client library to set up your calls to Google APIs (for example, when calling the Drive Files API ).
You can try out all the Google APIs and view their scopes at the OAuth 2.0 Playground .
HTTP GET examples
A call to the drive.files
endpoint (the Drive Files API) using the Authorization: Bearer
HTTP header might look like the following. Note that you need to specify your own access token:
GET /drive/v2/files HTTP/1.1 Host: www.googleapis.com Authorization: Bearer access_token
Here is a call to the same API for the authenticated user using the access_token
query string parameter:
GET https://www.googleapis.com/drive/v2/files?access_token=access_token
curl
examples
You can test these commands with the curl
command-line application. Here's an example that uses the HTTP header option (preferred):
curl -H "Authorization: Bearer access_token" https://www.googleapis.com/drive/v2/files
Or, alternatively, the query string parameter option:
curl https://www.googleapis.com/drive/v2/files?access_token=access_token
Refreshing an access token
Access tokens periodically expire and become invalid credentials for a related API request. You can refresh an access token without prompting the user for permission (including when the user is not present) if you requested offline access to the scopes associated with the token.
To refresh an access token, your application sends an HTTPS POST
request to Google's authorization server ( https://oauth2.googleapis.com/token
) that includes the following parameters:
فیلدها | |
---|---|
client_id | The client ID obtained from the API Console. |
client_secret | The client secret obtained from the API Console. (The client_secret is not applicable to requests from clients registered as Android, iOS, or Chrome applications.) |
grant_type | As defined in the OAuth 2.0 specification , this field's value must be set to refresh_token . |
refresh_token | The refresh token returned from the authorization code exchange. |
The following snippet shows a sample request:
POST /token HTTP/1.1 Host: oauth2.googleapis.com Content-Type: application/x-www-form-urlencoded client_id=your_client_id& client_secret=your_client_secret& refresh_token=refresh_token& grant_type=refresh_token
As long as the user has not revoked the access granted to the application, the token server returns a JSON object that contains a new access token. The following snippet shows a sample response:
{ "access_token": "1/fFAGRNJru1FTz70BzhT3Zg", "expires_in": 3920, "scope": "https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/calendar.readonly", "token_type": "Bearer" }
Note that there are limits on the number of refresh tokens that will be issued; one limit per client/user combination, and another per user across all clients. You should save refresh tokens in long-term storage and continue to use them as long as they remain valid. If your application requests too many refresh tokens, it may run into these limits, in which case older refresh tokens will stop working.
Revoking a token
In some cases a user may wish to revoke access given to an application. A user can revoke access by visiting Account Settings . See the Remove site or app access section of the Third-party sites & apps with access to your account support document for more information.
It is also possible for an application to programmatically revoke the access given to it. Programmatic revocation is important in instances where a user unsubscribes, removes an application, or the API resources required by an app have significantly changed. In other words, part of the removal process can include an API request to ensure the permissions previously granted to the application are removed.
To programmatically revoke a token, your application makes a request to https://oauth2.googleapis.com/revoke
and includes the token as a parameter:
curl -d -X -POST --header "Content-type:application/x-www-form-urlencoded" \ https://oauth2.googleapis.com/revoke?token={token}
The token can be an access token or a refresh token. If the token is an access token and it has a corresponding refresh token, the refresh token will also be revoked.
If the revocation is successfully processed, then the HTTP status code of the response is 200
. For error conditions, an HTTP status code 400
is returned along with an error code.
App redirect methods
Custom URI scheme (Android, iOS, UWP)
Custom URI schemes are a form of deeplinking that use a custom-defined scheme to open your app.
Alternative to using custom URI schemes on Android
Use the Google Sign-In for Android SDK which delivers the OAuth 2.0 response directly to your app, eliminating the need for a redirect URI.
How to migrate to the Google Sign-In for Android SDK
If you use a custom scheme for your OAuth integration on Android, you would need to complete the following actions to fully migrate to using the recommended Google Sign-In for Android SDK:
- Update your code to use the Google Sign-In SDK.
- Disable support for custom scheme in the Google API Console.
Follow the below steps to migrate to the Google Sign-In Android SDK:
- Update your code to use the Google Sign-In Android SDK:
- Examine your code to identify where you are sending a request to Google's OAuth 2.0 server ; if using a custom scheme, your request would look like the below:
https://accounts.google.com/o/oauth2/v2/auth? scope=<SCOPES>& response_type=code& &state=<STATE>& redirect_uri=com.example.app:/oauth2redirect& client_id=<CLIENT_ID>
com.example.app:/oauth2redirect
is the custom scheme redirect URI in the above example. See theredirect_uri
parameter definition for more details about the format of the custom URI scheme value. - Make note of the
scope
andclient_id
request parameters which you would need to configure the Google Sign-In SDK. - Follow the Start Integrating Google Sign-In into Your Android App instructions to set up the SDK. You can skip the Get your backend server's OAuth 2.0 client ID step as you would re-use the
client_id
you retrieved from the previous step. - Follow the Enabling Server-Side API access instructions. این شامل مراحل زیر است:
- Use the
getServerAuthCode
method to retrieve an auth code for the scopes you are requesting permission for. - Send the auth code to your app's backend to exchange it for an access & refresh token.
- Use the retrieved access token to make calls to Google APIs on behalf of the user.
- Use the
- Examine your code to identify where you are sending a request to Google's OAuth 2.0 server ; if using a custom scheme, your request would look like the below:
- Disable support for custom scheme in the Google API Console:
- Go to your OAuth 2.0 credentials list and select your Android client.
- Navigate to the Advanced Settings section, uncheck the Enable Custom URI Scheme checkbox, and click Save to disable custom URI scheme support.
Enable custom URI scheme
If the recommended alternative does not work for you, you can enable custom URI schemes for your Android client by following the below instructions:- Go to your OAuth 2.0 credentials list and select your Android client.
- Navigate to the Advanced Settings section, check the Enable Custom URI Scheme checkbox, and click Save to enable custom URI scheme support.
Alternative to using custom URI schemes on Chrome apps
Use the Chrome Identity API which delivers the OAuth 2.0 response directly to your app, eliminating the need for a redirect URI.
Loopback IP address (macOS, Linux, Windows desktop)
To receive the authorization code using this URL, your application must be listening on the local web server. That is possible on many, but not all, platforms. However, if your platform supports it, this is the recommended mechanism for obtaining the authorization code.
When your app receives the authorization response, for best usability it should respond by displaying an HTML page that instructs the user to close the browser and return to your app.
استفاده توصیه شده | macOS, Linux, and Windows desktop (but not Universal Windows Platform) apps |
Form values | Set the application type to Desktop app . |
Manual copy/paste (Deprecated)
Protect your apps
Verify app ownership (Android, Chrome)
You can verify ownership of your application to reduce the risk of app impersonation.
اندروید
To complete the verification process, you can use your Google Play Developer Account if you have one and your app is registered on the Google Play Console . The following requirements must be met for a successful verification:
- You must have a registered application in the Google Play Console with the same package name and SHA-1 signing certificate fingerprint as the Android OAuth client you are completing the verification for.
- You must have Admin permission for the app in the Google Play Console. Learn more about access management in the Google Play Console.
In the Verify App Ownership section of the Android client, click the Verify Ownership button to complete the verification process.
If the verification is successful, a notification will be displayed to confirm the success of the verification process. Otherwise, an error prompt will be shown.
To fix a failed verification, try the following:
- Make sure the app you are verifying is a registered app in the Google Play Console.
- Make sure you have Admin permission for the app in the Google Play Console.
کروم
To complete the verification process, you would use your Chrome Web Store Developer account. The following requirements must be met for a successful verification:
- You must have a registered item in the Chrome Web Store Developer Dashboard with the same item ID as the Chrome Extension OAuth client you are completing the verification for.
- You must be a publisher for the Chrome Web Store item. Learn more about access management in the Chrome Web Store Developer Dashboard.
In the Verify App Ownership section of the Chrome Extension client, click the Verify Ownership button to complete the verification process.
Note: Wait a few minutes before completing the verification process after granting access to your account.
If the verification is successful, a notification will be displayed to confirm the success of the verification process. Otherwise, an error prompt will be shown.
To fix a failed verification, try the following:
- Make sure there is a registered item in the Chrome Web Store Developer Dashboard with the same item ID as the Chrome Extension OAuth client you are completing the verification for.
- Make sure you are a publisher for the app, that is, you must either be the individual publisher of the app or a member of the group publisher of the app. Learn more about access management in the Chrome Web Store Developer Dashboard.
- If you just updated your group publisher list, verify that the group publisher membership list is synced in the Chrome Web Store Developer Dashboard. Learn more about syncing your publisher membership list.
App Check (iOS only)
The App Check feature helps safeguard your iOS applications from unauthorized usage by using Apple's App Attest service to verify that requests made to Google OAuth 2.0 endpoints originate from your authentic applications. This helps to reduce the risk of app impersonation.
Enable App Check for your iOS Client
The following requirements must be met to successfully enable App Check for your iOS client:- You must specify a team ID for your iOS client.
- You must not use a wildcard in your bundle ID since it can resolve to more than one app. This means that the bundle ID must not include the asterisk (*) symbol.
After enabling App Check, you will start seeing metrics related to OAuth requests from your client in the edit view of the OAuth client. Requests from unverified sources won't be blocked until you enforce App Check . The information in the metrics monitoring page can help you determine when to start enforcement.
You might see errors related to the App Check feature when enabling App Check for your iOS app. To fix these errors, try the following:
- Verify that the bundle ID and team ID you specified are valid.
- Verify that you are not using a wildcard for the bundle ID.
Enforce App Check for your iOS Client
Enabling App Check for your app does not automatically block unrecognized requests. To enforce this protection, go to the edit view of your iOS client. There, you will see App Check metrics to the right of the page under the Google Identity for iOS section. The metrics include the following information:- Number of verified requests - requests that have a valid App Check token. After you enable App Check enforcement, only requests in this category will succeed.
- Number of unverified requests: likely outdated client requests - requests missing an App Check token; these request may be from an older version of your app that doesn't include an App Check implementation.
- Number of unverified requests: unknown origin requests - requests missing an App Check token that don't look like they are coming from your app.
- Number of unverified requests: invalid requests - requests with an invalid App Check token, which may be from an inauthentic client attempting to impersonate your app, or from emulated environments.
To enforce App Check, click the ENFORCE button and confirm your choice. Once enforcement is active, all unverified requests from your client will be rejected.
Note : after you enable enforcement, it can take up to 15 minutes for the changes to take effect.
Unenforce App Check for your iOS Client
Unenforcing App Check for your app will stop enforcement and will allow all requests from your client to Google OAuth 2.0 endpoints, including unverified requests.
To unenforce App Check for your iOS client, navigate to the edit view of the iOS client and click the UNENFORCE button and confirm your choice.
Note : after unenforcing App Check, it can take up to 15 minutes for the changes to take effect.
Disable App Check for your iOS Client
Disabling App Check for your app will stop all App Check monitoring and enforcement . Consider unenforcing App Check instead so you can continue monitoring metrics for your client.
To disable App Check for your iOS client, navigate to the edit view of the iOS client and turn off the Protect your OAuth client from abuse with Firebase App Check toggle button.
Note : after disabling App Check, it can take up to 15 minutes for the changes to take effect.
Time-based access
Time-based access allows a user to grant your app access to their data for a limited duration to complete an action. Time-based access is available in select Google products during the consent flow, giving users the option to grant access for a limited period of time. An example is the Data Portability API which enables a one-time transfer of data.
When a user grants your application time-based access, the refresh token will expire after the specified duration. Note that refresh tokens may be invalidated earlier under specific circumstances; see these cases for details. The refresh_token_expires_in
field returned in the authorization code exchange response represents the time remaining until the refresh token expires in such cases.
ادامه مطلب
The IETF Best Current Practice OAuth 2.0 for Native Apps establishes many of the best practices documented here.
Implementing Cross-Account Protection
An additional step you should take to protect your users' accounts is implementing Cross-Account Protection by utilizing Google's Cross-Account Protection Service. This service lets you subscribe to security event notifications which provide information to your application about major changes to the user account. You can then use the information to take action depending on how you decide to respond to events.
Some examples of the event types sent to your app by Google's Cross-Account Protection Service are:
-
https://schemas.openid.net/secevent/risc/event-type/sessions-revoked
-
https://schemas.openid.net/secevent/oauth/event-type/token-revoked
-
https://schemas.openid.net/secevent/risc/event-type/account-disabled
See the Protect user accounts with Cross-Account Protection page for more information on how to implement Cross Account Protection and for the full list of available events.
This document explains how applications installed on devices like phones, tablets, and computers use Google's OAuth 2.0 endpoints to authorize access to Google APIs.
OAuth 2.0 به کاربران اجازه می دهد تا داده های خاصی را با یک برنامه به اشتراک بگذارند در حالی که نام کاربری، رمز عبور و سایر اطلاعات خود را خصوصی نگه می دارند. For example, an application can use OAuth 2.0 to obtain permission from users to store files in their Google Drives.
Installed apps are distributed to individual devices, and it is assumed that these apps cannot keep secrets. They can access Google APIs while the user is present at the app or when the app is running in the background.
This authorization flow is similar to the one used for web server applications . The main difference is that installed apps must open the system browser and supply a local redirect URI to handle responses from Google's authorization server.
جایگزین ها
For mobile apps, you may prefer to use Google Sign-in for Android or iOS . The Google Sign-in client libraries handle authentication and user authorization, and they may be simpler to implement than the lower-level protocol described here.
For apps running on devices that do not support a system browser or that have limited input capabilities, such as TVs, game consoles, cameras, or printers, see OAuth 2.0 for TVs & Devices or Sign-In on TVs and Limited Input Devices .
Libraries and samples
We recommend the following libraries and samples to help you implement the OAuth 2.0 flow described in this document:
- AppAuth for Android library
- AppAuth for iOS library
- OAuth for Apps: Windows Samples
پیش نیازها
Enable APIs for your project
Any application that calls Google APIs needs to enable those APIs in the API Console.
برای فعال کردن یک API برای پروژه خود:
- Open the API Library در Google API Console.
- If prompted, select a project, or create a new one.
- را API Library lists all available APIs, grouped by product family and popularity. If the API you want to enable isn't visible in the list, use search to find it, or click View All in the product family it belongs to.
- Select the API you want to enable, then click the Enable button.
- If prompted, enable billing.
- If prompted, read and accept the API's Terms of Service.
Create authorization credentials
Any application that uses OAuth 2.0 to access Google APIs must have authorization credentials that identify the application to Google's OAuth 2.0 server. The following steps explain how to create credentials for your project. Your applications can then use the credentials to access APIs that you have enabled for that project.
- Go to the Credentials page.
- Click Create client .
- The following sections describe the client types that Google's authorization server supports. Choose the client type that is recommended for your application, name your OAuth client, and set the other fields in the form as appropriate.
اندروید
- Select the Android application type.
- Enter a name for the OAuth client. This name is displayed on your project's to identify the client.
- Enter the package name of your Android app. This value is defined in the
package
attribute of the<manifest>
element in your app manifest file. - Enter the SHA-1 signing certificate fingerprint of the app distribution.
- If your app uses app signing by Google Play , copy the SHA-1 fingerprint from the app signing page of the Play Console.
- If you manage your own keystore and signing keys, use the keytool utility included with Java to print certificate information in a human-readable format. Copy the
SHA1
value in theCertificate fingerprints
section of the keytool output. See Authenticating Your Client in the Google APIs for Android documentation for more information.
- (Optional) Verify ownership of your Android application.
- روی ایجاد کلیک کنید.
iOS
- Select the iOS application type.
- Enter a name for the OAuth client. This name is displayed on your project's to identify the client.
- Enter the bundle identifier for your app. The bundle ID is the value of the CFBundleIdentifier key in your app's information property list resource file ( info.plist ). The value is most commonly displayed in the General pane or the Signing & Capabilities pane of the Xcode project editor. The bundle ID is also displayed in the General Information section of the App Information page for the app on Apple's App Store Connect site .
Confirm that you are using the correct bundle ID for your app, as you won't be able to change it if you are using the App Check feature.
- (اختیاری)
Enter your app's App Store ID if the app is published in Apple's App Store. The Store ID is a numeric string included in every Apple App Store URL.
- Open the Apple App Store app on your iOS or iPadOS device.
- برنامه خود را جستجو کنید.
- Select the Share button (square and arrow up symbol).
- کپی پیوند را انتخاب کنید.
- Paste the link into a text editor. The App Store ID is the final part of the URL.
Example:
https://apps.apple.com/app/google/id 284815942
- (اختیاری)
Enter your Team ID. See Locate your Team ID in the Apple Developer Account documentation for more information.
Note: The Team ID field is required if you are enabling App Check for your client. - (اختیاری)
Enable App Check for your iOS app. When you enable App Check, Apple's App Attest service is used to verify that OAuth 2.0 requests originating from your OAuth client are genuine and come from your app. This helps to reduce the risk of app impersonation. Learn more about enabling App Check for your iOS app .
- روی ایجاد کلیک کنید.
UWP
- Select the Universal Windows Platform application type.
- Enter a name for the OAuth client. This name is displayed on your project's to identify the client.
- Enter your app's 12-character Microsoft Store ID. You can find this value in Microsoft Partner Center on the App identity page in the App management section.
- روی ایجاد کلیک کنید.
For UWP apps, the custom URI scheme cannot be longer than 39 characters.
Identify access scopes
Scopes enable your application to only request access to the resources that it needs while also enabling users to control the amount of access that they grant to your application. Thus, there may be an inverse relationship between the number of scopes requested and the likelihood of obtaining user consent.
Before you start implementing OAuth 2.0 authorization, we recommend that you identify the scopes that your app will need permission to access.
The OAuth 2.0 API Scopes document contains a full list of scopes that you might use to access Google APIs.
Obtaining OAuth 2.0 access tokens
The following steps show how your application interacts with Google's OAuth 2.0 server to obtain a user's consent to perform an API request on the user's behalf. Your application must have that consent before it can execute a Google API request that requires user authorization.
Step 1: Generate a code verifier and challenge
Google supports the Proof Key for Code Exchange (PKCE) protocol to make the installed app flow more secure. A unique code verifier is created for every authorization request, and its transformed value, called "code_challenge", is sent to the authorization server to obtain the authorization code.
Create the code verifier
A code_verifier
is a high-entropy cryptographic random string using the unreserved characters [AZ] / [az] / [0-9] / "-" / "." / "_" / "~", with a minimum length of 43 characters and a maximum length of 128 characters.
The code verifier should have enough entropy to make it impractical to guess the value.
Create the code challenge
Two methods of creating the code challenge are supported.
Code Challenge Generation Methods | |
---|---|
S256 (recommended) | The code challenge is the Base64URL (with no padding) encoded SHA256 hash of the code verifier.
|
ساده | The code challenge is the same value as the code verifier generated above.
|
Step 2: Send a request to Google's OAuth 2.0 server
To obtain user authorization, send a request to Google's authorization server at https://accounts.google.com/o/oauth2/v2/auth
. This endpoint handles active session lookup, authenticates the user, and obtains user consent. The endpoint is only accessible over SSL, and it refuses HTTP (non-SSL) connections.
The authorization server supports the following query string parameters for installed applications:
پارامترها | |||||||
---|---|---|---|---|---|---|---|
client_id | مورد نیاز The client ID for your application. You can find this value in the . | ||||||
redirect_uri | مورد نیاز Determines how Google's authorization server sends a response to your app. There are several redirect options available to installed apps, and you will have set up your authorization credentials with a particular redirect method in mind. The value must exactly match one of the authorized redirect URIs for the OAuth 2.0 client, which you configured in your client's . If this value doesn't match an authorized URI, you will get a The table below shows the appropriate
| ||||||
response_type | مورد نیاز Determines whether the Google OAuth 2.0 endpoint returns an authorization code. Set the parameter value to | ||||||
scope | مورد نیاز A space-delimited list of scopes that identify the resources that your application could access on the user's behalf. These values inform the consent screen that Google displays to the user. Scopes enable your application to only request access to the resources that it needs while also enabling users to control the amount of access that they grant to your application. Thus, there is an inverse relationship between the number of scopes requested and the likelihood of obtaining user consent. | ||||||
code_challenge | توصیه می شود Specifies an encoded | ||||||
code_challenge_method | توصیه می شود Specifies what method was used to encode a | ||||||
state | توصیه می شود Specifies any string value that your application uses to maintain state between your authorization request and the authorization server's response. The server returns the exact value that you send as a You can use this parameter for several purposes, such as directing the user to the correct resource in your application, sending nonces, and mitigating cross-site request forgery. Since your | ||||||
login_hint | اختیاری If your application knows which user is trying to authenticate, it can use this parameter to provide a hint to the Google Authentication Server. The server uses the hint to simplify the login flow either by prefilling the email field in the sign-in form or by selecting the appropriate multi-login session. Set the parameter value to an email address or |
Sample authorization URLs
The tabs below show sample authorization URLs for the different redirect URI options.
The URLs are identical except for the value of the redirect_uri
parameter. The URLs also contain the required response_type
and client_id
parameters as well as the optional state
parameter. Each URL contains line breaks and spaces for readability.
Custom URI scheme
https://accounts.google.com/o/oauth2/v2/auth? scope=email%20profile& response_type=code& state=security_token%3D138r5719ru3e1%26url%3Dhttps%3A%2F%2Foauth2.example.com%2Ftoken& redirect_uri=com.example.app%3A/oauth2redirect& client_id=client_id
Loopback IP address
https://accounts.google.com/o/oauth2/v2/auth? scope=email%20profile& response_type=code& state=security_token%3D138r5719ru3e1%26url%3Dhttps%3A%2F%2Foauth2.example.com%2Ftoken& redirect_uri=http%3A//127.0.0.1%3A9004& client_id=client_id
Step 3: Google prompts user for consent
In this step, the user decides whether to grant your application the requested access. At this stage, Google displays a consent window that shows the name of your application and the Google API services that it is requesting permission to access with the user's authorization credentials and a summary of the scopes of access to be granted. The user can then consent to grant access to one or more scopes requested by your application or refuse the request.
Your application doesn't need to do anything at this stage as it waits for the response from Google's OAuth 2.0 server indicating whether any access was granted. That response is explained in the following step.
خطاها
Requests to Google's OAuth 2.0 authorization endpoint may display user-facing error messages instead of the expected authentication and authorization flows. Common error codes and suggested resolutions are listed below.
admin_policy_enforced
The Google Account is unable to authorize one or more scopes requested due to the policies of their Google Workspace administrator. See the Google Workspace Admin help article Control which third-party & internal apps access Google Workspace data for more information about how an administrator may restrict access to all scopes or sensitive and restricted scopes until access is explicitly granted to your OAuth client ID.
disallowed_useragent
The authorization endpoint is displayed inside an embedded user-agent disallowed by Google's OAuth 2.0 Policies .
اندروید
Android developers may encounter this error message when opening authorization requests inandroid.webkit.WebView
. Developers should instead use Android libraries such as Google Sign-In for Android or OpenID Foundation's AppAuth for Android .
Web developers may encounter this error when an Android app opens a general web link in an embedded user-agent and a user navigates to Google's OAuth 2.0 authorization endpoint from your site. Developers should allow general links to open in the default link handler of the operating system, which includes both Android App Links handlers or the default browser app. The Android Custom Tabs library is also a supported option.
iOS
iOS and macOS developers may encounter this error when opening authorization requests inWKWebView
. Developers should instead use iOS libraries such as Google Sign-In for iOS or OpenID Foundation's AppAuth for iOS .
Web developers may encounter this error when an iOS or macOS app opens a general web link in an embedded user-agent and a user navigates to Google's OAuth 2.0 authorization endpoint from your site. Developers should allow general links to open in the default link handler of the operating system, which includes both Universal Links handlers or the default browser app. TheSFSafariViewController
library is also a supported option.
org_internal
The OAuth client ID in the request is part of a project limiting access to Google Accounts in a specific Google Cloud Organization . For more information about this configuration option see the User type section in the Setting up your OAuth consent screen help article.
invalid_grant
If you are using a code verifier and challenge , the code_callenge
parameter is invalid or missing. Ensure that the code_challenge
parameter is set correctly.
When refreshing an access token , the token may have expired or has beeninvalidated. Authenticate the user again and ask for user consent to obtain new tokens. If you are continuing to see this error, ensure that your application has been configured correctly and that you are using the correct tokens and parameters in your request. Otherwise, the user account may have been deleted or disabled.
redirect_uri_mismatch
The redirect_uri
passed in the authorization request does not match an authorized redirect URI for the OAuth client ID. Review authorized redirect URIs in the .
The passed redirect_uri
may be invalid for the client type.
The redirect_uri
parameter may refer to the OAuth out-of-band (OOB) flow that has been deprecated and is no longer supported. Refer to the migration guide to update your integration.
invalid_request
There was something wrong with the request you made. این می تواند به دلایل مختلفی باشد:
- The request was not properly formatted
- The request was missing required parameters
- The request uses an authorization method that Google doesn't support. Verify your OAuth integration uses a recommended integration method
- A custom scheme is used for the redirect uri : If you see the error message Custom URI scheme is not supported on Chrome apps or Custom URI scheme is not enabled for your Android client , it means you are using a custom URI scheme which isn't supported on Chrome apps and is disabled by default on Android. Learn more about custom URI scheme alternatives
Step 4: Handle the OAuth 2.0 server response
The manner in which your application receives the authorization response depends on the redirect URI scheme that it uses. Regardless of the scheme, the response will either contain an authorization code ( code
) or an error ( error
). For example, error=access_denied
indicates that the user declined the request.
If the user grants access to your application, you can exchange the authorization code for an access token and a refresh token as described in the next step.
Step 5: Exchange authorization code for refresh and access tokens
To exchange an authorization code for an access token, call the https://oauth2.googleapis.com/token
endpoint and set the following parameters:
فیلدها | |
---|---|
client_id | The client ID obtained from the . |
client_secret | The client secret obtained from the . |
code | The authorization code returned from the initial request. |
code_verifier | The code verifier you created in Step 1 . |
grant_type | As defined in the OAuth 2.0 specification , this field's value must be set to authorization_code . |
redirect_uri | One of the redirect URIs listed for your project in the for the given client_id . |
The following snippet shows a sample request:
POST /token HTTP/1.1 Host: oauth2.googleapis.com Content-Type: application/x-www-form-urlencoded code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7& client_id=your_client_id& client_secret=your_client_secret& redirect_uri=http://127.0.0.1:9004& grant_type=authorization_code
Google responds to this request by returning a JSON object that contains a short-lived access token and a refresh token.
The response contains the following fields:
فیلدها | |
---|---|
access_token | The token that your application sends to authorize a Google API request. |
expires_in | The remaining lifetime of the access token in seconds. |
id_token | Note: This property is only returned if your request included an identity scope, such as openid , profile , or email . The value is a JSON Web Token (JWT) that contains digitally signed identity information about the user. |
refresh_token | A token that you can use to obtain a new access token. Refresh tokens are valid until the user revokes access or the refresh token expires. Note that refresh tokens are always returned for installed applications. |
refresh_token_expires_in | The remaining lifetime of the refresh token in seconds. This value is only set when the user grants time-based access . |
scope | The scopes of access granted by the access_token expressed as a list of space-delimited, case-sensitive strings. |
token_type | The type of token returned. At this time, this field's value is always set to Bearer . |
The following snippet shows a sample response:
{ "access_token": "1/fFAGRNJru1FTz70BzhT3Zg", "expires_in": 3920, "token_type": "Bearer", "scope": "https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/calendar.readonly", "refresh_token": "1//xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI" }
Step 6: Check which scopes users granted
When requesting multiple permissions (scopes), users may not grant your app access to all of them. Your app must verify which scopes were actually granted and gracefully handle situations where some permissions are denied, typically by disabling the features that rely on those denied scopes.
با این حال، استثناهایی وجود دارد. Google Workspace Enterprise apps with domain-wide delegation of authority , or apps marked as Trusted , bypass the granular permissions consent screen. For these apps, users won't see the granular permission consent screen. Instead, your app will either receive all requested scopes or none.
For more detailed information, see How to handle granular permissions .
To check whether the user has granted your application access to a particular scope, exam the scope
field in the access token response. The scopes of access granted by the access_token expressed as a list of space-delimited, case-sensitive strings.
For example, the following sample access token response indicates that the user has granted your application access to the read-only Drive activity and Calendar events permissions:
{ "access_token": "1/fFAGRNJru1FTz70BzhT3Zg", "expires_in": 3920, "token_type": "Bearer", "scope": "https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/calendar.readonly", "refresh_token": "1//xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI" }
Calling Google APIs
After your application obtains an access token, you can use the token to make calls to a Google API on behalf of a given user account if the scope(s) of access required by the API have been granted. To do this, include the access token in a request to the API by including either an access_token
query parameter or an Authorization
HTTP header Bearer
value. When possible, the HTTP header is preferable, because query strings tend to be visible in server logs. In most cases you can use a client library to set up your calls to Google APIs (for example, when calling the Drive Files API ).
You can try out all the Google APIs and view their scopes at the OAuth 2.0 Playground .
HTTP GET examples
A call to the drive.files
endpoint (the Drive Files API) using the Authorization: Bearer
HTTP header might look like the following. Note that you need to specify your own access token:
GET /drive/v2/files HTTP/1.1 Host: www.googleapis.com Authorization: Bearer access_token
Here is a call to the same API for the authenticated user using the access_token
query string parameter:
GET https://www.googleapis.com/drive/v2/files?access_token=access_token
curl
examples
You can test these commands with the curl
command-line application. Here's an example that uses the HTTP header option (preferred):
curl -H "Authorization: Bearer access_token" https://www.googleapis.com/drive/v2/files
Or, alternatively, the query string parameter option:
curl https://www.googleapis.com/drive/v2/files?access_token=access_token
Refreshing an access token
Access tokens periodically expire and become invalid credentials for a related API request. You can refresh an access token without prompting the user for permission (including when the user is not present) if you requested offline access to the scopes associated with the token.
To refresh an access token, your application sends an HTTPS POST
request to Google's authorization server ( https://oauth2.googleapis.com/token
) that includes the following parameters:
فیلدها | |
---|---|
client_id | The client ID obtained from the API Console. |
client_secret | The client secret obtained from the API Console. (The client_secret is not applicable to requests from clients registered as Android, iOS, or Chrome applications.) |
grant_type | As defined in the OAuth 2.0 specification , this field's value must be set to refresh_token . |
refresh_token | The refresh token returned from the authorization code exchange. |
The following snippet shows a sample request:
POST /token HTTP/1.1 Host: oauth2.googleapis.com Content-Type: application/x-www-form-urlencoded client_id=your_client_id& client_secret=your_client_secret& refresh_token=refresh_token& grant_type=refresh_token
As long as the user has not revoked the access granted to the application, the token server returns a JSON object that contains a new access token. The following snippet shows a sample response:
{ "access_token": "1/fFAGRNJru1FTz70BzhT3Zg", "expires_in": 3920, "scope": "https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/calendar.readonly", "token_type": "Bearer" }
Note that there are limits on the number of refresh tokens that will be issued; one limit per client/user combination, and another per user across all clients. You should save refresh tokens in long-term storage and continue to use them as long as they remain valid. If your application requests too many refresh tokens, it may run into these limits, in which case older refresh tokens will stop working.
Revoking a token
In some cases a user may wish to revoke access given to an application. A user can revoke access by visiting Account Settings . See the Remove site or app access section of the Third-party sites & apps with access to your account support document for more information.
It is also possible for an application to programmatically revoke the access given to it. Programmatic revocation is important in instances where a user unsubscribes, removes an application, or the API resources required by an app have significantly changed. In other words, part of the removal process can include an API request to ensure the permissions previously granted to the application are removed.
To programmatically revoke a token, your application makes a request to https://oauth2.googleapis.com/revoke
and includes the token as a parameter:
curl -d -X -POST --header "Content-type:application/x-www-form-urlencoded" \ https://oauth2.googleapis.com/revoke?token={token}
The token can be an access token or a refresh token. If the token is an access token and it has a corresponding refresh token, the refresh token will also be revoked.
If the revocation is successfully processed, then the HTTP status code of the response is 200
. For error conditions, an HTTP status code 400
is returned along with an error code.
App redirect methods
Custom URI scheme (Android, iOS, UWP)
Custom URI schemes are a form of deeplinking that use a custom-defined scheme to open your app.
Alternative to using custom URI schemes on Android
Use the Google Sign-In for Android SDK which delivers the OAuth 2.0 response directly to your app, eliminating the need for a redirect URI.
How to migrate to the Google Sign-In for Android SDK
If you use a custom scheme for your OAuth integration on Android, you would need to complete the following actions to fully migrate to using the recommended Google Sign-In for Android SDK:
- Update your code to use the Google Sign-In SDK.
- Disable support for custom scheme in the Google API Console.
Follow the below steps to migrate to the Google Sign-In Android SDK:
- Update your code to use the Google Sign-In Android SDK:
- Examine your code to identify where you are sending a request to Google's OAuth 2.0 server ; if using a custom scheme, your request would look like the below:
https://accounts.google.com/o/oauth2/v2/auth? scope=<SCOPES>& response_type=code& &state=<STATE>& redirect_uri=com.example.app:/oauth2redirect& client_id=<CLIENT_ID>
com.example.app:/oauth2redirect
is the custom scheme redirect URI in the above example. See theredirect_uri
parameter definition for more details about the format of the custom URI scheme value. - Make note of the
scope
andclient_id
request parameters which you would need to configure the Google Sign-In SDK. - Follow the Start Integrating Google Sign-In into Your Android App instructions to set up the SDK. You can skip the Get your backend server's OAuth 2.0 client ID step as you would re-use the
client_id
you retrieved from the previous step. - Follow the Enabling Server-Side API access instructions. این شامل مراحل زیر است:
- Use the
getServerAuthCode
method to retrieve an auth code for the scopes you are requesting permission for. - Send the auth code to your app's backend to exchange it for an access & refresh token.
- Use the retrieved access token to make calls to Google APIs on behalf of the user.
- Use the
- Examine your code to identify where you are sending a request to Google's OAuth 2.0 server ; if using a custom scheme, your request would look like the below:
- Disable support for custom scheme in the Google API Console:
- Go to your OAuth 2.0 credentials list and select your Android client.
- Navigate to the Advanced Settings section, uncheck the Enable Custom URI Scheme checkbox, and click Save to disable custom URI scheme support.
Enable custom URI scheme
If the recommended alternative does not work for you, you can enable custom URI schemes for your Android client by following the below instructions:- Go to your OAuth 2.0 credentials list and select your Android client.
- Navigate to the Advanced Settings section, check the Enable Custom URI Scheme checkbox, and click Save to enable custom URI scheme support.
Alternative to using custom URI schemes on Chrome apps
Use the Chrome Identity API which delivers the OAuth 2.0 response directly to your app, eliminating the need for a redirect URI.
Loopback IP address (macOS, Linux, Windows desktop)
To receive the authorization code using this URL, your application must be listening on the local web server. That is possible on many, but not all, platforms. However, if your platform supports it, this is the recommended mechanism for obtaining the authorization code.
When your app receives the authorization response, for best usability it should respond by displaying an HTML page that instructs the user to close the browser and return to your app.
استفاده توصیه شده | macOS, Linux, and Windows desktop (but not Universal Windows Platform) apps |
Form values | Set the application type to Desktop app . |
Manual copy/paste (Deprecated)
Protect your apps
Verify app ownership (Android, Chrome)
You can verify ownership of your application to reduce the risk of app impersonation.
اندروید
To complete the verification process, you can use your Google Play Developer Account if you have one and your app is registered on the Google Play Console . The following requirements must be met for a successful verification:
- You must have a registered application in the Google Play Console with the same package name and SHA-1 signing certificate fingerprint as the Android OAuth client you are completing the verification for.
- You must have Admin permission for the app in the Google Play Console. Learn more about access management in the Google Play Console.
In the Verify App Ownership section of the Android client, click the Verify Ownership button to complete the verification process.
If the verification is successful, a notification will be displayed to confirm the success of the verification process. Otherwise, an error prompt will be shown.
To fix a failed verification, try the following:
- Make sure the app you are verifying is a registered app in the Google Play Console.
- Make sure you have Admin permission for the app in the Google Play Console.
کروم
To complete the verification process, you would use your Chrome Web Store Developer account. The following requirements must be met for a successful verification:
- You must have a registered item in the Chrome Web Store Developer Dashboard with the same item ID as the Chrome Extension OAuth client you are completing the verification for.
- You must be a publisher for the Chrome Web Store item. Learn more about access management in the Chrome Web Store Developer Dashboard.
In the Verify App Ownership section of the Chrome Extension client, click the Verify Ownership button to complete the verification process.
Note: Wait a few minutes before completing the verification process after granting access to your account.
If the verification is successful, a notification will be displayed to confirm the success of the verification process. Otherwise, an error prompt will be shown.
To fix a failed verification, try the following:
- Make sure there is a registered item in the Chrome Web Store Developer Dashboard with the same item ID as the Chrome Extension OAuth client you are completing the verification for.
- Make sure you are a publisher for the app, that is, you must either be the individual publisher of the app or a member of the group publisher of the app. Learn more about access management in the Chrome Web Store Developer Dashboard.
- If you just updated your group publisher list, verify that the group publisher membership list is synced in the Chrome Web Store Developer Dashboard. Learn more about syncing your publisher membership list.
App Check (iOS only)
The App Check feature helps safeguard your iOS applications from unauthorized usage by using Apple's App Attest service to verify that requests made to Google OAuth 2.0 endpoints originate from your authentic applications. This helps to reduce the risk of app impersonation.
Enable App Check for your iOS Client
The following requirements must be met to successfully enable App Check for your iOS client:- You must specify a team ID for your iOS client.
- You must not use a wildcard in your bundle ID since it can resolve to more than one app. This means that the bundle ID must not include the asterisk (*) symbol.
After enabling App Check, you will start seeing metrics related to OAuth requests from your client in the edit view of the OAuth client. Requests from unverified sources won't be blocked until you enforce App Check . The information in the metrics monitoring page can help you determine when to start enforcement.
You might see errors related to the App Check feature when enabling App Check for your iOS app. To fix these errors, try the following:
- Verify that the bundle ID and team ID you specified are valid.
- Verify that you are not using a wildcard for the bundle ID.
Enforce App Check for your iOS Client
Enabling App Check for your app does not automatically block unrecognized requests. To enforce this protection, go to the edit view of your iOS client. There, you will see App Check metrics to the right of the page under the Google Identity for iOS section. The metrics include the following information:- Number of verified requests - requests that have a valid App Check token. After you enable App Check enforcement, only requests in this category will succeed.
- Number of unverified requests: likely outdated client requests - requests missing an App Check token; these request may be from an older version of your app that doesn't include an App Check implementation.
- Number of unverified requests: unknown origin requests - requests missing an App Check token that don't look like they are coming from your app.
- Number of unverified requests: invalid requests - requests with an invalid App Check token, which may be from an inauthentic client attempting to impersonate your app, or from emulated environments.
To enforce App Check, click the ENFORCE button and confirm your choice. Once enforcement is active, all unverified requests from your client will be rejected.
Note : after you enable enforcement, it can take up to 15 minutes for the changes to take effect.
Unenforce App Check for your iOS Client
Unenforcing App Check for your app will stop enforcement and will allow all requests from your client to Google OAuth 2.0 endpoints, including unverified requests.
To unenforce App Check for your iOS client, navigate to the edit view of the iOS client and click the UNENFORCE button and confirm your choice.
Note : after unenforcing App Check, it can take up to 15 minutes for the changes to take effect.
Disable App Check for your iOS Client
Disabling App Check for your app will stop all App Check monitoring and enforcement . Consider unenforcing App Check instead so you can continue monitoring metrics for your client.
To disable App Check for your iOS client, navigate to the edit view of the iOS client and turn off the Protect your OAuth client from abuse with Firebase App Check toggle button.
Note : after disabling App Check, it can take up to 15 minutes for the changes to take effect.
Time-based access
Time-based access allows a user to grant your app access to their data for a limited duration to complete an action. Time-based access is available in select Google products during the consent flow, giving users the option to grant access for a limited period of time. An example is the Data Portability API which enables a one-time transfer of data.
When a user grants your application time-based access, the refresh token will expire after the specified duration. Note that refresh tokens may be invalidated earlier under specific circumstances; see these cases for details. The refresh_token_expires_in
field returned in the authorization code exchange response represents the time remaining until the refresh token expires in such cases.
ادامه مطلب
The IETF Best Current Practice OAuth 2.0 for Native Apps establishes many of the best practices documented here.
Implementing Cross-Account Protection
An additional step you should take to protect your users' accounts is implementing Cross-Account Protection by utilizing Google's Cross-Account Protection Service. This service lets you subscribe to security event notifications which provide information to your application about major changes to the user account. You can then use the information to take action depending on how you decide to respond to events.
Some examples of the event types sent to your app by Google's Cross-Account Protection Service are:
-
https://schemas.openid.net/secevent/risc/event-type/sessions-revoked
-
https://schemas.openid.net/secevent/oauth/event-type/token-revoked
-
https://schemas.openid.net/secevent/risc/event-type/account-disabled
See the Protect user accounts with Cross-Account Protection page for more information on how to implement Cross Account Protection and for the full list of available events.