Sample code for 30+ languages & platforms
PowerBuilder Requires Chilkat v11.6.0+

OneDrive: Obtain and Refresh a SharePoint Transfer Token

See more OneDrive Examples

Obtain and refresh a delegated SharePoint Online access token for the OneDrive download, upload-session, version-download, and copy-monitor examples. This is for Microsoft 365 work/school accounts, not personal OneDrive. It saves qa_data/tokens/oneDrive-sharepoint.json with the exact host and access_token those transfer examples read, plus a refresh token, lifetime, acquisition time, and cache context. The Graph cache oneDrive.json remains separate.

Register a public application in Microsoft Entra, select the intended organizational account type, and add http://localhost:3017/ as a Mobile and desktop applications redirect URI. Set tenantId to the Directory (tenant) GUID and clientId to the Application (client) ID. Add the appropriate SharePoint delegated permissions in API permissions: use MyFiles.Read for reading the user's files or MyFiles.Write for writing them; use AllSites.Read or AllSites.Write where broader SharePoint site access is needed. Select only the permissions required and obtain consent as tenant policy requires. The user's actual access still applies. Graph permissions alone do not establish this SharePoint grant.

Set sharepointHost from trusted configuration: typically tenant.sharepoint.com for SharePoint libraries or tenant-my.sharepoint.com for the user's business OneDrive. Use the exact host expected by the transfer examples, without a scheme, path, port, or wildcard. This global-cloud example requests https://{host}/.default, which uses configured, consented SharePoint permissions, plus offline_access during authorization. Do not combine .default with dynamic API permission scopes. If newly configured permissions are not already consented, arrange renewed consent; refreshing does not grant them. How .default consent works.

Run action="authorize" for initial PKCE browser sign-in, choosing the same intended user as the Graph workflow. This explicitly replaces an existing cache after validation and backup. Change to action="refresh" for unattended renewal before obtained_at plus expires_in, allowing a safety margin. Refresh sends an explicit SharePoint resource scope and never opens a browser automatically. It saves a rotated refresh token, or retains the previous one when omitted. Revocation, Conditional Access, invalid_grant, or interaction requirements can require a new sign-in.

The local accountKey is an application-defined cache key, not a verified identity claim. The refresh branch requires matching host, tenant, client, account key, and resource scope before sending credentials. Use separate protected caches for different contexts and update the transfer cache path accordingly. Existing hand-written caches without the refresh/context fields need authorize. Do not decode an access token and assume its internal format or audience claim is a stable client contract.

Only validated responses replace the cache. A checked private backup supports recovery; production applications should serialize refreshes and commit credentials atomically in protected storage. Neither token is printed. A loopback listener is required only for initial sign-in; no hosted web server is needed. This is not certificate-based application access, which obtains new tokens through client credentials and has no refresh token.

OneDrive transfer authentication guide. Microsoft's SharePoint delegated permission overview.

Permission: Configured SharePoint delegated permissions and offline_access.

Check the expected HTTP status before using a response body. A false method result can still contain an HTTP error response; status zero means no response arrived. For 401, renew authorization; for 403, check permissions, access grants, and policy; for 429, honor Retry-After with a bounded retry. Reconcile uncertain writes before repeating them. Keep credentials, file contents, upload/download URLs, and detailed diagnostics private.

Microsoft documentation

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
string ls_Action
string ls_TenantId
string ls_ClientId
string ls_SharepointHost
string ls_AccountKey
string ls_CachePath
string ls_BackupPath
oleobject loo_Host
oleobject loo_Match
long li_Matched
oleobject loo_Tenant
oleobject loo_Http
oleobject loo_TokenEndpoint
oleobject loo_AuthorizeEndpoint
oleobject loo_ScopeBuilder
string ls_ResourceScope
oleobject loo_Fac
long li_HadCache
oleobject loo_Previous
oleobject loo_Result
oleobject loo_Oauth
oleobject loo_Scopes
string ls_AuthUrl
long li_Waited
oleobject loo_Req
oleobject loo_Resp
oleobject loo_Obtained

