บริการบางอย่างของ 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 API