บริการบางอย่างของ 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)
หรือใช้ตัวให้สิทธิ์ Fetcher กับ
ไลบรารีของไคลเอ็นต์ Google APIs