Now that the Android application has an authorization code, it can exchange it for an access_token required to make a request to a vehicle.


1. Obtain an access token

After receiving the authorization code, the Android application must exchange it for an access_token. To do so, we can send the code to the back-end service which will implement in a bit. Until then, let’s assume our back-end service contains an endpoint /exchange that receives an authorization code as a query parameter and exchanges it for an access_token.

We can add this logic in our SmartcarCallback object -

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
// /app/src/main/java/smartcar/com/getting_started_android_sdk/MainActivity.javafinal OkHttpClient client = new OkHttpClient();

public void handleResponse(final SmartcarResponse smartcarResponse) {
    Log.i("MainActivity", smartcarResponse.getCode());

    final OkHttpClient client = new OkHttpClient();

    // TODO: Request Step 1: Obtain and access token

    // Request can not run on the Main Thread
    // Main Thread is used for UI and therefore can not be blocked
    new Thread(new Runnable() {
        @Override
        public void run() {
    
            // send request to exchange the auth code for the access token
            Request exchangeRequest = new Request.Builder()
                .url(getString(R.string.app_server) + "/exchange?code=" + smartcarResponse.getCode())
                .build();
        
            try {
                client.newCall(exchangeRequest).execute();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
    
    // TODO: Request Step 2: Get vehicle information
}

Notice that our back-end service does not return the access_token. This is by design. For security, our front-end should never have access to the access_token and should always be stored in the back-end.

2. Get vehicle attributes

Once the backend has the access_token, it can send requests to a vehicle using the Smartcar API. The Android app will have to send a request to the back-end service which in turn sends a request to Smartcar. We have to do this because our front-end does not have the access_token.

Assuming our back-end has a /vehicle endpoint that returns the information of a user’s vehicle, we can make this query in our completion callback and start another activity the returned vehicle attributes -

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
// /app/src/main/java/smartcar/com/getting_started_android_sdk/MainActivity.javafinal

public void handleResponse(final SmartcarResponse smartcarResponse) {
    ...
    
    // TODO: Request Step 2: Get vehicle information

    // send request to retrieve the vehicle info
    Request infoRequest = new Request.Builder()
        .url(getString(R.string.app_server) + "/vehicle")
        .build();
    
    try {
        Response response = client.newCall(infoRequest).execute();
    
        String jsonBody = response.body().string();
        JSONObject JObject = new JSONObject(jsonBody);
    
        String make = JObject.getString("make");
        String model = JObject.getString("model");
        String year = JObject.getString("year");
    
        Intent intent = new Intent(appContext, DisplayInfoActivity.class);
        intent.putExtra("INFO", make + " " + model + " " + year);
        startActivity(intent);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

3. Back-end service

Now that our front-end is complete, we will need to create a back-end service that contains the logic for the /exchange and /vehicle endpoints.

You can use any of our Smartcar API SDKs to set up the service. For set up instructions, you can read the Setup section from our Getting Started guide. Once you are set up, you can follow along with the Making a request section.

Note: you can ignore the Try it out section of the Getting Started guide, and instead follow along with the next section of this guide.

Try it out

Make sure your back-end server is up and running. In your back-end directory -

  • $
  • $
export SMARTCAR_REDIRECT_URI=sc + "clientId" + ://exchange
node index.js

Now run the Android application in Android Studio!

Next steps