li_Success = 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
li_Success = 0
//  Run authorize once; change to refresh for subsequent unattended renewals.
ls_Action = "authorize"
ls_TenantId = "YOUR_TENANT_ID"
ls_ClientId = "YOUR_APPLICATION_CLIENT_ID"
//  A trusted bare hostname, e.g. tenant.sharepoint.com or tenant-my.sharepoint.com.
//  Configure the exact host used by the intended transfer; do not copy an untrusted URL here.
ls_SharepointHost = "YOUR_TENANT.sharepoint.com"
//  An application-defined key for this user/account, not an access token or password.
ls_AccountKey = "YOUR_LOCAL_ACCOUNT_KEY"
ls_CachePath = "qa_data/tokens/oneDrive-sharepoint.json"
ls_BackupPath = "qa_data/tokens/oneDrive-sharepoint.json.bak"
if (ls_Action <> "authorize") AND (ls_Action <> "refresh") then
    Write-Debug "Choose authorize or refresh."
    return
end if

loo_Host = create oleobject
li_rc = loo_Host.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
    destroy loo_Host
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Host.Append(ls_SharepointHost)
loo_Match = create oleobject
li_rc = loo_Match.ConnectToNewObject("Chilkat.JsonObject")

li_Matched = loo_Host.RegexMatch("(?i)\A[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.sharepoint\.com\z",loo_Match,100)
if li_Matched <> 1 then
    Write-Debug "Set the trusted SharePoint hostname for this global-cloud example, without a scheme, path, or port."
    destroy loo_Host
    destroy loo_Match
    return
end if

//  Use the organization's Directory (tenant) ID, not common, organizations, or consumers.
loo_Tenant = create oleobject
li_rc = loo_Tenant.ConnectToNewObject("Chilkat.StringBuilder")

loo_Tenant.Append(ls_TenantId)
li_Matched = loo_Tenant.RegexMatch("(?i)\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z",loo_Match,100)
if li_Matched <> 1 then
    Write-Debug "Set the organization tenant GUID before running."
    destroy loo_Host
    destroy loo_Match
    destroy loo_Tenant
    return
end if

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")

loo_Http.RequireSslCertVerify = 1
loo_Http.FollowRedirects = 0
loo_Http.ConnectTimeout = 20
loo_Http.ReadTimeout = 30
loo_TokenEndpoint = create oleobject
li_rc = loo_TokenEndpoint.ConnectToNewObject("Chilkat.StringBuilder")

loo_TokenEndpoint.Append("https://login.microsoftonline.com/")
loo_TokenEndpoint.Append(loo_Http.UrlEncode(ls_TenantId))
loo_TokenEndpoint.Append("/oauth2/v2.0/token")
loo_AuthorizeEndpoint = create oleobject
li_rc = loo_AuthorizeEndpoint.ConnectToNewObject("Chilkat.StringBuilder")

loo_AuthorizeEndpoint.Append("https://login.microsoftonline.com/")
loo_AuthorizeEndpoint.Append(loo_Http.UrlEncode(ls_TenantId))
loo_AuthorizeEndpoint.Append("/oauth2/v2.0/authorize")
loo_ScopeBuilder = create oleobject
li_rc = loo_ScopeBuilder.ConnectToNewObject("Chilkat.StringBuilder")

loo_ScopeBuilder.Append("https://")
loo_ScopeBuilder.Append(ls_SharepointHost)
loo_ScopeBuilder.Append("/.default")
ls_ResourceScope = loo_ScopeBuilder.GetAsString()
loo_Fac = create oleobject
li_rc = loo_Fac.ConnectToNewObject("Chilkat.FileAccess")

li_HadCache = loo_Fac.FileExists(ls_CachePath)
loo_Previous = create oleobject
li_rc = loo_Previous.ConnectToNewObject("Chilkat.JsonObject")

