Android

Authorization

To make requests to a vehicle, your end user must connect their vehicle using Smartcar Connect. Connect uses the OAuth 2.0 protocol to provide a secure and elegant authorization flow for your users.

1. Initialize the Smartcar object

Instantiate a Smartcar object in the onCreate function of the MainActivity.

  • 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
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
// /app/src/main/java/smartcar/com/getting_started_android_sdk/MainActivity.java

// TODO: Authorization Step 1a: Initialize the Smartcar object
private static String CLIENT_ID;
private static String REDIRECT_URI;
private static String[] SCOPE;
private SmartcarAuth smartcarAuth;

protected void onCreate(Bundle savedInstanceState) {
    ...

    // TODO: Authorization Step 1b: Initialize the Smartcar object
    CLIENT_ID = getString(R.string.client_id);
    REDIRECT_URI = "sc" + getString(R.string.client_id) + "://exchange";
    SCOPE = new String[]{"read_vehicle_info"};
    
    smartcarAuth = new SmartcarAuth(
        CLIENT_ID,
        REDIRECT_URI,
        SCOPE,
        true,
        new SmartcarCallback() {
            // TODO: Authorization Step 3b: Receive an authorization code
        }
    );
}

2. Launch Connect

The Android application will launch a WebView with Connect to request access to a user's vehicle. On Connect, the user logs in with their vehicle credentials and grants the application access to their vehicle.

To launch Connect, we can use the addClickHandler function that our Smartcar object has access to.

  • 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
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
// /app/src/main/java/smartcar/com/getting_started_android_sdk/MainActivity.java

// TODO: Authorization Step 2: Launch Connect
smartcarAuth.addClickHandler(appContext, connectButton);

3. Receive an authorization code

Once a user has authorized the application to access their vehicle, the user is redirected to the redirect_uri with an authorization code as a query parameter.

Android applications use custom URI schemes to intercept calls and launch the relevant application. This is defined within the AndroidManifest.

  • 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
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
// /app/src/main/AndroidManifest.xml

<!-- TODO: Authorization Step 3a: Receive an authorization code -->
<activity android:name="com.smartcar.sdk.SmartcarCodeReceiver">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data
            android:host="@string/smartcar_auth_host"
            android:scheme="@string/smartcar_auth_scheme" />
    </intent-filter>
</activity>

Using the Android SDK, the application can receive the code in the SmartcarCallback object passed into the Smartcar 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
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
// /app/src/main/java/smartcar/com/getting_started_android_sdk/MainActivity.java

smartcarAuth = new SmartcarAuth(
    CLIENT_ID,
    REDIRECT_URI,
    SCOPE,
    true,
    new SmartcarCallback() {
        // TODO: Authorization Step 3b: Receive an authorization code
        @Override
        public void handleResponse(final SmartcarResponse smartcarResponse) {
            Log.i("MainActivity", smartcarResponse.getCode());

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

            // TODO: Request Step 2: Get vehicle information
        }
    }
)

Try it out

Let's try authenticating a vehicle in test mode.

In test mode, any login credentials for all brands are valid!

Build your application in Android Studio and click on the “Connect your vehicle" button. Notice once you log in, Smartcar showcases all the permissions your application is asking for, in this case, read_vehicle_info. A user has to consent to all the permissions.

Once you have logged in and accepted the permissions, you should see your authorization code printed to the Logcat console in Android Studio.


In the next section, we will cover how to exchange the authorization code for an access_token and make your first request to Smartcar API with it!