अहम जानकारी: Apple, 1 मई, 2024 से GoogleSignIn-iOS के साथ-साथ, आम तौर पर इस्तेमाल किए जाने वाले SDK टूल का इस्तेमाल करने वाले iOS ऐप्लिकेशन के लिए, निजता मेनिफ़ेस्ट और हस्ताक्षर स्वीकार करना ज़रूरी कर देगा. 1 मई, 2024 से पहले, GoogleSignIn-iOS v7.1.0+ वर्शन पर अपग्रेड करें. हमारी अपग्रेड गाइड देखें.
संग्रह की मदद से व्यवस्थित रहें
अपनी प्राथमिकताओं के आधार पर, कॉन्टेंट को सेव करें और कैटगरी में बांटें.
Drive, Gmail जैसी Google की कुछ सेवाएं, सार्वजनिक एपीआई उपलब्ध कराती हैं
जिसका इस्तेमाल करके, ऐसे ऐप्लिकेशन बनाए जा सकते हैं जो इन ऐप्लिकेशन में लोगों के डेटा का इस्तेमाल करने में उनकी मदद करते हैं
सेवाओं. इन सेवाओं को ऐक्सेस करने के लिए, ऐप्लिकेशन को OAuth 2.0 में से किसी एक को लागू करना होगा
क्लाइंट फ़्लो, उपयोगकर्ताओं की सहमति लेने और ऐक्सेस टोकन पाने के लिए किया जाता है.
कुछ एपीआई ऐक्सेस कर सकता है.
Google साइन-इन लाइब्रेरी का इस्तेमाल किया जा सकता है.यह लाइब्रेरी,
ताकि साइन-इन किए हुए उपयोगकर्ता के लिए ऐक्सेस टोकन मिल सकें.
Google API को कॉल करने से पहले, देख लें कि कौनसे स्कोप पहले ही इस्तेमाल किए जा चुके हैं
GIDGoogleUser की grantedScopes प्रॉपर्टी का इस्तेमाल करके, आपके ऐप्लिकेशन को दिया गया:
NSString*driveScope=@"https://www.googleapis.com/auth/drive.readonly";// Check if the user has granted the Drive scopeif(![user.grantedScopescontainsObject:driveScope]){// request additional drive scope}
उपयोगकर्ता ने कोई खास दायरा दिया है या नहीं, इसके आधार पर
किसी अतिरिक्त दायरे का अनुरोध करने की ज़रूरत है, ताकि
इंटरैक्शन.
2. अतिरिक्त दायरों का अनुरोध करें
अगर आपको अतिरिक्त दायरों का अनुरोध करना है, तो
addScopes:presentingViewController:completion या
उपयोगकर्ता से आपका ऐप्लिकेशन इस्तेमाल करने का अनुरोध करने के लिए addScopes:presentingWindow:completion
का अतिरिक्त ऐक्सेस है.
उदाहरण के लिए, उपयोगकर्ता की Drive की फ़ाइलों को रीड ओनली ऐक्सेस देने का अनुरोध करने के लिए:
Swift
letadditionalScopes=["https://www.googleapis.com/auth/drive.readonly"]guardletcurrentUser=GIDSignIn.sharedInstance.currentUserelse{return;/* Not signed in. */}currentUser.addScopes(additionalScopes,presenting:self){signInResult,erroringuarderror==nilelse{return}guardletsignInResult=signInResultelse{return}// Check if the user granted access to the scopes you requested.}
Objective-C
NSArray*additionalScopes=@[@"https://www.googleapis.com/auth/drive.readonly"];GIDGoogleUser*currentUser=GIDSignIn.sharedInstance.currentUser;[currentUseraddScopes:additionalScopespresentingViewController:selfcompletion:^(GIDSignInResult*_NullablesignInResult,NSError*_Nullableerror){if(error){return;}if(signInResult==nil){return;}// Check if the user granted access to the scopes you requested.}];
3. नए टोकन के साथ एपीआई कॉल करें
यह पक्का करने के लिए कि आपके Google API कॉल में हमेशा ऐसे ऐक्सेस टोकन मौजूद रहें जिनकी समयसीमा खत्म न हुई हो
इस ईमेल के साथ, कॉल को refreshTokensIfNeededWithCompletion: ब्लॉक में रैप करें:
Swift
currentUser.refreshTokensIfNeeded{user,erroringuarderror==nilelse{return}guardletuser=userelse{return}// Get the access token to attach it to a REST or gRPC request.letaccessToken=user.accessToken.tokenString// Or, get an object that conforms to GTMFetcherAuthorizationProtocol for// use with GTMAppAuth and the Google APIs client library.letauthorizer=user.fetcherAuthorizer()}
Objective-C
[currentUserrefreshTokensIfNeededWithCompletion:^(GIDGoogleUser*_Nullableuser,NSError*_Nullableerror){if(error){return;}if(user==nil){return;}// Get the access token to attach it to a REST or gRPC request.NSString*accessToken=user.accessToken.tokenString;// Or, get an object that conforms to GTMFetcherAuthorizationProtocol for// use with GTMAppAuth and the Google APIs client library.id<GTMFetcherAuthorizationProtocol>authorizer=[userfetcherAuthorizer];}];
एपीआई को कॉल करने के लिए, ऐक्सेस टोकन का इस्तेमाल करें. इसके लिए,
किसी REST या gRPC अनुरोध (Authorization: Bearer ACCESS_TOKEN) का हेडर,
या फ़ेचर के लिए अनुमति देने वाले टूल का इस्तेमाल करके
Google API क्लाइंट लाइब्रेरी.
[[["समझने में आसान है","easyToUnderstand","thumb-up"],["मेरी समस्या हल हो गई","solvedMyProblem","thumb-up"],["अन्य","otherUp","thumb-up"]],[["वह जानकारी मौजूद नहीं है जो मुझे चाहिए","missingTheInformationINeed","thumb-down"],["बहुत मुश्किल है / बहुत सारे चरण हैं","tooComplicatedTooManySteps","thumb-down"],["पुराना","outOfDate","thumb-down"],["अनुवाद से जुड़ी समस्या","translationIssue","thumb-down"],["सैंपल / कोड से जुड़ी समस्या","samplesCodeIssue","thumb-down"],["अन्य","otherDown","thumb-down"]],["आखिरी बार 2025-07-25 (UTC) को अपडेट किया गया."],[[["\u003cp\u003eGoogle services like Drive and Gmail offer public APIs for building apps that interact with user data, requiring OAuth 2.0 for user consent and access tokens.\u003c/p\u003e\n"],["\u003cp\u003eBefore making API calls, verify granted scopes using \u003ccode\u003egrantedScopes\u003c/code\u003e and request additional scopes if needed with \u003ccode\u003eaddScopes\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eEnsure API calls have unexpired access tokens by wrapping them in \u003ccode\u003erefreshTokensIfNeededWithCompletion\u003c/code\u003e to refresh tokens.\u003c/p\u003e\n"],["\u003cp\u003eAccess tokens can be used directly in REST/gRPC requests or with the Google APIs Client Library for seamless integration.\u003c/p\u003e\n"],["\u003cp\u003eBasic Google Sign-In integration is a prerequisite before using these features.\u003c/p\u003e\n"]]],[],null,["# Access Google APIs in an iOS app\n\nSome Google services, such as Drive, Gmail, and many others, provide public APIs\nthat you can use to create apps that help users work with their data in these\nservices. To access these services, apps must implement one of the OAuth 2.0\nclient flows to get consent from users and obtain *access tokens*, which grant\naccess to the APIs.\n\nYou can use the Google Sign-In library, which implements the OAuth 2.0 flow for\nyou, to get access tokens for the signed-in user.\n\nBefore you begin\n----------------\n\nYou must complete the [basic Google Sign-In integration](/identity/sign-in/ios/sign-in).\n\n1. Check which scopes have been granted\n---------------------------------------\n\nBefore you make a call to a Google API, check which scopes have already been\ngranted to your app, using the `grantedScopes` property of `GIDGoogleUser`: \n\n### Swift\n\n let driveScope = \"https://www.googleapis.com/auth/drive.readonly\"\n let grantedScopes = user.grantedScopes\n if grantedScopes == nil || !grantedScopes!.contains(driveScope) {\n // Request additional Drive scope.\n }\n\n### Objective-C\n\n NSString *driveScope = @\"https://www.googleapis.com/auth/drive.readonly\";\n\n // Check if the user has granted the Drive scope\n if (![user.grantedScopes containsObject:driveScope]) {\n // request additional drive scope\n }\n\nBased on whether or not a certain scope has been granted by the user, you might\nneed to make a request for an additional scope in order to support a particular\ninteraction.\n\n2. Request additional scopes\n----------------------------\n\nIf you need to request additional scopes, call\n`addScopes:presentingViewController:completion` or\n`addScopes:presentingWindow:completion` to ask the user to grant your app\nadditional access.\n\nFor example, to request read-only access to a user's Drive files: \n\n### Swift\n\n let additionalScopes = [\"https://www.googleapis.com/auth/drive.readonly\"]\n guard let currentUser = GIDSignIn.sharedInstance.currentUser else {\n return ; /* Not signed in. */\n }\n\n currentUser.addScopes(additionalScopes, presenting: self) { signInResult, error in\n guard error == nil else { return }\n guard let signInResult = signInResult else { return }\n\n // Check if the user granted access to the scopes you requested.\n }\n\n### Objective-C\n\n NSArray *additionalScopes = @[ @\"https://www.googleapis.com/auth/drive.readonly\" ];\n GIDGoogleUser *currentUser = GIDSignIn.sharedInstance.currentUser;\n\n [currentUser addScopes:additionalScopes\n presentingViewController:self\n completion:^(GIDSignInResult * _Nullable signInResult,\n NSError * _Nullable error) {\n if (error) { return; }\n if (signInResult == nil) { return; }\n\n // Check if the user granted access to the scopes you requested.\n }];\n\n3. Make an API call with fresh tokens\n-------------------------------------\n\nTo ensure that your Google API calls always have unexpired access tokens\nattached, wrap the calls in a `refreshTokensIfNeededWithCompletion:` block: \n\n### Swift\n\n currentUser.refreshTokensIfNeeded { user, error in\n guard error == nil else { return }\n guard let user = user else { return }\n\n // Get the access token to attach it to a REST or gRPC request.\n let accessToken = user.accessToken.tokenString\n\n // Or, get an object that conforms to GTMFetcherAuthorizationProtocol for\n // use with GTMAppAuth and the Google APIs client library.\n let authorizer = user.fetcherAuthorizer()\n }\n\n### Objective-C\n\n [currentUser refreshTokensIfNeededWithCompletion:^(\n GIDGoogleUser * _Nullable user,\n NSError * _Nullable error) {\n if (error) { return; }\n if (user == nil) { return; }\n\n // Get the access token to attach it to a REST or gRPC request.\n NSString *accessToken = user.accessToken.tokenString;\n\n // Or, get an object that conforms to GTMFetcherAuthorizationProtocol for\n // use with GTMAppAuth and the Google APIs client library.\n id\u003cGTMFetcherAuthorizationProtocol\u003e authorizer = [user fetcherAuthorizer];\n }];\n\nUse the access token to call the API, by either including the access token in\nthe header of a REST or gRPC request (`Authorization: Bearer `\u003cvar translate=\"no\"\u003eACCESS_TOKEN\u003c/var\u003e),\nor by using the fetcher authorizer with the\n[Google APIs Client Library](https://github.com/google/google-api-objectivec-client-for-rest/)."]]