Android™ Requires Chilkat v11.0.0+
Android™
PayPal: Get a sandbox access token
See more PayPal Examples
Exchange a sandbox REST app client ID and secret for a bearer token on a merchant-controlled backoffice workstation. PayPal supplies its lifetime in seconds. Store the token in a protected credential store; the companion expiry example reads only timing metadata.
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;
// See Unlock Chilkat before making requests.
CkHttp http = new CkHttp();
// Load these values from the credential store on the merchant backoffice workstation.
// Never embed the merchant secret in an app distributed to customers.
http.put_Login("SANDBOX_CLIENT_ID");
http.put_Password("SANDBOX_CLIENT_SECRET");
http.put_BasicAuth(true);
CkDateTime issued = new CkDateTime();
issued.SetFromCurrentSystemTime();
CkHttpResponse resp = new CkHttpResponse();
success = http.HttpStr("POST","https://api-m.sandbox.paypal.com/v1/oauth2/token","grant_type=client_credentials","utf-8","application/x-www-form-urlencoded",resp);
if (success != true) {
Log.i(TAG, "No HTTP response: " + http.lastErrorText());
return;
}
if ((resp.get_StatusCode() < 200) or (resp.get_StatusCode() >= 300)) {
CkJsonObject errorInfo = new CkJsonObject();
errorInfo.Load(resp.bodyStr());
Log.i(TAG, "HTTP " + String.valueOf(resp.get_StatusCode()) + " " + errorInfo.stringOf("error") + " " + errorInfo.stringOf("error_description"));
Log.i(TAG, "PayPal debug ID: " + errorInfo.stringOf("debug_id"));
return;
}
CkJsonObject result = new CkJsonObject();
boolean parsed = result.Load(resp.bodyStr());
if (parsed != true) {
Log.i(TAG, "Expected a JSON response; HTTP " + String.valueOf(resp.get_StatusCode()));
return;
}
String accessToken = result.stringOf("access_token");
int lifetime = result.IntOf("expires_in");
if ((result.HasMember("access_token") != true) or (lifetime <= 0)) {
Log.i(TAG, "Missing token or lifetime.");
return;
}
// Supply accessToken to Http.AuthToken. Never log the bearer token or client secret.
// Save only timing metadata here, relative to the start of the token request.
CkJsonObject timing = new CkJsonObject();
timing.UpdateString("issued_at",issued.getAsTimestamp(false));
timing.UpdateInt("expires_in",lifetime);
boolean savedTiming = timing.WriteFile("paypal-token-timing.json");
if (savedTiming != true) {
Log.i(TAG, "Could not save token timing.");
return;
}
Log.i(TAG, "Token received; lifetime in seconds: " + String.valueOf(lifetime));
}
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."
}
}