App Flip สำหรับ iOS

การลิงก์ App Flip ที่ใช้ OAuth (App Flip) จะเปิดแอป iOS จากแอป Google เพื่อช่วยให้ผู้ใช้แอป Google ลิงก์บัญชีของตนได้ง่ายขึ้น คุณต้องทำ การเปลี่ยนแปลงโค้ดเล็กน้อยในแอป iOS เพื่อใช้ฟีเจอร์นี้

ในเอกสารนี้ คุณจะได้เรียนรู้วิธีแก้ไขแอป iOS ให้รองรับ App Flip

ดูตัวอย่าง

แอปตัวอย่างของ App Flip สาธิตการผสานรวมการลิงก์บัญชีใน iOS ที่เข้ากันได้กับ App Flip คุณสามารถใช้แอปนี้เพื่อยืนยันวิธีตอบสนองต่อ Universal App Link ของ App Flip ที่เข้ามาจากแอปบนอุปกรณ์เคลื่อนที่ของ Google

แอปตัวอย่างได้รับการกำหนดค่าไว้ล่วงหน้าเพื่อผสานรวมกับเครื่องมือทดสอบการพลิกแอปสำหรับ iOS ซึ่งคุณสามารถใช้ยืนยันการผสานรวมแอป iOS กับ App Flip ก่อน ให้คุณกำหนดค่าการลิงก์บัญชีกับ Google แอปนี้จะจำลอง Universal Link ที่เรียกให้แสดงโดยแอปบนอุปกรณ์เคลื่อนที่ของ Google เมื่อเปิดใช้ App Flip

วิธีการทำงาน

ต่อไปนี้คือขั้นตอนของขั้นตอนที่แอป Google และแอปของคุณใช้เมื่อ การพลิกแอปเกิดขึ้น

  1. แอป Google จะพยายามเปิด Universal Link ของแอปคุณ สามารถ เปิดแอปของคุณหากได้ติดตั้งไว้ในอุปกรณ์ของผู้ใช้ และเชื่อมโยงกับ Universal Link ดูรายละเอียดได้ที่การรองรับ Universal Link

  2. แอปของคุณตรวจสอบว่าพารามิเตอร์ client_id และ redirect_uri เข้ารหัสแล้ว ใน URL ขาเข้าตรงกับ Universal Link ของ Google ที่คาดไว้

  3. แอปของคุณขอรหัสการให้สิทธิ์จากเซิร์ฟเวอร์ OAuth2 ในช่วงท้าย ของขั้นตอนนี้ แอปของคุณจะแสดงรหัสการให้สิทธิ์หรือข้อผิดพลาดไปยัง แอป Google ซึ่งทำได้โดยการเปิดลิงก์สากลของ Google พร้อมข้อความต่อท้าย สำหรับรหัสการให้สิทธิ์หรือข้อผิดพลาด

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

แก้ไขแอป iOS ให้รองรับ App Flip

หากต้องการรองรับ App Flip ให้ทำการเปลี่ยนแปลงโค้ดต่อไปนี้ในแอป iOS ของคุณ:

  1. จัดการ NSUserActivityTypeBrowsingWeb ในการมอบสิทธิ์แอป
  2. บันทึกพารามิเตอร์ redirect_uri และ state จาก URL เพื่อใช้ในภายหลัง
  3. ตรวจสอบว่า redirect_uri ตรงกับรูปแบบนี้
    https://oauth-redirect.googleusercontent.com/a/GOOGLE_APP_BUNDLE_ID
    https://oauth-redirect-sandbox.googleusercontent.com/a/GOOGLE_APP_BUNDLE_ID
  4. ตรวจสอบว่ารหัสไคลเอ็นต์ตรงกับค่าที่คาดไว้ ใช้รายการต่อไปนี้ ตัวอย่างโค้ด:

    func application(_ application: UIApplication,
                     continue userActivity: NSUserActivity,
                     restorationHandler: @escaping ([Any]?) -> Void) -> Bool
    {
        guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
            let incomingURL = userActivity.webpageURL,
            let components = URLComponents(url: incomingURL, resolvingAgainstBaseURL: false),
            let params = components.queryItems else {
                return false
        }
    
        if let clientId = params.filter({$0.name == "client_id"}).first?.value,
            let state = params.filter({$0.name == "state"}).first?.value,
            let redirectUri = params.filter({$0.name == "redirect_uri"}).first?.value {
    
            // Save the redirect_uri and state for later...
    
            // Verify the client id
            return (clientId == GOOGLE_CLIENT_ID)
        } else {
            // Missing required parameters
            return false
        }
    }
    
  5. เมื่อให้สิทธิ์สําเร็จแล้ว ให้เรียกใช้ URI การเปลี่ยนเส้นทางด้วยรหัสการให้สิทธิ์ ใช้ตัวอย่างโค้ดต่อไปนี้

    func returnAuthCode(code: String, state: String, redirectUri: String) {
        var redirectURL = URL(string: redirectUri)
        var components = URLComponents(url: redirectURL, resolvingAgainstBaseURL: false)
    
        // Return the authorization code and original state
        let paramAuthCode = URLQueryItem(name: "code", value: code)
        let paramState = URLQueryItem(name: "state", value: state)
        components?.queryItems = [paramAuthCode, paramState]
        if let resultURL = components?.url {
            UIApplication.shared.open(
                resultURL,
                options: [UIApplicationOpenURLOptionUniversalLinksOnly : true],
                completionHandler: nil)
        }
    }
    
  6. หากเกิดข้อผิดพลาด ให้แนบผลลัพธ์ที่มีข้อผิดพลาดไปยัง URI การเปลี่ยนเส้นทางแทน ใช้ตัวอย่างโค้ดต่อไปนี้

    func returnError(redirectUri: String) {
        var redirectURL = URL(string: redirectUri)
        var components = URLComponents(url: redirectURL, resolvingAgainstBaseURL: false)
    
        // Return the authorization code and original state
        let paramError = URLQueryItem(name: "error", value: "invalid_request")
        let paramDescription = URLQueryItem(name: "error_description", value: "Invalid Request")
        components?.queryItems = [paramError, paramDescription]
        if let resultURL = components?.url {
            UIApplication.shared.open(
                resultURL,
                options: [UIApplicationOpenURLOptionUniversalLinksOnly : true],
                completionHandler: nil)
        }
    }
    

