Sample code for 30+ languages & platforms
Java Requires Chilkat v11.0.0+

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.

PayPal documentation

Chilkat Java Downloads

Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.DateTime;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;

public class ChilkatExample {

    public static void main(String[] args) {
        // See Unlock Chilkat before making requests.
        Http http = new Http();
        // Load these values from the credential store on the merchant backoffice workstation.
        // Never embed the merchant secret in an app distributed to customers.
        http.setLogin("SANDBOX_CLIENT_ID");
        http.setPassword("SANDBOX_CLIENT_SECRET");
        http.setBasicAuth(true);
        DateTime issued = new DateTime();
        issued.setFromCurrentSystemTime();
        HttpResponse resp = new HttpResponse();
        try {
            http.httpStr("POST", "https://api-m.sandbox.paypal.com/v1/oauth2/token", "grant_type=client_credentials", "utf-8", "application/x-www-form-urlencoded", resp);
        } catch (ChilkatException e) {
            System.out.println("No HTTP response: " + e.getLastErrorText());
            return;
        }

        if ((resp.getStatusCode() < 200) || (resp.getStatusCode() >= 300)) {
            JsonObject errorInfo = new JsonObject();
            errorInfo.load(resp.getBodyStr());
            System.out.println("HTTP " + resp.getStatusCode() + " " + errorInfo.stringOf("error") + " " + errorInfo.stringOf("error_description"));
            System.out.println("PayPal debug ID: " + errorInfo.stringOf("debug_id"));
            return;
        }

        JsonObject result = new JsonObject();
        try {
            result.load(resp.getBodyStr());
        } catch (ChilkatException e) {
            System.out.println("Expected a JSON response; HTTP " + resp.getStatusCode());
            return;
        }

        String accessToken = result.stringOf("access_token");
        int lifetime = result.intOf("expires_in");
        if ((!result.hasMember("access_token")) || (lifetime <= 0)) {
            System.out.println("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.
        JsonObject timing = new JsonObject();
        timing.updateString("issued_at", issued.getAsTimestamp(false));
        timing.updateInt("expires_in", lifetime);
        try {
            timing.writeFile("paypal-token-timing.json");
        } catch (ChilkatException e) {
            System.out.println("Could not save token timing.");
            return;
        }

        System.out.println("Token received; lifetime in seconds: " + lifetime);
    }
}