บริการบางอย่างของ Google เช่น ไดรฟ์, Gmail และอื่นๆ อีกมากมายมี API สาธารณะที่คุณสามารถใช้เพื่อสร้างแอปที่ช่วยให้ผู้ใช้ทํางานกับข้อมูลในบริการเหล่านี้ได้ หากต้องการเข้าถึงบริการเหล่านี้ แอปต้องใช้ขั้นตอนไคลเอ็นต์ OAuth 2.0 เพื่อขอความยินยอมจากผู้ใช้และรับโทเค็นเพื่อการเข้าถึง ซึ่งให้สิทธิ์เข้าถึง API
คุณสามารถใช้ไลบรารีการลงชื่อเข้าใช้ Google ซึ่งใช้ขั้นตอน 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:callback
หรือ addScopes:presentingWindow:callback
เพื่อขอให้ผู้ใช้ให้สิทธิ์เข้าถึงเพิ่มเติมแก่แอปของคุณ
เช่น หากต้องการขอสิทธิ์เข้าถึงแบบอ่านอย่างเดียวในไดรฟ์ของผู้ใช้ ให้ทําดังนี้
Swift
let additionalScopes = ["https://www.googleapis.com/auth/drive.readonly"]
GIDSignIn.sharedInstance.addScopes(additionalScopes, presenting: self) { user, error in
guard error == nil else { return }
guard let user = user else { return }
// Check if the user granted access to the scopes you requested.
}
Objective-C
NSArray *additionalScopes = @[ @"https://www.googleapis.com/auth/drive.readonly" ];
[GIDSignIn.sharedInstance addScopes:additionalScopes
presentingViewController:self
callback:^(GIDGoogleUser * _Nullable user,
NSError * _Nullable error) {
if (error) { return; }
if (user == nil) { return; }
// Check if the user granted access to the scopes you requested.
}];
3. เรียก API ด้วยโทเค็นใหม่
เพื่อให้การเรียก Google API ของคุณแนบโทเค็นเพื่อการเข้าถึงที่ไม่หมดอายุเสมอ ให้ตัดการเรียกไว้ในบล็อก doWithFreshTokens:
โดยทําดังนี้
Swift
user.authentication.do { authentication, error in
guard error == nil else { return }
guard let authentication = authentication else { return }
// Get the access token to attach it to a REST or gRPC request.
let accessToken = authentication.accessToken
// Or, get an object that conforms to GTMFetcherAuthorizationProtocol for
// use with GTMAppAuth and the Google APIs client library.
let authorizer = authentication.fetcherAuthorizer()
}
Objective-C
[user.authentication doWithFreshTokens:^(GIDAuthentication * _Nullable authentication,
NSError * _Nullable error) {
if (error) { return; }
if (authentication == nil) { return; }
// Get the access token to attach it to a REST or gRPC request.
NSString *accessToken = authentication.accessToken;
// Or, get an object that conforms to GTMFetcherAuthorizationProtocol for
// use with GTMAppAuth and the Google APIs client library.
id<GTMFetcherAuthorizationProtocol> authorizer = [authentication fetcherAuthorizer];
}];
ใช้โทเค็นเพื่อการเข้าถึงเพื่อเรียก API โดยระบุโทเค็นเพื่อการเข้าถึงในส่วนหัวของคําขอ REST หรือ gRPC (Authorization: Bearer ACCESS_TOKEN
) หรือโดยใช้ตัวให้สิทธิ์การดึงข้อมูลกับไลบรารีของไคลเอ็นต์ Google APIs