Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

Node.js Web API Examples

Primary Categories

AWS Translate
Activix CRM
Adyen
Amazon MWS
Azure Maps
Azure Monitor
Azure OAuth2
Azure Storage Accounts
Bluzone
CardConnect
ClickBank
Clickatell
Cloudfare
DocuSign
ETrade
Etsy
Facebook
GeoOp
GetHarvest
Jira
MYOB

Magento
Microsoft Calendar
Microsoft Group
Microsoft Tasks and Plans
Okta OAuth/OIDC
OneLogin OIDC
PayPal
Peoplevox
Populi
QuickBooks
SOAP finkok.com
Shippo
Shopify
Stripe
SugarCRM
Trello
Twitter
VoiceBase
Vonage
Walmart
Walmart v3
Xero
eBay
effectconnect

 

 

 

(Node.js) Check PayPal Access Token Expiration

Checks to see if the PayPal access token expired.

Install Chilkat for Node.js and Electron using npm at

Chilkat npm packages for Node.js

Chilkat npm packages for Electron

on Windows, Linux, MacOSX, and ARM

var os = require('os');
if (os.platform() == 'win32') {  
    if (os.arch() == 'ia32') {
        var chilkat = require('@chilkat/ck-node11-win-ia32');
    } else {
        var chilkat = require('@chilkat/ck-node11-win64'); 
    }
} else if (os.platform() == 'linux') {
    if (os.arch() == 'arm') {
        var chilkat = require('@chilkat/ck-node11-arm');
    } else if (os.arch() == 'x86') {
        var chilkat = require('@chilkat/ck-node11-linux32');
    } else {
        var chilkat = require('@chilkat/ck-node11-linux64');
    }
} else if (os.platform() == 'darwin') {
    var chilkat = require('@chilkat/ck-node11-macosx');
}

function chilkatExample() {

    // Note: Requires Chilkat v9.5.0.64 or greater.

    // This requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // In the example linked here: PayPal OAuth2 Token,
    // we fetched a PayPal access token and saved it to a JSON file.
    // We also recorded the current date/time.
    // Load this JSON file and compare the current date/time with the fetch date/time
    // and the "expires_in" value to see if the token is expired.

    var json = new chilkat.JsonObject();
    var success = json.LoadFile("qa_data/tokens/paypal.json");
    if (success !== true) {
        console.log("Failed to load access key json file.");
        return;
    }

    // Get the current date/time.
    var dateTime = new chilkat.CkDateTime();
    var bLocalTime = false;
    var dtNow = dateTime.GetAsUnixTime(bLocalTime);

    // Get the access token create date/time
    var dtCreate = json.IntOf("tokenCreateTimeUtc");

    // Find out how many seconds have elapsed.
    var numSeconds = dtNow - dtCreate;

    // Get the expires_in value from the JSON.
    var expires_in = json.IntOf("expires_in");

    console.log("token age (in seconds) = " + numSeconds);
    console.log("expires_in = " + expires_in);

    // If the token expired, or if it will expire in 5 minutes or less,
    // get another token.
    if (numSeconds < (expires_in - 300)) {
        console.log("The token is not yet expired.  No need to fetch another.");
        return;
    }

    console.log("Time to fetch a new access token...");

    // Get another token..
    // See the example at Get PayPal OAuth2 Token
    // to get another token..

}

chilkatExample();

 

© 2000-2019 Chilkat Software, Inc. All Rights Reserved.