App Flip (適用於 iOS)

基於 OAuth 的 App Flip 鏈接 (App Flip) 從 Google 應用打開您的 iOS 應用,以幫助 Google 應用用戶更輕鬆地鏈接他們的帳戶。您需要對 iOS 應用程序進行少量代碼更改才能實現此功能。

在本文檔中,您將了解如何修改您的 iOS 應用以支持 App Flip。

試試樣品

該應用程序翻轉示例應用程序演示連接iOS上的整合是公司的App翻轉兼容的帳戶。您可以使用此應用來驗證如何響應來自 Google 移動應用的傳入 App Flip 通用鏈接。

示例應用程序預配置與整合應用翻轉測試工具為iOS ,你可以用它來驗證您的iOS應用的使用App翻轉整合之前配置帳戶與谷歌聯繫起來。此應用模擬啟用 App Flip 時由 Google 移動應用觸發的通用鏈接。

怎麼運行的

以下是發生 App Flip 時 Google 應用和您的應用所採取的流程步驟:

  1. Google 應用會嘗試打開您應用的通用鏈接。如果您的應用程序安裝在用戶的設備上並與通用鏈接相關聯,則它可以打開您的應用程序。見支持通用鏈接了解詳情。

  2. 您的應用程序檢查該client_idredirect_uri在輸入URL編碼參數的預期谷歌的通用鏈接匹配。

  3. 您的應用程序向您的 OAuth2 服務器請求授權碼。在此流程結束時,您的應用會向 Google 應用返回授權代碼或錯誤。為此,它會打開 Google 的通用鏈接,其中包含授權代碼或錯誤的附加參數。

  4. Google 應用程序處理傳入的 Google 通用鏈接並繼續執行流程的其餘部分。如果提供授權碼,則立即完成鏈接。令牌交換發生在服務器到服務器之間,與基於瀏覽器的 OAuth 鏈接流程中的方式相同。如果返回錯誤代碼,鏈接流將繼續使用替代選項。

修改您的 iOS 應用以支持 App Flip

要支持 App Flip,請對您的 iOS 應用程序進行以下代碼更改:

  1. 處理NSUserActivityTypeBrowsingWeb在應用程序委託。
  2. 捕捉redirect_uristate從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. 驗證客戶端 ID 是否與預期值匹配。使用以下代碼示例:

    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_idString ):谷歌client_id您的應用程序下註冊的公司。
  • scopeList of String ):請用空格隔開的範圍列表。
  • stateString ):由谷歌中使用的隨機數,以驗證該授權結果是響應於谷歌的呼出請求。
  • redirect_uriString ):谷歌的通用連接。用於打開 Google 應用並傳遞結果的“翻轉”URI。

授權結果返回成功時使用的參數:

  • codeString ):授權碼的所述的值(如果可用)。
  • stateString ):從輸入的通用鏈路接收的精確值。

授權結果返回失敗時使用的參數:

  • errorString ),具有以下值:

    • cancelled :可恢復的錯誤。 Google 應用將嘗試使用授權 URL 關聯帳戶。一些示例是用戶無法登錄、設備離線或連接超時。
    • unrecoverable :不可恢復的錯誤。例如,用戶嘗試與禁用的帳戶關聯。Google 應用程序將中止帳戶關聯。
    • invalid_request :請求參數無效或丟失。這是一個可恢復的錯誤。 Google 應用將嘗試使用授權 URL 關聯帳戶。
    • access_denied :用戶拒絕同意請求。這是一個不可恢復的錯誤; Google 應用中止鏈接。
  • error_descriptionString ,可選):一個用戶友好的錯誤消息。

對於所有的錯誤類型,則必須返回響應數據到指定REDIRECT_URI ,以確保適當的後退時被trigerred。

修改您的授權端點以支持 App Flip

將您的平台配置為使用 Google 的 App Flip 重定向 URL 接受請求:

  • 谷歌主頁的應用程序
    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
    
  • 谷歌應用程序助理
    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_requestredirect_uri