loo_Previous.Strict = 1
if li_HadCache = 1 then
    li_Success = loo_Previous.LoadFile(ls_CachePath)
    if li_Success <> 1 then
        Write-Debug "Invalid existing cache; recover its private backup before continuing."
        destroy loo_Host
        destroy loo_Match
        destroy loo_Tenant
        destroy loo_Http
        destroy loo_TokenEndpoint
        destroy loo_AuthorizeEndpoint
        destroy loo_ScopeBuilder
        destroy loo_Fac
        destroy loo_Previous
        return
    end if

end if

loo_Result = create oleobject
li_rc = loo_Result.ConnectToNewObject("Chilkat.JsonObject")

loo_Result.Strict = 1
if ls_Action = "authorize" then
    //  Register http://localhost:3017/ as a Mobile and desktop applications redirect URI.
    loo_Oauth = create oleobject
    li_rc = loo_Oauth.ConnectToNewObject("Chilkat.OAuth2")

    loo_Oauth.AuthorizationEndpoint = loo_AuthorizeEndpoint.GetAsString()
    loo_Oauth.TokenEndpoint = loo_TokenEndpoint.GetAsString()
    loo_Oauth.ClientId = ls_ClientId
    loo_Oauth.LocalHost = "localhost"
    loo_Oauth.ListenPort = 3017
    loo_Oauth.CodeChallenge = 1
    loo_Oauth.CodeChallengeMethod = "S256"
    //  No client secret in a public desktop application.
    loo_Scopes = create oleobject
    li_rc = loo_Scopes.ConnectToNewObject("Chilkat.StringBuilder")

    loo_Scopes.Append("offline_access ")
    loo_Scopes.Append(ls_ResourceScope)
    loo_Oauth.Scope = loo_Scopes.GetAsString()
    li_Success = loo_Oauth.AddAuthQueryParam("prompt","select_account")
    if li_Success <> 1 then
        Write-Debug "Could not configure the authorization request."
        destroy loo_Host
        destroy loo_Match
        destroy loo_Tenant
        destroy loo_Http
        destroy loo_TokenEndpoint
        destroy loo_AuthorizeEndpoint
        destroy loo_ScopeBuilder
        destroy loo_Fac
        destroy loo_Previous
        destroy loo_Result
        destroy loo_Oauth
        destroy loo_Scopes
        return
    end if

    ls_AuthUrl = loo_Oauth.StartAuth()
    if loo_Oauth.LastMethodSuccess <> 1 then
        Write-Debug "Could not start the local authorization listener."
        destroy loo_Host
        destroy loo_Match
        destroy loo_Tenant
        destroy loo_Http
        destroy loo_TokenEndpoint
        destroy loo_AuthorizeEndpoint
        destroy loo_ScopeBuilder
        destroy loo_Fac
        destroy loo_Previous
        destroy loo_Result
        destroy loo_Oauth
        destroy loo_Scopes
        return
    end if

    li_Success = loo_Oauth.LaunchBrowser(ls_AuthUrl)
    if li_Success <> 1 then
        loo_Oauth.Cancel()
        Write-Debug "Could not open the system browser."
        destroy loo_Host
        destroy loo_Match
        destroy loo_Tenant
        destroy loo_Http
        destroy loo_TokenEndpoint
        destroy loo_AuthorizeEndpoint
        destroy loo_ScopeBuilder
        destroy loo_Fac
        destroy loo_Previous
        destroy loo_Result
        destroy loo_Oauth
        destroy loo_Scopes
        return
    end if

    li_Waited = 0
    do while ((loo_Oauth.AuthFlowState < 3) AND (li_Waited < 180000))
        loo_Oauth.SleepMs(200)
        li_Waited = li_Waited + 200
    loop
    if loo_Oauth.AuthFlowState < 3 then
        loo_Oauth.Cancel()
        Write-Debug "Authorization timed out."
        destroy loo_Host
        destroy loo_Match
        destroy loo_Tenant
        destroy loo_Http
        destroy loo_TokenEndpoint
        destroy loo_AuthorizeEndpoint
        destroy loo_ScopeBuilder
        destroy loo_Fac
        destroy loo_Previous
        destroy loo_Result
        destroy loo_Oauth
        destroy loo_Scopes
        return
    end if

    if loo_Oauth.AuthFlowState <> 3 then
        Write-Debug "Authorization did not complete. Flow state: " + string(loo_Oauth.AuthFlowState)
        destroy loo_Host
        destroy loo_Match
        destroy loo_Tenant
        destroy loo_Http
        destroy loo_TokenEndpoint
        destroy loo_AuthorizeEndpoint
        destroy loo_ScopeBuilder
        destroy loo_Fac
        destroy loo_Previous
        destroy loo_Result
        destroy loo_Oauth
        destroy loo_Scopes
        return
    end if

    li_Success = loo_Result.Load(loo_Oauth.AccessTokenResponse)
    if li_Success <> 1 then
        Write-Debug "Invalid token response; existing cache unchanged."
        destroy loo_Host
        destroy loo_Match
        destroy loo_Tenant
        destroy loo_Http
        destroy loo_TokenEndpoint
        destroy loo_AuthorizeEndpoint
        destroy loo_ScopeBuilder
        destroy loo_Fac
        destroy loo_Previous
        destroy loo_Result
        destroy loo_Oauth
        destroy loo_Scopes
        return
    end if

    if (loo_Result.JsonTypeOf("refresh_token") <> 1) OR (loo_Result.StringOfEquals("refresh_token","",1) = 1) then
        Write-Debug "Expected a nonempty refresh_token in the JSON."
        destroy loo_Host
        destroy loo_Match
        destroy loo_Tenant
        destroy loo_Http
        destroy loo_TokenEndpoint
        destroy loo_AuthorizeEndpoint
        destroy loo_ScopeBuilder
        destroy loo_Fac
        destroy loo_Previous
        destroy loo_Result
        destroy loo_Oauth
        destroy loo_Scopes
        return
    end if

