Android™
Android™
Configure Google Service Account Authentication for REST
See more REST Examples
Demonstrates Rest.SetAuthGoogle, which associates an AuthGoogle provider so Chilkat supplies a Google OAuth 2.0 bearer token on each request. The provider is configured with a service account JSON key and the required OAuth scope.
The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.
Background. The AuthGoogle provider performs a service-account (server-to-server) OAuth flow. For interactive, browser-based user consent, the OAuth2 provider is used instead.
Chilkat Android™ Downloads
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
private static final String TAG = "Chilkat";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean success = false;
CkRest rest = new CkRest();
boolean bTls = true;
boolean bAutoReconnect = true;
success = rest.Connect("example.com",443,bTls,bAutoReconnect);
if (success == false) {
Log.i(TAG, rest.lastErrorText());
return;
}
// Configure Google service-account authentication. Load the service account JSON key and set the
// required OAuth scope(s).
CkAuthGoogle gAuth = new CkAuthGoogle();
CkStringBuilder sbJsonKey = new CkStringBuilder();
boolean bLoaded = sbJsonKey.LoadFile("qa_data/service_account.json");
if (bLoaded != true) {
Log.i(TAG, "Failed to load the service account JSON key.");
return;
}
String jsonKey = sbJsonKey.getAsString();
if (sbJsonKey.get_LastMethodSuccess() == false) {
Log.i(TAG, sbJsonKey.lastErrorText());
return;
}
gAuth.put_JsonKey(jsonKey);
// A space-delimited list of the requested permissions.
gAuth.put_Scope("https://www.googleapis.com/auth/cloud-platform");
// Associate the provider so Chilkat supplies a Google bearer token on each request.
success = rest.SetAuthGoogle(gAuth);
if (success == false) {
Log.i(TAG, rest.lastErrorText());
return;
}
String responseText = rest.fullRequestNoBody("GET","/storage/v1/b?project=my-project");
if (rest.get_LastMethodSuccess() == false) {
Log.i(TAG, rest.lastErrorText());
return;
}
Log.i(TAG, responseText);
}
static {
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}