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

Unicode C++ Examples

Web API Categories

ASN.1
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Geolocation
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text

HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(Unicode C++) Google Sheets Refresh an Expired Access Token

Demonstrates how to refresh an expired access token. If a Google Sheets REST API call results in a 401 "unauthorized" response, then it is likely the access token expired. An application should be able to automatically refresh the access token with no user interaction and then re-try the request with the newly obtained access token.

Chilkat C/C++ Library Downloads

MS Visual C/C++

Linux/CentOS C/C++

Alpine Linux C/C++

MAC OS X C/C++

armhf/aarch64 C/C++

C++ Builder

iOS C/C++

Android C/C++

Win Mobile 5.0/Pocket PC 2003

Solaris C/C++

FreeBSD C/C++

OpenBSD C/C++

MinGW C/C++

#include <CkJsonObjectW.h>
#include <CkOAuth2W.h>
#include <CkStringBuilderW.h>

void ChilkatSample(void)
    {
    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    //  It is assumed we previously obtained an OAuth2 access token.
    //  This example loads the JSON access token file
    //  saved by this example: Get Google Sheets OAuth2 Access Token

    CkJsonObjectW jsonToken;
    bool success = jsonToken.LoadFile(L"qa_data/tokens/googleSheets.json");
    if (success != true) {
        wprintf(L"Failed to load googleSheets.json\n");
        return;
    }

    //  The access token JSON looks like this:

    //  	{
    //  	 "access_token": "abac.Ci-ZabcdF37-NUrmSzxbHxTsMbtlN-uPfabcdAQN_2ekzj0s5ofCzRabcdTDMGaa2Q",
    //  	 "token_type": "Bearer",
    //  	 "expires_in": 3600,
    //  	 "refresh_token": "1/lEe5abcdNJ77eOSDBabcdkQGVmDalkwabcdR2Hatbo"
    //  	}
    // 

    CkOAuth2W oauth2;

    oauth2.put_TokenEndpoint(L"https://www.googleapis.com/oauth2/v4/token");

    //  Replace these with actual values.
    oauth2.put_ClientId(L"GOOGLE-CLIENT-ID");
    oauth2.put_ClientSecret(L"GOOGLE-CLIENT-SECRET");

    //  Get the "refresh_token"
    oauth2.put_RefreshToken(jsonToken.stringOf(L"refresh_token"));

    //  Send the HTTP POST to refresh the access token..
    success = oauth2.RefreshAccessToken();
    if (success != true) {
        wprintf(L"%s\n",oauth2.lastErrorText());
        return;
    }

    //  The response contains a new access token, but we must keep
    //  our existing refresh token for when we need to refresh again in the future.
    jsonToken.UpdateString(L"access_token",oauth2.accessToken());

    //  Save the new JSON access token response to a file.
    CkStringBuilderW sbJson;
    jsonToken.put_EmitCompact(false);
    jsonToken.EmitSb(sbJson);
    sbJson.WriteFile(L"qa_data/tokens/googleSheets.json",L"utf-8",false);

    wprintf(L"OAuth2 authorization granted!\n");
    wprintf(L"New Access Token = %s\n",oauth2.accessToken());
    }

 

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