iOS용 앱 플립

OAuth 기반 App Flip 연결(App Flip)은 Google 앱에서 iOS 앱을 열어 Google 앱 사용자가 계정을 보다 쉽게 ​​연결할 수 있도록 도와줍니다. 이 기능을 구현하려면 iOS 앱에 약간의 코드를 변경해야 합니다.

이 문서에서는 App Flip을 지원하도록 iOS 앱을 수정하는 방법을 배웁니다.

샘플 사용해보기

앱 플립 샘플 응용 프로그램은 응용 프로그램의 플립 - 호환 iOS의 통합을 연결하는 계정을 보여줍니다. 이 앱을 사용하여 Google 모바일 앱에서 수신되는 App Flip 범용 링크에 응답하는 방법을 확인할 수 있습니다.

샘플 응용 프로그램은과 통합 사전 구성되어 iOS 용 앱 플립 테스트 툴 당신이 구성 구글 계정 연결하기 전에 앱 플립와 iOS 앱의 통합을 확인하는 데 사용할 수 있습니다. 이 앱은 App Flip이 활성화된 경우 Google 모바일 앱에 의해 트리거되는 범용 링크를 시뮬레이션합니다.

작동 원리

다음은 App Flip이 발생할 때 Google 앱과 앱이 수행하는 흐름 단계입니다.

  1. Google 앱이 앱의 범용 링크를 열려고 합니다. 앱이 사용자의 기기에 설치되어 있고 범용 링크와 연결된 경우 앱을 열 수 있습니다. 참조 범용 링크를 지원 자세한 내용은.

  2. 것을 앱 검사 client_idredirect_uri 들어오는 URL로 인코딩 된 매개 변수가 예상 구글 보편적 링크를 일치합니다.

  3. 앱이 OAuth2 서버에서 인증 코드를 요청합니다. 이 흐름이 끝나면 앱에서 승인 코드나 오류를 Google 앱에 반환합니다. 이를 위해 인증 코드 또는 오류에 대한 매개변수가 추가된 Google의 범용 링크를 엽니다.

  4. Google 앱은 들어오는 Google 범용 링크를 처리하고 나머지 흐름을 계속합니다. 인증코드를 입력하면 바로 연결이 완료됩니다. 토큰 교환은 브라우저 기반 OAuth 연결 흐름에서와 동일한 방식으로 서버 간 발생합니다. 오류 코드가 반환되면 연결 흐름이 대체 옵션으로 계속됩니다.

App Flip을 지원하도록 iOS 앱 수정

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_id ( String ) : 구글은 client_id 귀하의 응용 프로그램에 등록이의를.
  • scope ( List of String ) : 요청 공백으로 구분 된 범위의 목록.
  • state ( String ) : 구글에 의해 사용되는 넌스 권한 부여 결과는 구글의 나가는 요청에 대한 응답에 있는지 확인합니다.
  • redirect_uri ( String ) : 구글의 보편적 인 링크. Google 앱을 열고 결과를 전달하는 "뒤집기" URI입니다.

인증 결과가 성공적으로 반환될 때 사용되는 매개변수:

  • code ( String ) : 인증 코드의 값 가능한 경우.
  • state ( String ) : 들어오는 보편적 링크에서받은 정확한 값.

인증 결과가 성공적으로 반환되지 않을 때 사용되는 매개변수:

  • error ( String 다음 값) :

    • cancelled : 복구 가능한 오류입니다. Google 앱은 인증 URL을 사용하여 계정 연결을 시도합니다. 몇 가지 예는 사용자가 로그인하지 못하거나 장치가 오프라인 상태이거나 연결 시간이 초과된 경우입니다.
    • unrecoverable : 복구 할 수없는 오류가 발생했습니다. 예를 들어 사용자가 비활성화된 계정과 연결을 시도합니다. Google 앱이 계정 연결을 중단합니다.
    • invalid_request : 요청 매개 변수가 유효하지 않거나 누락되었습니다. 복구 가능한 오류입니다. Google 앱은 인증 URL을 사용하여 계정 연결을 시도합니다.
    • access_denied : 사용자가 동의 요청을 거부합니다. 이것은 복구할 수 없는 오류입니다. Google 앱이 연결을 중단합니다.
  • error_description ( String , 선택 사항) : 사용자 친화적 오류 메시지.

모든 오류 유형의 경우, 지정된에 대한 응답 데이터를 반환해야합니다 REDIRECT_URI 적절한 대체가 trigerred되어 있는지 확인 할 수 있습니다.

App Flip을 지원하도록 권한 부여 엔드포인트 수정

Google의 App Flip 리디렉션 URL을 사용하여 요청을 수락하도록 플랫폼을 구성합니다.

  • Google 홈 응용 프로그램
    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_request 받는 redirect_uri .