2017-07-26 124 views
0

我正在使用AppAuth實施Google登錄。該應用可以成功驗證。但我需要一個id_token用於我的服務器,以便我可以從我的應用程序與我的服務器通信。爲此我相信我需要包含audience:server:client_id:WEB_CLIENT_ID,如下面的鏈接所示。使用AppAuth和跨客戶端標識的Google登錄

https://developers.google.com/identity/sign-in/android/v1/backend-auth

的更多信息,請訪問: https://developers.google.com/identity/protocols/CrossClientAuth

我如何使用我的Web客戶端ID從應用程序,這樣我可以可靠地使用該令牌我的服務器通信,以獲取一個id_token?

回答

0

範圍audience:server:client_id:WEB_CLIENT_ID特定於Android。對於iOS,我們需要將audience=WEB_CLIENT_ID作爲參數發送給令牌端點。

它在我的情況下使用下面的代碼。

OIDServiceConfiguration *configuration = [[OIDServiceConfiguration alloc] initWithAuthorizationEndpoint:authorizationEndpoint tokenEndpoint:tokenEndpoint]; 

// builds authentication request 
OIDAuthorizationRequest *authorizationRequest = 
[[OIDAuthorizationRequest alloc] initWithConfiguration:configuration 
               clientId:kClientId 
               scopes:@[OIDScopeOpenID, 
                 OIDScopeEmail] 
              redirectURL:[NSURL URLWithString:kRedirectUri] 
              responseType:OIDResponseTypeCode 
            additionalParameters:nil]; 

// performs authentication request 
OIDAuthorizationUICoordinatorIOS *coordinator = [[OIDAuthorizationUICoordinatorIOS alloc] 
               initWithPresentingViewController:self]; 
id<OIDAuthorizationFlowSession> authFlowSession = [OIDAuthorizationService 
                presentAuthorizationRequest:authorizationRequest 
                UICoordinator:coordinator 
                callback:^(OIDAuthorizationResponse *_Nullable authorizationResponse, 
                   NSError *_Nullable authorizationError) { 
                 // inspects response and processes further if needed (e.g. authorization 
                 // code exchange) 
                 if (authorizationResponse) { 
                  if ([authorizationRequest.responseType 
                   isEqualToString:OIDResponseTypeCode]) { 
                   // if the request is for the code flow (NB. not hybrid), assumes the 
                   // code is intended for this client, and performs the authorization 
                   // code exchange 

                   OIDTokenRequest *tokenExchangeRequest = 
                   [[OIDTokenRequest alloc] initWithConfiguration:authorizationRequest.configuration 
                            grantType:OIDGrantTypeAuthorizationCode 
                          authorizationCode:authorizationResponse.authorizationCode 
                            redirectURL:authorizationRequest.redirectURL 
                            clientID:authorizationRequest.clientID 
                           clientSecret:authorizationRequest.clientSecret 

                             scope:authorizationRequest.scope 
                           refreshToken:nil 
                           codeVerifier:authorizationRequest.codeVerifier 
                         additionalParameters:@{@"audience":kWebClientId}]; 
                   //tokenExchangeRequest.scope = kAudienceServerClientId; 

                   [OIDAuthorizationService 
                   performTokenRequest:tokenExchangeRequest 
                   callback:^(OIDTokenResponse *_Nullable tokenResponse, 
                      NSError *_Nullable tokenError) { 
                    OIDAuthState *authState; 
                    if (tokenResponse) { 
                     authState = [[OIDAuthState alloc] 
                        initWithAuthorizationResponse: 
                        authorizationResponse 
                        tokenResponse:tokenResponse]; 
                    } 

                    [self onSignInResponse:authState error:tokenError]; 
                   }]; 
                  } else { 
                   // implicit or hybrid flow (hybrid flow assumes code is not for this 
                   // client) 
                   OIDAuthState *authState = [[OIDAuthState alloc] 
                          initWithAuthorizationResponse:authorizationResponse]; 

                   [self onSignInResponse:authState error:authorizationError]; 
                  } 
                 } else { 
                  [self onSignInResponse:nil error:authorizationError]; 
                 } 
                }]; 

MyAppDelegate *appDelegate = [MyAppDelegate sharedInstance]; 
appDelegate.currentAuthorizationFlow = authFlowSession;