Android
Authorization
1. Initialize the Smartcar object
Instantiate a Smartcar
object in the onCreate
function of the MainActivity
.
// /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 = getString(R.string.smartcar_auth_scheme) + "://" + getString(R.string.smartcar_auth_host);
SCOPE = new String[]{"required: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 Chrome Tab with Smartcar Connect to request access to a user’s vehicle. On Connect, the user logs in with the username and password for their vehicle’s connected services account and grants the application access to their vehicle.
To launch Connect, we can use the addClickHandler
function that our Smartcar
object has access to.
// /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
.
// /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.
// /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.
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!