중요:
2024년 5월 1일부터 Apple에서는 GoogleSignIn-iOS와 같이 흔히 사용되는 SDK를 사용하는 iOS 애플리케이션의 개인 정보 보호 매니페스트 및 서명을
요구합니다. 2024년 5월 1일 전에 GoogleSignIn-iOS v7.1.0 이상으로 업그레이드하세요.
업그레이드 가이드를 따르세요.
iOS 앱에서 Google API에 액세스
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
Drive, Gmail 등 일부 Google 서비스는 공개 API를 제공합니다.
사용자가 이러한 서비스에서 자신의 데이터로 작업하는 데 도움이 되는 앱을 만드는 데 사용할 수 있는
제공합니다 이러한 서비스에 액세스하려면 앱에서 OAuth 2.0 중 하나를 구현해야 합니다.
사용자의 동의를 얻고 액세스 토큰을 얻는 클라이언트 플로우를 사용하여
액세스할 수 있습니다
OAuth 2.0 흐름을 구현하는 Google 로그인 라이브러리를 사용할 수 있습니다.
로그인한 사용자의 액세스 토큰을 얻을 수 있습니다.
시작하기 전에
기본 Google 로그인 통합을 완료해야 합니다.
1. 부여된 범위 확인
Google API를 호출하기 전에 이미 어느 범위가 사용되었는지 확인하세요.
GIDGoogleUser
의 grantedScopes
속성을 사용하여 앱에 부여됩니다.
Swift
let driveScope = "https://www.googleapis.com/auth/drive.readonly"
let grantedScopes = user.grantedScopes
if grantedScopes == nil || !grantedScopes!.contains(driveScope) {
// Request additional Drive scope.
}
Objective-C
NSString *driveScope = @"https://www.googleapis.com/auth/drive.readonly";
// Check if the user has granted the Drive scope
if (![user.grantedScopes containsObject:driveScope]) {
// request additional drive scope
}
사용자가 특정 범위를 부여했는지 여부에 따라
특정 IP 주소를 지원하려면 추가 범위를 요청해야 함
상호작용하지 않습니다.
2. 추가 범위 요청
추가 범위를 요청해야 하는 경우
addScopes:presentingViewController:completion
또는
addScopes:presentingWindow:completion
: 사용자에게 앱 권한 부여를 요청합니다.
추가 액세스를 제공합니다.
예를 들어 사용자의 Drive 파일에 대한 읽기 전용 액세스 권한을 요청하려면 다음 안내를 따르세요.
Swift
let additionalScopes = ["https://www.googleapis.com/auth/drive.readonly"]
guard let currentUser = GIDSignIn.sharedInstance.currentUser else {
return ; /* Not signed in. */
}
currentUser.addScopes(additionalScopes, presenting: self) { signInResult, error in
guard error == nil else { return }
guard let signInResult = signInResult else { 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;
[currentUser addScopes:additionalScopes
presentingViewController:self
completion:^(GIDSignInResult * _Nullable signInResult,
NSError * _Nullable error) {
if (error) { return; }
if (signInResult == nil) { return; }
// Check if the user granted access to the scopes you requested.
}];
3. 새 토큰으로 API 호출
Google API 호출에 만료되지 않은 액세스 토큰이 항상 포함되도록 하기 위해
refreshTokensIfNeededWithCompletion:
블록에 호출을 래핑합니다.
Swift
currentUser.refreshTokensIfNeeded { user, error in
guard error == nil else { return }
guard let user = user else { return }
// Get the access token to attach it to a REST or gRPC request.
let accessToken = user.accessToken.tokenString
// Or, get an object that conforms to GTMFetcherAuthorizationProtocol for
// use with GTMAppAuth and the Google APIs client library.
let authorizer = user.fetcherAuthorizer()
}
Objective-C
[currentUser refreshTokensIfNeededWithCompletion:^(
GIDGoogleUser * _Nullable user,
NSError * _Nullable error) {
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 = [user fetcherAuthorizer];
}];
다음과 같이 액세스 토큰을 사용하여 API를 호출합니다.
REST 또는 gRPC 요청의 헤더 (Authorization: Bearer ACCESS_TOKEN
)
또는 geter 승인자를
Google API 클라이언트 라이브러리.
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 Oracle 계열사의 등록 상표입니다.
최종 업데이트: 2025-07-25(UTC)
[null,null,["최종 업데이트: 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/)."]]