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

VBScript Examples

ASN.1
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
Facebook
FileAccess
Firebase
GMail REST API
GeoOp
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Sheets
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)
Jira
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
PayPal
Peoplevox
QuickBooks
REST
REST Misc
RSA Encryption
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Shopify
Socket/SSL/TLS
Spider
Stream
Stripe
SugarCRM
Tar Archive
Twitter
Upload
VoiceBase
Walmart
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Xero
Zip
curl
eBay

 

 

 

(VBScript) Refresh HMRC OAuth2 Access Token

Demonstrates how to refresh an HMRC access token.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set outFile = fso.CreateTextFile("output.txt", True)

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

'  See Get HMRC Access Token
'  for sample code showing how to obtain a HMRC access token for desktop apps.

'  It is assumed we previously obtained an OAuth2 access token for HMRC.

set jsonToken = CreateObject("Chilkat_9_5_0.JsonObject")
success = jsonToken.LoadFile("qa_data/tokens/hmrc.json")
If (success <> 1) Then
    outFile.WriteLine("Failed to load hmrc.json")
    WScript.Quit
End If

'  The access token JSON looks something like this:

'  {
'    "token_type": "Bearer",
'    "scope": "user_impersonation",
'    "expires_in": "3599",
'    "ext_expires_in": "0",
'    "expires_on": "1524783438",
'    "not_before": "1524779538",
'    "resource": "https://mydomain.api.crm.dynamics.com",
'    "access_token": "...",
'    "refresh_token": "...",
'    "id_token": "..."
'  }

set oauth2 = CreateObject("Chilkat_9_5_0.OAuth2")

oauth2.TokenEndpoint = "https://test-api.service.hmrc.gov.uk/oauth/token"

'  Replace these with actual values.
oauth2.ClientId = "HMRC-CLIENT-ID"
oauth2.ClientSecret = "HMRC-CLIENT-SECRET"

'  Get the "refresh_token"
oauth2.RefreshToken = jsonToken.StringOf("refresh_token")

'  Send the HTTP POST to refresh the access token..
success = oauth2.RefreshAccessToken()
If (success <> 1) Then
    outFile.WriteLine(oauth2.LastErrorText)
    WScript.Quit
End If

'  Save the new JSON access token response to a file.
'  The access + refresh tokens contained in this JSON will be needed for the next refresh.
set sbJson = CreateObject("Chilkat_9_5_0.StringBuilder")
jsonToken.EmitCompact = 0
success = jsonToken.EmitSb(sbJson)
success = sbJson.WriteFile("qa_data/tokens/hmrc.json","utf-8",0)

outFile.WriteLine("OAuth2 authorization granted!")
outFile.WriteLine("New Access Token = " & oauth2.AccessToken)

outFile.Close

 

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