Implementing Callbacks

  1. In your Activity/Fragment where you want to integrate the Truecaller OAuth flow, either make the component implement the interface TcOAuthCallback or create an instance of it which you would require to initialize TcSdkOptions in the next step.

The interface has 2 functions which need to be overridden -

private val tcOAuthCallback: TcOAuthCallback = object : TcOAuthCallback {
    override fun onSuccess(tcOAuthData: TcOAuthData) {
        ..
    }

    override fun onFailure(tcOAuthError: TcOAuthError) {
        ..
    }
}
  • onFailure() method will be called in case of an error. You would get the error details like the error code and error message through tcOAuthError returned with this method.

  • onSuccess() method will be called when the user gives consent to authorize your app by tapping on the primary button on the Truecaller’s consent screen, and subsequently, an authorization code will be successfully generated and received. This method would return tcOAuthData which contains information like :

    • authorizationCode - which you can utilize to fetch the user’s access token

    • scopesGranted - list of scopes granted by the user

    • state - state parameter returned by the authorisation server. If the state set by your application is the same as the state returned by the authorisation server, it’s safe to proceed further. If state parameters are different, someone else has initiated the request and it could be a case of request forgery.

  1. Override the onActivityResult() method of the component used in step 1 and call the onActivityResultObtained() method if the requestCode matches to TcSdk.SHARE_PROFILE_REQUEST_CODE.

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == TcSdk.SHARE_PROFILE_REQUEST_CODE) {
             TcSdk.getInstance().onActivityResultObtained(this, requestCode, resultCode, data)
        }
}

Last updated

Copyright © 2009-2024 True Software Scandinavia AB. All rights reserved. Truecaller™ is a registered trademark.