else
    if li_HadCache <> 1 then
        Write-Debug "No SharePoint token cache. Run authorize first."
        destroy loo_Host
        destroy loo_Match
        destroy loo_Tenant
        destroy loo_Http
        destroy loo_TokenEndpoint
        destroy loo_AuthorizeEndpoint
        destroy loo_ScopeBuilder
        destroy loo_Fac
        destroy loo_Previous
        destroy loo_Result
        destroy loo_Oauth
        destroy loo_Scopes
        return
    end if

    if loo_Previous.StringOfEquals("host",ls_SharepointHost,1) <> 1 then
        Write-Debug "Cache context differs from the configured host, tenant, app, or account. Authorize deliberately or use a separate cache."
        destroy loo_Host
        destroy loo_Match
        destroy loo_Tenant
        destroy loo_Http
        destroy loo_TokenEndpoint
        destroy loo_AuthorizeEndpoint
        destroy loo_ScopeBuilder
        destroy loo_Fac
        destroy loo_Previous
        destroy loo_Result
        destroy loo_Oauth
        destroy loo_Scopes
        return
    end if

    if loo_Previous.StringOfEquals("tenant_id",ls_TenantId,1) <> 1 then
        Write-Debug "Cache context differs from the configured host, tenant, app, or account. Authorize deliberately or use a separate cache."
        destroy loo_Host
        destroy loo_Match
        destroy loo_Tenant
        destroy loo_Http
        destroy loo_TokenEndpoint
        destroy loo_AuthorizeEndpoint
        destroy loo_ScopeBuilder
        destroy loo_Fac
        destroy loo_Previous
        destroy loo_Result
        destroy loo_Oauth
        destroy loo_Scopes
        return
    end if

    if loo_Previous.StringOfEquals("client_id",ls_ClientId,1) <> 1 then
        Write-Debug "Cache context differs from the configured host, tenant, app, or account. Authorize deliberately or use a separate cache."
        destroy loo_Host
        destroy loo_Match
        destroy loo_Tenant
        destroy loo_Http
        destroy loo_TokenEndpoint
        destroy loo_AuthorizeEndpoint
        destroy loo_ScopeBuilder
        destroy loo_Fac
        destroy loo_Previous
        destroy loo_Result
        destroy loo_Oauth
        destroy loo_Scopes
        return
    end if

    if loo_Previous.StringOfEquals("account_key",ls_AccountKey,1) <> 1 then
        Write-Debug "Cache context differs from the configured host, tenant, app, or account. Authorize deliberately or use a separate cache."
        destroy loo_Host
        destroy loo_Match
        destroy loo_Tenant
        destroy loo_Http
        destroy loo_TokenEndpoint
        destroy loo_AuthorizeEndpoint
        destroy loo_ScopeBuilder
        destroy loo_Fac
        destroy loo_Previous
        destroy loo_Result
        destroy loo_Oauth
        destroy loo_Scopes
        return
    end if

    if loo_Previous.StringOfEquals("resource_scope",ls_ResourceScope,1) <> 1 then
        Write-Debug "Cache context differs from the configured host, tenant, app, or account. Authorize deliberately or use a separate cache."
        destroy loo_Host
        destroy loo_Match
        destroy loo_Tenant
        destroy loo_Http
        destroy loo_TokenEndpoint
        destroy loo_AuthorizeEndpoint
        destroy loo_ScopeBuilder
        destroy loo_Fac
        destroy loo_Previous
        destroy loo_Result
        destroy loo_Oauth
        destroy loo_Scopes
        return
    end if

    if (loo_Previous.JsonTypeOf("refresh_token") <> 1) OR (loo_Previous.StringOfEquals("refresh_token","",1) = 1) then
        Write-Debug "Expected a nonempty refresh_token in the JSON."
        destroy loo_Host
        destroy loo_Match
        destroy loo_Tenant
        destroy loo_Http
        destroy loo_TokenEndpoint
        destroy loo_AuthorizeEndpoint
        destroy loo_ScopeBuilder
        destroy loo_Fac
        destroy loo_Previous
        destroy loo_Result
        destroy loo_Oauth
        destroy loo_Scopes
        return
    end if

    loo_Req = create oleobject
    li_rc = loo_Req.ConnectToNewObject("Chilkat.HttpRequest")

    loo_Req.HttpVerb = "POST"
    loo_Req.ContentType = "application/x-www-form-urlencoded"
    loo_Req.AddParam("client_id",ls_ClientId)
    loo_Req.AddParam("grant_type","refresh_token")
    loo_Req.AddParam("refresh_token",loo_Previous.StringOf("refresh_token"))
    //  Explicit resource scope keeps the renewal on SharePoint, not Microsoft Graph.
    loo_Req.AddParam("scope",ls_ResourceScope)
    loo_Resp = create oleobject
    li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

    li_Success = loo_Http.HttpReq(loo_TokenEndpoint.GetAsString(),loo_Req,loo_Resp)
    if (li_Success <> 1) AND (loo_Resp.StatusCode = 0) then
        Write-Debug "No token response received; existing cache unchanged."
        destroy loo_Host
        destroy loo_Match
        destroy loo_Tenant
        destroy loo_Http
        destroy loo_TokenEndpoint
        destroy loo_AuthorizeEndpoint
        destroy loo_ScopeBuilder
        destroy loo_Fac
        destroy loo_Previous
        destroy loo_Result
        destroy loo_Oauth
        destroy loo_Scopes
        destroy loo_Req
        destroy loo_Resp
        return
    end if

    if loo_Resp.StatusCode <> 200 then
        Write-Debug "Token renewal failed. HTTP status: " + string(loo_Resp.StatusCode)
        if loo_Resp.StatusCode = 429 then
            Write-Debug "Retry-After: " + loo_Resp.GetHeaderField("Retry-After")
        end if

        Write-Debug "Check consent and token validity. If interaction is required, run authorize deliberately."
        destroy loo_Host
        destroy loo_Match
        destroy loo_Tenant
        destroy loo_Http
        destroy loo_TokenEndpoint
        destroy loo_AuthorizeEndpoint
        destroy loo_ScopeBuilder
        destroy loo_Fac
        destroy loo_Previous
        destroy loo_Result
        destroy loo_Oauth
        destroy loo_Scopes
        destroy loo_Req
        destroy loo_Resp
        return
    end if

    if li_Success <> 1 then
        Write-Debug "Token response did not complete; existing cache unchanged."
        destroy loo_Host
        destroy loo_Match
        destroy loo_Tenant
        destroy loo_Http
        destroy loo_TokenEndpoint
        destroy loo_AuthorizeEndpoint
        destroy loo_ScopeBuilder
        destroy loo_Fac
        destroy loo_Previous
        destroy loo_Result
        destroy loo_Oauth
        destroy loo_Scopes
        destroy loo_Req
        destroy loo_Resp
        return
    end if

    li_Success = loo_Result.Load(loo_Resp.BodyStr)
    if li_Success <> 1 then
        Write-Debug "Invalid refresh response; existing cache unchanged."
        destroy loo_Host
        destroy loo_Match
        destroy loo_Tenant
        destroy loo_Http
        destroy loo_TokenEndpoint
        destroy loo_AuthorizeEndpoint
        destroy loo_ScopeBuilder
        destroy loo_Fac
        destroy loo_Previous
        destroy loo_Result
        destroy loo_Oauth
        destroy loo_Scopes
        destroy loo_Req
        destroy loo_Resp
        return
    end if

    if loo_Result.JsonTypeOf("refresh_token") = -1 then
        loo_Result.UpdateString("refresh_token",loo_Previous.StringOf("refresh_token"))
    else
        if (loo_Result.JsonTypeOf("refresh_token") <> 1) OR (loo_Result.StringOfEquals("refresh_token","",1) = 1) then
            Write-Debug "Expected a nonempty refresh_token in the JSON."
            destroy loo_Host
            destroy loo_Match
            destroy loo_Tenant
            destroy loo_Http
            destroy loo_TokenEndpoint
            destroy loo_AuthorizeEndpoint
            destroy loo_ScopeBuilder
            destroy loo_Fac
            destroy loo_Previous
            destroy loo_Result
            destroy loo_Oauth
            destroy loo_Scopes
            destroy loo_Req
            destroy loo_Resp
            return
        end if

    end if

