應用程式翻轉 (iOS 版)

以 OAuth 為基礎的應用程式翻轉連結 (App Flip) 可透過 Google 應用程式開啟您的 iOS 應用程式 ,協助 Google 應用程式使用者更輕鬆地連結帳戶。您必須 只要稍微修改 iOS 應用程式的程式碼,即可導入這項功能。

本文說明如何修改 iOS 應用程式,以支援 App Flip。

試用範例

應用程式快速連結範例應用程式示範 iOS 上與應用程式快速連結相容的帳戶連結整合功能。你可以使用這個應用程式,驗證如何回應傳入的應用程式 Flip 通用支援功能 連結。

範例應用程式已預先設定為整合 App Flip Test Tool iOS、 ,在之前,供您驗證 iOS 應用程式與 App Flip 的整合 設定帳戶與 Google 連結。這個應用程式會模擬 Google 行動應用程式啟用應用程式翻轉功能時,觸發的通用連結。

運作方式

以下是 Google 應用程式和您的應用程式 應用程式切換鈕發生以下情況:

  1. Google 應用程式會嘗試開啟應用程式的通用連結。如果使用者已在裝置上安裝應用程式,且已與通用連結建立關聯,系統就能開啟您的應用程式。詳情請參閱「支援通用連結」。

  2. 應用程式會檢查 client_id 和 redirect_uri 參數是否經過編碼 是否符合預期的 Google 通用連結。

  3. 應用程式向 OAuth2 伺服器要求授權碼。在這個流程結束時,應用程式會將授權碼或錯誤訊息傳回至 Google 應用程式。為此,應用程式會開啟 Google 通用連結,並附加授權碼或錯誤訊息的參數。

  4. Google 應用程式會處理傳入的 Google 通用連結,然後繼續 程序的其餘部分如果提供了授權碼,連結就會 都會立即完成系統是在伺服器對伺服器之間交換權杖 進行管理。如果傳回錯誤代碼,連結流程會繼續使用其他選項。

修改 iOS 應用程式以支援 App Flip

如要支援 App Flip,請對 iOS 應用程式進行以下程式碼變更:

  1. 在應用程式委派函式中處理 NSUserActivityTypeBrowsingWeb。
  2. 從網址擷取 redirect_uri 和 state 參數,以便稍後使用。
  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_id (String):在應用程式下註冊的 Google client_id。
  • scope (List of String):要求以空格分隔的範圍清單。
  • state (String):Google 使用 Nonce 來驗證授權 結果。
  • redirect_uri (String):Google 的通用連結。「翻轉」要開啟的 URI 並透過 Google 應用程式傳遞搜尋結果

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

  • code (String):授權碼的值 (如果有的話)。
  • state (String):從傳入的通用連結接收到的確切值。

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

  • error (String),其值如下:

    • cancelled:可復原的錯誤。Google 應用程式會嘗試使用授權網址連結帳戶。一些例子是 或連線逾時。
    • unrecoverable:無法復原的錯誤。舉例來說,如果使用者嘗試連結已停用的帳戶,Google 應用程式就會中止帳戶連結作業。
    • invalid_request:要求參數無效或遺失。這是可復原的錯誤。Google 應用程式會使用授權網址嘗試連結帳戶。
    • access_denied:使用者拒絕同意聲明要求。這是無法復原的錯誤;Google 應用程式會取消連結。
  • error_description (String,選用):使用者容易理解的錯誤訊息。

針對所有錯誤類型,您都必須將回應資料傳回指定的 REDIRECT_URI,確保觸發適當的備用廣告。

修改授權端點以支援 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 助理應用程式
    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 和 redirect_uri 參數指定的網址,是否與收到要求時的預期值相符。如果用戶端驗證失敗,請將錯誤 invalid_request 傳回至 redirect_uri。