บริการบางอย่างของ Google เช่น ไดรฟ์, Gmail และอื่นๆ อีกมากมายมี API สาธารณะ ซึ่งคุณสามารถใช้ในการสร้างแอปที่ช่วยให้ผู้ใช้สามารถทำงานกับข้อมูลของตนในส่วน บริการต่างๆ แอปต้องใช้ OAuth 2.0 รายการใดรายการหนึ่งเพื่อเข้าถึงบริการเหล่านี้ โฟลว์ไคลเอ็นต์เพื่อรับความยินยอมจากผู้ใช้ และรับโทเค็นเพื่อการเข้าถึง ซึ่งเป็น สิทธิ์เข้าถึง API
คุณสามารถใช้ไลบรารี Google Sign-In ซึ่งใช้ขั้นตอน OAuth 2.0 สำหรับ คุณเพื่อรับโทเค็นเพื่อการเข้าถึงสำหรับผู้ใช้ที่ลงชื่อเข้าใช้
ก่อนเริ่มต้น
คุณต้องดำเนินการผสานรวม Google Sign-In พื้นฐานให้เสร็จสมบูรณ์
1. ตรวจสอบขอบเขตที่ได้รับสิทธิ์
ก่อนเรียกใช้ Google API ให้ตรวจสอบว่าขอบเขตใดที่เคยไป มอบให้แก่แอปของคุณ โดยใช้พร็อพเพอร์ตี้ grantedScopes
ของ GIDGoogleUser
:
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 }
ขึ้นอยู่กับว่าผู้ใช้ให้สิทธิ์ขอบเขตหนึ่งๆ หรือไม่ คุณอาจ คุณต้องส่งคำขอขอบเขตเพิ่มเติมเพื่อสนับสนุน การโต้ตอบ
2. ขอขอบเขตเพิ่มเติม
หากต้องการขอขอบเขตเพิ่มเติม โปรดโทรติดต่อ addScopes:presentingViewController:completion
หรือ addScopes:presentingWindow:completion
เพื่อขอให้ผู้ใช้ให้สิทธิ์แอปของคุณ การเข้าถึงเพิ่มเติม
เช่น หากต้องการขอสิทธิ์การเข้าถึงแบบอ่านอย่างเดียวสำหรับไฟล์ในไดรฟ์ของผู้ใช้ ให้ทำดังนี้
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
) หรือโดยใช้ตัวให้สิทธิ์ตัวดึงข้อมูลกับ ไลบรารีของไคลเอ็นต์ Google APIs