end if

if (loo_Result.JsonTypeOf("access_token") <> 1) OR (loo_Result.StringOfEquals("access_token","",1) = 1) then
    Write-Debug "Expected a nonempty access_token in the JSON."
    destroy loo_Host
    destroy loo_Match
    destroy loo_Tenant
    destroy loo_Http
    destroy loo_TokenEndpoint
    destroy loo_AuthorizeEndpoint
    destroy loo_ScopeBuilder
    destroy loo_Fac
    destroy loo_Previous
    destroy loo_Result
    destroy loo_Oauth
    destroy loo_Scopes
    destroy loo_Req
    destroy loo_Resp
    return
end if

if (loo_Result.JsonTypeOf("expires_in") <> 2) OR (loo_Result.IntOf("expires_in") <= 0) then
    Write-Debug "Expected a positive access-token lifetime; existing cache unchanged."
    destroy loo_Host
    destroy loo_Match
    destroy loo_Tenant
    destroy loo_Http
    destroy loo_TokenEndpoint
    destroy loo_AuthorizeEndpoint
    destroy loo_ScopeBuilder
    destroy loo_Fac
    destroy loo_Previous
    destroy loo_Result
    destroy loo_Oauth
    destroy loo_Scopes
    destroy loo_Req
    destroy loo_Resp
    return
