Receiving Authorization for Android

The SDK uses an Intent to return the authorization result. In order to extract the authorization response, use the following method with the Intent containing the result or the authorization request.

    AuthorizationResponse.fromIntent(intent) 

The result comes back in the data parameter of the requesting Activity/Fragment's onActivityResult(int requestCode, int resultCode, Intent data) method.

public class MyActivity extends AppCompatActivity {

    private static final int REQUEST_CODE_ZENKEY = 1234;

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       if (requestCode == REQUEST_CODE_ZENKEY) {
          if (resultCode == RESULT_OK) {
              handleAuthorizationResponse(data);
          } else {
             //Authorization canceled by user.
          }
       }
    }

    private void handleAuthorizationResponse(Intent data) {
       AuthorizationResponse response = AuthorizationResponse.fromIntent(data);
       if(response.isSuccessful()){
          //Authorization Succeed
          finishZenKeyAuthentication(response);
       } else {
           //Authorization Failed
           handleZenKeyError(response);
       }
    }

    private void finishZenKeyAuthentication(AuthorizationResponse response) {
       String authorizationCode = response.getAuthorizationCode();
       String mcc = response.getMcc();
       String mnc = response.getMnc();
       // Send those values to your back-end to finish the authentication process.
    }

    private void handleZenKeyError(AuthorizationResponse response) {
       AuthorizationError error = response.getError();
       switch (error){
          case AuthorizationError.INVALID_CONFIGURATION:
             break;
          case AuthorizationError.INVALID_REQUEST:
             break;
          case AuthorizationError.REQUEST_DENIED:
             break;
          case AuthorizationError.REQUEST_TIMEOUT:
             break;
          case AuthorizationError.SERVER_ERROR:
             break;
          case AuthorizationError.NETWORK_FAILURE:
             break;
          case AuthorizationError.DISCOVERY_STATE:
             break;
          case AuthorizationError.UNKNOWN:
             break;
       }
    }
}
```java public class MyActivity extends AppCompatActivity {
private static final int REQUEST_CODE_ZENKEY = 1234;

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (requestCode == REQUEST_CODE_ZENKEY) {
      if (resultCode == RESULT_OK) {
          handleAuthorizationResponse(data);
      } else {
         //Authorization canceled by user.
      }
   }
}

private void handleAuthorizationResponse(Intent data) {
   AuthorizationResponse response = AuthorizationResponse.fromIntent(data);
   if(response.isSuccessful()){
      //Authorization Succeed
      finishZenKeyAuthentication(response);
   } else {
       //Authorization Failed
       handleZenKeyError(response);
   }
}

private void finishZenKeyAuthentication(AuthorizationResponse response) {
   String authorizationCode = response.getAuthorizationCode();
   String mcc = response.getMcc();
   String mnc = response.getMnc();
   String codeVerifier = response.getCodeVerifier();
   // Send those four values to your back-end to finish the authentication process.
}

private void handleZenKeyError(AuthorizationResponse response) {
   AuthorizationError error = response.getError();
   switch (error){
      case AuthorizationError.INVALID_CONFIGURATION:
         break;
      case AuthorizationError.INVALID_REQUEST:
         break;
      case AuthorizationError.REQUEST_DENIED:
         break;
      case AuthorizationError.REQUEST_TIMEOUT:
         break;
      case AuthorizationError.SERVER_ERROR:
         break;
      case AuthorizationError.NETWORK_FAILURE:
         break;
      case AuthorizationError.DISCOVERY_STATE:
         break;
      case AuthorizationError.UNKNOWN:
         break;
   }
}

}

</div>

 

Did this page help you?