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

บริการบางอย่างของ Google เช่น ไดรฟ์, Gmail และอื่นๆ อีกมากมายมี API สาธารณะที่คุณสามารถใช้สร้างแอปเพื่อช่วยให้ผู้ใช้ทำงานกับข้อมูลของตนในบริการเหล่านี้ได้ หากต้องการเข้าถึงบริการเหล่านี้ แอปต้องใช้โฟลว์ไคลเอ็นต์ OAuth 2.0 อย่างใดอย่างหนึ่งเพื่อขอความยินยอมจากผู้ใช้และรับโทเค็นเพื่อการเข้าถึง ซึ่งให้สิทธิ์เข้าถึง API

คุณสามารถใช้ไลบรารี Google Sign-In ซึ่งใช้ขั้นตอน OAuth 2.0 สำหรับคุณเพื่อรับโทเค็นเพื่อการเข้าถึงสำหรับผู้ใช้ที่ลงชื่อเข้าใช้

ข้อกำหนดเบื้องต้น

ก่อนที่แอป iOS จะเรียกใช้ Google APIs ได้ ให้ทำตามขั้นตอนการตั้งค่าต่อไปนี้ให้เสร็จสมบูรณ์

1. เปิดใช้ API สำหรับโปรเจ็กต์

แอปพลิเคชันที่เรียกใช้ Google APIs จะต้องเปิดใช้ API เหล่านั้นในคอนโซล Google Cloud

วิธีเปิดใช้ API สำหรับโปรเจ็กต์

  1. เปิดคลัง API ในคอนโซล Google Cloud
  2. หากได้รับข้อความแจ้ง ให้เลือกโปรเจ็กต์หรือสร้างโปรเจ็กต์ใหม่
  3. เลือก API ที่ต้องการเปิดใช้ แล้วคลิกเปิดใช้

2. สร้างข้อมูลเข้าสู่ระบบสำหรับการให้สิทธิ์

แอปพลิเคชัน iOS ที่ใช้ OAuth 2.0 เพื่อเข้าถึง Google APIs ต้องมีข้อมูลเข้าสู่ระบบการให้สิทธิ์ที่ระบุแอปพลิเคชันไปยังเซิร์ฟเวอร์การให้สิทธิ์ของ Google ขั้นตอนต่อไปนี้จะอธิบายวิธีสร้างข้อมูลเข้าสู่ระบบสำหรับโปรเจ็กต์

  1. ไปที่หน้าข้อมูลเข้าสู่ระบบในคอนโซล Google Cloud
  2. คลิกสร้างไคลเอ็นต์ (หรือสร้างข้อมูลเข้าสู่ระบบ > รหัสไคลเอ็นต์ OAuth)
  3. เลือกประเภทแอปพลิเคชัน iOS
  4. ป้อนชื่อไคลเอ็นต์ OAuth ชื่อนี้จะระบุไคลเอ็นต์ในหน้าข้อมูลเข้าสู่ระบบของโปรเจ็กต์
  5. ป้อนรหัสชุดซอฟต์แวร์สำหรับแอปของคุณ รหัสชุดคือค่าของคีย์ CFBundleIdentifier ใน Info.plist ของแอป โดยทั่วไปค่านี้จะแสดงในแผง "ทั่วไป" หรือแผง "การลงชื่อและความสามารถ" ของเครื่องมือแก้ไขโปรเจ็กต์ Xcode หรือในหน้าข้อมูลแอปใน Apple App Store Connect ตรวจสอบว่าคุณใช้รหัสชุดที่ถูกต้อง เนื่องจากจะเปลี่ยนไม่ได้หากใช้ App Check
  6. (ไม่บังคับ) ป้อนรหัส App Store ของแอปหากแอปเผยแพร่ใน Apple App Store รหัส Store คือสตริงตัวเลขที่รวมอยู่ใน URL ของ Apple App Store ทุกรายการ
  7. (ไม่บังคับ) ป้อนรหัสทีม ดูค้นหารหัสทีมในเอกสารประกอบของบัญชีนักพัฒนาแอป Apple ต้องระบุรหัสทีมหากคุณเปิดใช้ App Check สำหรับไคลเอ็นต์
  8. (ไม่บังคับ) เปิดใช้ App Check สำหรับแอป iOS เมื่อเปิดใช้ App Check ระบบจะใช้บริการ App Attest ของ Apple เพื่อยืนยันว่าคำขอ OAuth 2.0 ที่มาจากไคลเอ็นต์ OAuth ของคุณเป็นคำขอจริงและมาจากแอปของคุณ ซึ่งจะช่วยลดความเสี่ยงของการแอบอ้างเป็นแอป
  9. คลิกสร้าง

ระบบจะสร้างรหัสไคลเอ็นต์และแสดงในคอนโซล Google Cloud เพิ่ม URL Scheme ของรหัสไคลเอ็นต์แบบย้อนกลับ (เช่น com.googleusercontent.apps.1234567890-abcdef) ลงใน Info.plist ของแอปเพื่อจัดการการเปลี่ยนเส้นทางการให้สิทธิ์

3. ผสานรวม Google Sign-In พื้นฐานให้เสร็จสมบูรณ์

ทำการผสานรวม Google Sign-In พื้นฐานให้เสร็จสมบูรณ์ก่อนขอขอบเขตเพิ่มเติมและเรียกใช้ Google APIs

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