end if

if loo_Result.StringOfEquals("token_type","Bearer",0) <> 1 then
    Write-Debug "Expected a bearer access token; existing cache unchanged."
    destroy loo_Host
    destroy loo_Match
    destroy loo_Tenant
    destroy loo_Http
    destroy loo_TokenEndpoint
    destroy loo_AuthorizeEndpoint
    destroy loo_ScopeBuilder
    destroy loo_Fac
    destroy loo_Previous
    destroy loo_Result
    destroy loo_Oauth
    destroy loo_Scopes
    destroy loo_Req
    destroy loo_Resp
    return
end if

loo_Obtained = create oleobject
li_rc = loo_Obtained.ConnectToNewObject("Chilkat.CkDateTime")

li_Success = loo_Obtained.SetFromCurrentSystemTime()
if li_Success <> 1 then
    Write-Debug "Could not timestamp the access token."
    destroy loo_Host
    destroy loo_Match
    destroy loo_Tenant
    destroy loo_Http
    destroy loo_TokenEndpoint
    destroy loo_AuthorizeEndpoint
    destroy loo_ScopeBuilder
    destroy loo_Fac
    destroy loo_Previous
    destroy loo_Result
    destroy loo_Oauth
    destroy loo_Scopes
    destroy loo_Req
    destroy loo_Resp
    destroy loo_Obtained
    return
