App Flip สําหรับ iOS

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

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

ดูตัวอย่าง

App Flip ตัวอย่างแอป จะสาธิตการผสานรวมการลิงก์บัญชีบน iOS ซึ่งใช้ร่วมกับ App Flip ได้ คุณใช้แอปนี้เพื่อยืนยันวิธีตอบสนองต่อ 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 เปิดไว้ Universal Link ของแอปจะรวมสิ่งต่อไปนี้ พารามิเตอร์การค้นหา:

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

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