2017-07-26 22 views
0

私はAppAuthを使用してGoogleログインを実装しています。アプリは正常に認証されました。しかし、自分のサーバーにid_tokenが必要なので、私のアプリケーションから自分のサーバーと通信することができます。そのためには、次のリンクに示すようにaudience:server:client_id:WEB_CLIENT_IDを含める必要があると思います。AppAuthとクロスクライアントIDを使用したGoogleのログイン

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

詳しい情報はこちら: https://developers.google.com/identity/protocols/CrossClientAuth

どのように私は確実にそのトークンを使用して、私のサーバーと通信できるようにid_tokenを得るために、アプリから自分のWebクライアントIDを使用できますか?

答えて

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; 
関連する問題