2017-07-26 5 views
0

Ich verwende AppAuth, um Google-Anmeldung zu implementieren. Die App konnte erfolgreich authentifiziert werden. Aber ich brauche einen id_token für meinen Server, damit ich mit meinem Server von meiner App aus kommunizieren kann. Dafür glaube ich, dass ich audience:server:client_id:WEB_CLIENT_ID einschließen muss, wie in dem folgenden Link gezeigt.Google-Anmeldung mit AppAuth und clientübergreifender Identität

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

Weitere Informationen finden Sie hier: https://developers.google.com/identity/protocols/CrossClientAuth

Wie kann ich meine Web-Client-ID aus der App verwenden, um eine id_token zu bekommen, so dass ich sicher mit meinem Server mit, dass Token kommunizieren kann?

Antwort

0

Der Bereich audience:server:client_id:WEB_CLIENT_ID ist spezifisch für Android. Für iOS müssen wir audience=WEB_CLIENT_ID als Parameter an den Token-Endpunkt senden.

Es funktioniert in meinem Fall mit dem folgenden Code.

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; 
Verwandte Themen