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

PayPal: Decide when to renew an access token

See more PayPal Examples

Use the UTC request time and expires_in from the token example. Renew five minutes early. This is a cache decision; a revoked token can still receive HTTP 401 before its calculated expiry.

PayPal documentation

Chilkat Android™ Downloads

Android™
// 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);

    //  Read the metadata saved by Get a sandbox access token.
    CkJsonObject timing = new CkJsonObject();
    CkDateTime issued = new CkDateTime();
    boolean loadedTiming = timing.LoadFile("paypal-token-timing.json");
    if (loadedTiming != true) {
        Log.i(TAG, "No timing metadata. Request a token.");
        return;
        }

    boolean validTimestamp = issued.SetFromTimestamp(timing.stringOf("issued_at"));
    if (validTimestamp != true) {
        Log.i(TAG, "Invalid issue time. Request a token.");
        return;
        }

    CkDateTime now = new CkDateTime();
    now.SetFromCurrentSystemTime();
    int age = now.DiffSeconds(issued);
    int lifetime = timing.IntOf("expires_in");
    if ((age < 0) or (lifetime <= 300) or (age >= (lifetime - 300))) {
        Log.i(TAG, "Request a new client-credentials token.");
        return;
        }

    Log.i(TAG, "Reuse the matching token from the credential store.");

  }

  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."
  }
}