end if

loo_Result.UpdateString("obtained_at",loo_Obtained.GetAsTimestamp(0))
//  The transfer examples read host and access_token; these extra fields bind future refreshes.
loo_Result.UpdateString("host",ls_SharepointHost)
loo_Result.UpdateString("tenant_id",ls_TenantId)
loo_Result.UpdateString("client_id",ls_ClientId)
loo_Result.UpdateString("account_key",ls_AccountKey)
loo_Result.UpdateString("resource_scope",ls_ResourceScope)
li_Success = loo_Fac.DirEnsureExists("qa_data/tokens")
if li_Success <> 1 then
    Write-Debug "Could not create the private cache directory."
    destroy loo_Host
    destroy loo_Match
    destroy loo_Tenant
    destroy loo_Http
    destroy loo_TokenEndpoint
    destroy loo_AuthorizeEndpoint
    destroy loo_ScopeBuilder
    destroy loo_Fac
    destroy loo_Previous
    destroy loo_Result
    destroy loo_Oauth
    destroy loo_Scopes
    destroy loo_Req
    destroy loo_Resp
    destroy loo_Obtained
    return
end if

if li_HadCache = 1 then
    li_Success = loo_Previous.WriteFile(ls_BackupPath)
    if li_Success <> 1 then
        Write-Debug "Could not back up the previous cache; no replacement attempted."
        destroy loo_Host
        destroy loo_Match
        destroy loo_Tenant
        destroy loo_Http
        destroy loo_TokenEndpoint
        destroy loo_AuthorizeEndpoint
        destroy loo_ScopeBuilder
        destroy loo_Fac
        destroy loo_Previous
        destroy loo_Result
        destroy loo_Oauth
        destroy loo_Scopes
        destroy loo_Req
        destroy loo_Resp
        destroy loo_Obtained
        return
    end if

end if

li_Success = loo_Result.WriteFile(ls_CachePath)
if li_Success <> 1 then
    Write-Debug "Could not save the new cache; recover the private backup if an older cache existed."
    destroy loo_Host
    destroy loo_Match
    destroy loo_Tenant
    destroy loo_Http
    destroy loo_TokenEndpoint
    destroy loo_AuthorizeEndpoint
    destroy loo_ScopeBuilder
    destroy loo_Fac
    destroy loo_Previous
    destroy loo_Result
    destroy loo_Oauth
    destroy loo_Scopes
    destroy loo_Req
    destroy loo_Resp
    destroy loo_Obtained
    return
end if

//  Never print the tokens, token response, or authorization URL.
Write-Debug "SharePoint token cache saved. The OneDrive transfer examples can now load it."


destroy loo_Host
destroy loo_Match
destroy loo_Tenant
destroy loo_Http
destroy loo_TokenEndpoint
destroy loo_AuthorizeEndpoint
destroy loo_ScopeBuilder
destroy loo_Fac
destroy loo_Previous
destroy loo_Result
destroy loo_Oauth
destroy loo_Scopes
destroy loo_Req
destroy loo_Resp
destroy loo_Obtained