เมื่อเปิดโดยแอป Google ลิงก์อเนกประสงค์ของแอปจะมีพารามิเตอร์การค้นหาต่อไปนี้

  • client_id (String): Google client_id ที่ลงทะเบียนภายใต้แอปของคุณ
  • scope (List of String): ขอรายการขอบเขตที่คั่นด้วยพื้นที่ทำงานแล้ว
  • state (String): Nonce ที่ Google ใช้เพื่อยืนยันว่าการให้สิทธิ์ เป็นการตอบกลับคำขอขาออกของ Google
  • redirect_uri (String): Universal Link ของ Google URI "Flip" เพื่อเปิดแอป Google และส่งผลลัพธ์

พารามิเตอร์ที่ใช้เมื่อระบบแสดงผลลัพธ์การให้สิทธิ์สําเร็จ

  • code (String): ค่าของรหัสการให้สิทธิ์ (หากมี)
  • state (String): ค่าที่แน่นอนที่ได้รับจาก Universal Link ที่เข้ามาใหม่

พารามิเตอร์ที่ใช้เมื่อแสดงผลลัพธ์การให้สิทธิ์ไม่สำเร็จ ได้แก่

  • error (String) ที่มีค่าต่อไปนี้

    • cancelled: ข้อผิดพลาดที่กู้คืนได้ แอป Google จะพยายามเข้าถึงบัญชี โดยใช้ URL การให้สิทธิ์ เช่น ผู้ใช้ดำเนินการไม่สำเร็จ อุปกรณ์ออฟไลน์ หรือการเชื่อมต่อหมดเวลา
    • unrecoverable: ข้อผิดพลาดที่กู้คืนไม่ได้ เช่น ผู้ใช้พยายามลิงก์กับบัญชีที่ถูกปิดใช้ แอป Google จะล้มเลิกการลิงก์บัญชี
    • invalid_request: พารามิเตอร์คำขอไม่ถูกต้องหรือไม่มี ซึ่งเป็นข้อผิดพลาดที่กู้คืนได้ แอป Google จะพยายามลิงก์บัญชีโดยใช้ URL การให้สิทธิ์
    • access_denied: ผู้ใช้ปฏิเสธคำขอความยินยอม นี่เป็นข้อผิดพลาดที่กู้คืนไม่ได้ แอป Google ล้มเลิกการลิงก์
  • error_description (String ไม่บังคับ): ข้อความแสดงข้อผิดพลาดที่ใช้ง่าย

สำหรับข้อผิดพลาดทุกประเภท คุณต้องส่งคืนข้อมูลการตอบสนองไปยัง REDIRECT_URI เพื่อให้มีการเรียกใช้วิดีโอสำรองที่เหมาะสม

แก้ไขปลายทางการให้สิทธิ์ให้รองรับ App Flip

กำหนดค่าแพลตฟอร์มให้ยอมรับคำขอโดยใช้ URL การเปลี่ยนเส้นทาง App Flip ของ Google

  • แอป Google Home
    https://oauth-redirect.googleusercontent.com/a/com.google.Chromecast.dev
    https://oauth-redirect.googleusercontent.com/a/com.google.Chromecast.enterprise
    https://oauth-redirect.googleusercontent.com/a/com.google.Chromecast.rc
    https://oauth-redirect.googleusercontent.com/a/com.google.Chromecast
    https://oauth-redirect-sandbox.googleusercontent.com/a/com.google.Chromecast.dev
    https://oauth-redirect-sandbox.googleusercontent.com/a/com.google.Chromecast.enterprise
    https://oauth-redirect-sandbox.googleusercontent.com/a/com.google.Chromecast.rc
    https://oauth-redirect-sandbox.googleusercontent.com/a/com.google.Chromecast
    
  • แอป Google Assistant
    https://oauth-redirect.googleusercontent.com/a/com.google.OPA.dev
    https://oauth-redirect.googleusercontent.com/a/com.google.OPA.enterprise
    https://oauth-redirect.googleusercontent.com/a/com.google.OPA.rc
    https://oauth-redirect.googleusercontent.com/a/com.google.OPA
    https://oauth-redirect-sandbox.googleusercontent.com/a/com.google.OPA.dev
    https://oauth-redirect-sandbox.googleusercontent.com/a/com.google.OPA.enterprise
    https://oauth-redirect-sandbox.googleusercontent.com/a/com.google.OPA.rc
    https://oauth-redirect-sandbox.googleusercontent.com/a/com.google.OPA
    

ตรวจสอบว่า client_id และ URL ที่ระบุโดยพารามิเตอร์ redirect_uri ตรงกับค่าที่คาดไว้เมื่อได้รับคําขอ หากการยืนยันไคลเอ็นต์ไม่สําเร็จ ให้แสดงข้อผิดพลาด invalid_request ไปยัง redirect_uri