เข้าถึง Google APIs ในแอป iOS

บริการบางอย่างของ 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: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 ด้วยโทเค็นใหม่

ให้รวมการเรียกใช้บล็อก refreshTokensIfNeededWithCompletion: ไว้เพื่อให้การเรียก Google API แนบโทเค็นเพื่อการเข้าถึงที่ยังไม่หมดอายุเสมอ ดังนี้

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