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

OneDrive: List Versions and Download a Previous Version

See more OneDrive Examples

Enumerate a file's version history, then optionally download an explicitly selected older version. The default only lists. Keep version IDs such as 1.0 as strings and do not infer the latest version by numeric sorting. Available history and fields depend on the drive and retention settings. The version-content API cannot download the current version; use the ordinary file-content download example for that.

Accepts direct content or the documented redirect response. Binary bytes, including empty files, are saved to a generated local filename only after HTTP success. This demonstration buffers the version in memory; use streamed download handling for large files. Downloading a version does not restore or change the current file.

Follows complete @odata.nextLink URLs, including after empty pages. It validates the Graph host/API path, rejects repeated links, and reports a stop at the demonstration bound of 100 pages. $top is page size, not the total result count. Enumeration is not a transactionally consistent snapshot during concurrent changes.

Microsoft has announced changes to preauthenticated URLs beginning April 1, 2027. The example supports Graph-hosted URLs with the Graph token and an explicitly configured SharePoint host with a separate SharePoint-audience token. A legacy preauthenticated URL is requested without a bearer token. Never forward the Graph token to a SharePoint or content host. If required, prepare private qa_data/tokens/oneDrive-sharepoint.json with host and access_token from a separately authorized SharePoint token acquisition; token refresh must preserve that audience. A 401 requires checking the correct token audience or URL expiry, not blindly adding credentials.

Permission: Files.Read.

Uses direct Chilkat.Http requests to Microsoft Graph v1.0 in the global cloud. The delegated token is read from the private qa_data/tokens/oneDrive.json file created by the PKCE authorization example. Personal Microsoft accounts and Microsoft 365 work/school accounts can use delegated authorization when the app registration supports them. Refresh expired tokens; scopes and actual file access both matter.

For app-only Microsoft 365 access, use client credentials with administrator-approved application permissions. /me requires a signed-in user; app-only calls must address an authorized user, site, or drive explicitly. Client credentials do not grant access to personal OneDrive accounts. National clouds use their own authority and Graph hosts.

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 SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @iTmp1 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @sTmp1 nvarchar(4000)
    DECLARE @sTmp2 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Assumes Chilkat is unlocked. See Global Unlock Sample.
    SELECT @success = 0
    --  Obtain a delegated Graph token with this example's file permissions.
    --  See OneDrive OAuth2 with PKCE.
    DECLARE @token int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @token OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @token, 'Strict', 1
    EXEC sp_OAMethod @token, 'LoadFile', @success OUT, 'qa_data/tokens/oneDrive.json'
    IF @success <> 1
      BEGIN

        PRINT 'Could not load the private OneDrive token cache.'
        EXEC @hr = sp_OADestroy @token
        RETURN
      END
    EXEC sp_OAMethod @token, 'JsonTypeOf', @iTmp0 OUT, 'access_token'
    EXEC sp_OAMethod @token, 'StringOfEquals', @iTmp1 OUT, 'access_token', '', 1
    IF (@iTmp0 <> 1) or (@iTmp1 = 1)
      BEGIN

        PRINT 'Expected a nonempty access_token in the JSON.'
        EXEC @hr = sp_OADestroy @token
        RETURN
      END
    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT

    EXEC sp_OAMethod @token, 'StringOf', @sTmp0 OUT, 'access_token'
    EXEC sp_OASetProperty @http, 'AuthToken', @sTmp0
    EXEC sp_OASetProperty @http, 'RequireSslCertVerify', 1
    EXEC sp_OASetProperty @http, 'FollowRedirects', 0
    EXEC sp_OASetProperty @http, 'ConnectTimeout', 20
    EXEC sp_OASetProperty @http, 'ReadTimeout', 120
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Accept', 'application/json'
    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    DECLARE @result int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @result OUT

    EXEC sp_OASetProperty @result, 'Strict', 1
    DECLARE @driveId nvarchar(4000)
    SELECT @driveId = 'YOUR_DRIVE_ID'
    DECLARE @itemId nvarchar(4000)
    SELECT @itemId = 'YOUR_ITEM_ID'
    DECLARE @endpoint int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @endpoint OUT

    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, 'https://graph.microsoft.com/v1.0/drives/'
    EXEC sp_OAMethod @http, 'UrlEncode', @sTmp0 OUT, @driveId
    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, @sTmp0
    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, '/items/'
    EXEC sp_OAMethod @http, 'UrlEncode', @sTmp0 OUT, @itemId
    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, @sTmp0
    --  Leave empty to list only. Set an explicit previous version ID to download it.
    --  A version ID such as 1.0 is a STRING, not a decimal number.
    DECLARE @versionId nvarchar(4000)
    SELECT @versionId = ''
    DECLARE @foundVersion int
    SELECT @foundVersion = 0
    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, '/versions'
    DECLARE @url nvarchar(4000)
    EXEC sp_OAMethod @endpoint, 'GetAsString', @url OUT
    DECLARE @nextLink int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @nextLink OUT

    DECLARE @seenLinks int
    EXEC @hr = sp_OACreate 'Chilkat.Hashtable', @seenLinks OUT

    DECLARE @pageCount int
    SELECT @pageCount = 0
    WHILE ((@url <> '') and (@pageCount < 100))
      BEGIN
        EXEC sp_OAMethod @nextLink, 'SetString', @success OUT, @url
        EXEC sp_OAMethod @nextLink, 'StartsWith', @iTmp0 OUT, 'https://graph.microsoft.com/v1.0/', 1
        IF @iTmp0 <> 1
          BEGIN

            PRINT 'Unexpected pagination host or API version; no request sent.'
            EXEC @hr = sp_OADestroy @token
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @nextLink
            EXEC @hr = sp_OADestroy @seenLinks
            RETURN
          END
        EXEC sp_OAMethod @seenLinks, 'Contains', @iTmp0 OUT, @url
        IF @iTmp0 = 1
          BEGIN

            PRINT 'Repeated pagination URL; stopping.'
            EXEC @hr = sp_OADestroy @token
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @nextLink
            EXEC @hr = sp_OADestroy @seenLinks
            RETURN
          END
        EXEC sp_OAMethod @seenLinks, 'AddInt', @success OUT, @url, 1
        EXEC sp_OAMethod @http, 'HttpNoBody', @success OUT, 'GET', @url, @resp
        EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
        IF (@success <> 1) and (@iTmp0 = 0)
          BEGIN

            PRINT 'No HTTP response received; inspect diagnostics privately and reconcile writes before retrying.'
            EXEC @hr = sp_OADestroy @token
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @nextLink
            EXEC @hr = sp_OADestroy @seenLinks
            RETURN
          END
        EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
        IF (@iTmp0 <> 200)
          BEGIN

            EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
            PRINT 'Unexpected HTTP status: ' + @iTmp0

            EXEC sp_OAMethod @resp, 'GetHeaderField', @sTmp0 OUT, 'request-id'
            PRINT 'Request ID: ' + @sTmp0
            EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
            IF @iTmp0 = 429
              BEGIN

                EXEC sp_OAMethod @resp, 'GetHeaderField', @sTmp0 OUT, 'Retry-After'
                PRINT 'Retry-After: ' + @sTmp0
              END
            EXEC @hr = sp_OADestroy @token
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @nextLink
            EXEC @hr = sp_OADestroy @seenLinks
            RETURN
          END
        EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
        EXEC sp_OAMethod @result, 'Load', @success OUT, @sTmp0
        IF @success <> 1
          BEGIN

            PRINT 'Expected a well-formed JSON response.'
            EXEC @hr = sp_OADestroy @token
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @nextLink
            EXEC @hr = sp_OADestroy @seenLinks
            RETURN
          END
        EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'value'
        IF @iTmp0 <> 4
          BEGIN

            PRINT 'Expected a value array.'
            EXEC @hr = sp_OADestroy @token
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @nextLink
            EXEC @hr = sp_OADestroy @seenLinks
            RETURN
          END
        DECLARE @i int
        SELECT @i = 0
        EXEC sp_OAMethod @result, 'SizeOfArray', @iTmp0 OUT, 'value'
        WHILE (@i < @iTmp0)
          BEGIN
            EXEC sp_OASetProperty @result, 'I', @i
            EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'value[i].id'
            EXEC sp_OAMethod @result, 'StringOfEquals', @iTmp1 OUT, 'value[i].id', '', 1
            IF (@iTmp0 <> 1) or (@iTmp1 = 1)
              BEGIN

                PRINT 'Expected a nonempty value[i].id in the JSON.'
                EXEC @hr = sp_OADestroy @token
                EXEC @hr = sp_OADestroy @http
                EXEC @hr = sp_OADestroy @resp
                EXEC @hr = sp_OADestroy @result
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @nextLink
                EXEC @hr = sp_OADestroy @seenLinks
                RETURN
              END

            EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'value[i].id'
            PRINT 'Version ID: ' + @sTmp0

            EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'value[i].lastModifiedDateTime'
            PRINT 'Modified: ' + @sTmp0

            EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'value[i].size'
            PRINT 'Size in bytes: ' + @sTmp0
            EXEC sp_OAMethod @result, 'StringOfEquals', @iTmp0 OUT, 'value[i].id', @versionId, 1
            IF @iTmp0 = 1
              BEGIN
                SELECT @foundVersion = 1
              END
            SELECT @i = @i + 1
          END
        SELECT @pageCount = @pageCount + 1
        --  A quoted JSON path treats the period in @odata.nextLink literally.
        DECLARE @nextType int
        EXEC sp_OAMethod @result, 'JsonTypeOf', @nextType OUT, '"@odata.nextLink"'
        IF (@nextType = -1) or (@nextType = 6)
          BEGIN
            SELECT @url = ''
          END
        ELSE
          BEGIN
            EXEC sp_OAMethod @result, 'StringOfEquals', @iTmp0 OUT, '"@odata.nextLink"', '', 1
            IF (@nextType <> 1) or (@iTmp0 = 1)
              BEGIN

                PRINT 'Invalid continuation link; cannot establish completeness.'
                EXEC @hr = sp_OADestroy @token
                EXEC @hr = sp_OADestroy @http
                EXEC @hr = sp_OADestroy @resp
                EXEC @hr = sp_OADestroy @result
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @nextLink
                EXEC @hr = sp_OADestroy @seenLinks
                RETURN
              END
            EXEC sp_OAMethod @result, 'StringOf', @url OUT, '"@odata.nextLink"'
          END
      END
    IF @url <> ''
      BEGIN

        PRINT 'Stopped at the example''s 100-page limit; save the continuation privately to resume.'
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @nextLink
        EXEC @hr = sp_OADestroy @seenLinks
        RETURN
      END

    PRINT 'Completed pages: ' + @pageCount
    IF @versionId = ''
      BEGIN

        PRINT 'Listing only. Select a previous version ID to download.'
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @nextLink
        EXEC @hr = sp_OADestroy @seenLinks
        RETURN
      END
    IF @foundVersion <> 1
      BEGIN

        PRINT 'The selected version was not in the completed listing; nothing downloaded.'
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @nextLink
        EXEC @hr = sp_OADestroy @seenLinks
        RETURN
      END
    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, '/'
    EXEC sp_OAMethod @http, 'UrlEncode', @sTmp0 OUT, @versionId
    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, @sTmp0
    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, '/content'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Accept', '*/*'
    EXEC sp_OAMethod @endpoint, 'GetAsString', @sTmp0 OUT
    EXEC sp_OAMethod @http, 'HttpNoBody', @success OUT, 'GET', @sTmp0, @resp
    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    IF (@success <> 1) and (@iTmp0 = 0)
      BEGIN

        PRINT 'No HTTP response received; inspect diagnostics privately and reconcile writes before retrying.'
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @nextLink
        EXEC @hr = sp_OADestroy @seenLinks
        RETURN
      END
    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp1 OUT
    IF (@iTmp0 <> 200) and (@iTmp1 <> 302)
      BEGIN

        EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
        PRINT 'Unexpected HTTP status: ' + @iTmp0

        EXEC sp_OAMethod @resp, 'GetHeaderField', @sTmp0 OUT, 'request-id'
        PRINT 'Request ID: ' + @sTmp0
        EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
        IF @iTmp0 = 429
          BEGIN

            EXEC sp_OAMethod @resp, 'GetHeaderField', @sTmp0 OUT, 'Retry-After'
            PRINT 'Retry-After: ' + @sTmp0
          END
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @nextLink
        EXEC @hr = sp_OADestroy @seenLinks
        RETURN
      END
    DECLARE @contents int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @contents OUT

    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    IF @iTmp0 = 200
      BEGIN
        EXEC sp_OAMethod @resp, 'GetBodyBd', @success OUT, @contents
        IF @success <> 1
          BEGIN

            PRINT 'Could not read the version bytes.'
            EXEC @hr = sp_OADestroy @token
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @nextLink
            EXEC @hr = sp_OADestroy @seenLinks
            EXEC @hr = sp_OADestroy @contents
            RETURN
          END
      END
    ELSE
      BEGIN
        DECLARE @downloadUrl nvarchar(4000)
        EXEC sp_OAMethod @resp, 'GetHeaderField', @downloadUrl OUT, 'Location'
        DECLARE @serviceUrl int
        EXEC @hr = sp_OACreate 'Chilkat.Url', @serviceUrl OUT

        EXEC sp_OAMethod @serviceUrl, 'ParseUrl', @success OUT, @downloadUrl
        IF @success <> 1
          BEGIN

            PRINT 'Invalid service URL.'
            EXEC @hr = sp_OADestroy @token
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @nextLink
            EXEC @hr = sp_OADestroy @seenLinks
            EXEC @hr = sp_OADestroy @contents
            EXEC @hr = sp_OADestroy @serviceUrl
            RETURN
          END
        EXEC sp_OAGetProperty @serviceUrl, 'Ssl', @iTmp0 OUT
        EXEC sp_OAGetProperty @serviceUrl, 'Host', @sTmp0 OUT
        EXEC sp_OAGetProperty @serviceUrl, 'Login', @sTmp1 OUT
        EXEC sp_OAGetProperty @serviceUrl, 'Password', @sTmp2 OUT
        IF (@iTmp0 <> 1) or (@sTmp0 = '') or (@sTmp1 <> '') or (@sTmp2 <> '')
          BEGIN

            PRINT 'Expected an HTTPS service URL without embedded login credentials.'
            EXEC @hr = sp_OADestroy @token
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @nextLink
            EXEC @hr = sp_OADestroy @seenLinks
            EXEC @hr = sp_OADestroy @contents
            EXEC @hr = sp_OADestroy @serviceUrl
            RETURN
          END
        DECLARE @downloadHttp int
        EXEC @hr = sp_OACreate 'Chilkat.Http', @downloadHttp OUT

        EXEC sp_OASetProperty @downloadHttp, 'RequireSslCertVerify', 1
        EXEC sp_OASetProperty @downloadHttp, 'FollowRedirects', 0
        EXEC sp_OASetProperty @downloadHttp, 'ConnectTimeout', 20
        EXEC sp_OASetProperty @downloadHttp, 'ReadTimeout', 120
        DECLARE @serviceHost int
        EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @serviceHost OUT

        EXEC sp_OAGetProperty @serviceUrl, 'Host', @sTmp0 OUT
        EXEC sp_OAMethod @serviceHost, 'Append', @success OUT, @sTmp0
        EXEC sp_OAMethod @serviceHost, 'ContentsEqual', @iTmp0 OUT, 'graph.microsoft.com', 0
        EXEC sp_OAGetProperty @serviceUrl, 'Port', @iTmp1 OUT
        IF (@iTmp0 = 1) and (@iTmp1 = 443)
          BEGIN
            EXEC sp_OAMethod @token, 'StringOf', @sTmp0 OUT, 'access_token'
            EXEC sp_OASetProperty @downloadHttp, 'AuthToken', @sTmp0
          END
        ELSE
          BEGIN
            DECLARE @tokenFiles int
            EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @tokenFiles OUT

            EXEC sp_OAMethod @tokenFiles, 'FileExists', @iTmp0 OUT, 'qa_data/tokens/oneDrive-sharepoint.json'
            IF @iTmp0 = 1
              BEGIN
                DECLARE @sharepointToken int
                EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @sharepointToken OUT

                EXEC sp_OASetProperty @sharepointToken, 'Strict', 1
                EXEC sp_OAMethod @sharepointToken, 'LoadFile', @success OUT, 'qa_data/tokens/oneDrive-sharepoint.json'
                IF @success <> 1
                  BEGIN

                    PRINT 'Could not read the private SharePoint token cache.'
                    EXEC @hr = sp_OADestroy @token
                    EXEC @hr = sp_OADestroy @http
                    EXEC @hr = sp_OADestroy @resp
                    EXEC @hr = sp_OADestroy @result
                    EXEC @hr = sp_OADestroy @endpoint
                    EXEC @hr = sp_OADestroy @nextLink
                    EXEC @hr = sp_OADestroy @seenLinks
                    EXEC @hr = sp_OADestroy @contents
                    EXEC @hr = sp_OADestroy @serviceUrl
                    EXEC @hr = sp_OADestroy @downloadHttp
                    EXEC @hr = sp_OADestroy @serviceHost
                    EXEC @hr = sp_OADestroy @tokenFiles
                    EXEC @hr = sp_OADestroy @sharepointToken
                    RETURN
                  END
                EXEC sp_OAMethod @sharepointToken, 'JsonTypeOf', @iTmp0 OUT, 'host'
                EXEC sp_OAMethod @sharepointToken, 'StringOfEquals', @iTmp1 OUT, 'host', '', 1
                IF (@iTmp0 <> 1) or (@iTmp1 = 1)
                  BEGIN

                    PRINT 'Expected a nonempty host in the JSON.'
                    EXEC @hr = sp_OADestroy @token
                    EXEC @hr = sp_OADestroy @http
                    EXEC @hr = sp_OADestroy @resp
                    EXEC @hr = sp_OADestroy @result
                    EXEC @hr = sp_OADestroy @endpoint
                    EXEC @hr = sp_OADestroy @nextLink
                    EXEC @hr = sp_OADestroy @seenLinks
                    EXEC @hr = sp_OADestroy @contents
                    EXEC @hr = sp_OADestroy @serviceUrl
                    EXEC @hr = sp_OADestroy @downloadHttp
                    EXEC @hr = sp_OADestroy @serviceHost
                    EXEC @hr = sp_OADestroy @tokenFiles
                    EXEC @hr = sp_OADestroy @sharepointToken
                    RETURN
                  END
                EXEC sp_OAMethod @sharepointToken, 'JsonTypeOf', @iTmp0 OUT, 'access_token'
                EXEC sp_OAMethod @sharepointToken, 'StringOfEquals', @iTmp1 OUT, 'access_token', '', 1
                IF (@iTmp0 <> 1) or (@iTmp1 = 1)
                  BEGIN

                    PRINT 'Expected a nonempty access_token in the JSON.'
                    EXEC @hr = sp_OADestroy @token
                    EXEC @hr = sp_OADestroy @http
                    EXEC @hr = sp_OADestroy @resp
                    EXEC @hr = sp_OADestroy @result
                    EXEC @hr = sp_OADestroy @endpoint
                    EXEC @hr = sp_OADestroy @nextLink
                    EXEC @hr = sp_OADestroy @seenLinks
                    EXEC @hr = sp_OADestroy @contents
                    EXEC @hr = sp_OADestroy @serviceUrl
                    EXEC @hr = sp_OADestroy @downloadHttp
                    EXEC @hr = sp_OADestroy @serviceHost
                    EXEC @hr = sp_OADestroy @tokenFiles
                    EXEC @hr = sp_OADestroy @sharepointToken
                    RETURN
                  END
                --  host is a configured exact hostname, not a URL and not obtained from the service response.
                EXEC sp_OAMethod @sharepointToken, 'StringOf', @sTmp0 OUT, 'host'
                EXEC sp_OAMethod @serviceHost, 'ContentsEqual', @iTmp0 OUT, @sTmp0, 0
                EXEC sp_OAGetProperty @serviceUrl, 'Port', @iTmp1 OUT
                IF (@iTmp0 = 1) and (@iTmp1 = 443)
                  BEGIN
                    EXEC sp_OAMethod @sharepointToken, 'StringOf', @sTmp0 OUT, 'access_token'
                    EXEC sp_OASetProperty @downloadHttp, 'AuthToken', @sTmp0
                  END
              END
          END
        --  With no matching token configured, only a still-valid preauthenticated URL can succeed.
        DECLARE @downloadResponse int
        EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @downloadResponse OUT

        EXEC sp_OAMethod @downloadHttp, 'HttpNoBody', @success OUT, 'GET', @downloadUrl, @downloadResponse
        EXEC sp_OAGetProperty @downloadResponse, 'StatusCode', @iTmp0 OUT
        IF (@success <> 1) and (@iTmp0 = 0)
          BEGIN

            PRINT 'No HTTP response received; inspect diagnostics privately and reconcile writes before retrying.'
            EXEC @hr = sp_OADestroy @token
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @nextLink
            EXEC @hr = sp_OADestroy @seenLinks
            EXEC @hr = sp_OADestroy @contents
            EXEC @hr = sp_OADestroy @serviceUrl
            EXEC @hr = sp_OADestroy @downloadHttp
            EXEC @hr = sp_OADestroy @serviceHost
            EXEC @hr = sp_OADestroy @tokenFiles
            EXEC @hr = sp_OADestroy @sharepointToken
            EXEC @hr = sp_OADestroy @downloadResponse
            RETURN
          END
        EXEC sp_OAGetProperty @downloadResponse, 'StatusCode', @iTmp0 OUT
        IF (@iTmp0 <> 200)
          BEGIN

            EXEC sp_OAGetProperty @downloadResponse, 'StatusCode', @iTmp0 OUT
            PRINT 'Unexpected HTTP status: ' + @iTmp0

            EXEC sp_OAMethod @downloadResponse, 'GetHeaderField', @sTmp0 OUT, 'request-id'
            PRINT 'Request ID: ' + @sTmp0
            EXEC sp_OAGetProperty @downloadResponse, 'StatusCode', @iTmp0 OUT
            IF @iTmp0 = 429
              BEGIN

                EXEC sp_OAMethod @downloadResponse, 'GetHeaderField', @sTmp0 OUT, 'Retry-After'
                PRINT 'Retry-After: ' + @sTmp0
              END
            EXEC @hr = sp_OADestroy @token
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @nextLink
            EXEC @hr = sp_OADestroy @seenLinks
            EXEC @hr = sp_OADestroy @contents
            EXEC @hr = sp_OADestroy @serviceUrl
            EXEC @hr = sp_OADestroy @downloadHttp
            EXEC @hr = sp_OADestroy @serviceHost
            EXEC @hr = sp_OADestroy @tokenFiles
            EXEC @hr = sp_OADestroy @sharepointToken
            EXEC @hr = sp_OADestroy @downloadResponse
            RETURN
          END
        EXEC sp_OAMethod @downloadResponse, 'GetBodyBd', @success OUT, @contents
        IF @success <> 1
          BEGIN

            PRINT 'Could not read the version download.'
            EXEC @hr = sp_OADestroy @token
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @nextLink
            EXEC @hr = sp_OADestroy @seenLinks
            EXEC @hr = sp_OADestroy @contents
            EXEC @hr = sp_OADestroy @serviceUrl
            EXEC @hr = sp_OADestroy @downloadHttp
            EXEC @hr = sp_OADestroy @serviceHost
            EXEC @hr = sp_OADestroy @tokenFiles
            EXEC @hr = sp_OADestroy @sharepointToken
            EXEC @hr = sp_OADestroy @downloadResponse
            RETURN
          END
      END
    --  Hold binary bytes without text conversion. This example expects a version that fits in memory.
    DECLARE @fac int
    EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @fac OUT

    EXEC sp_OAMethod @fac, 'DirEnsureExists', @success OUT, 'qa_output/oneDrive'
    IF @success <> 1
      BEGIN

        PRINT 'Could not create the output directory.'
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @nextLink
        EXEC @hr = sp_OADestroy @seenLinks
        EXEC @hr = sp_OADestroy @contents
        EXEC @hr = sp_OADestroy @serviceUrl
        EXEC @hr = sp_OADestroy @downloadHttp
        EXEC @hr = sp_OADestroy @serviceHost
        EXEC @hr = sp_OADestroy @tokenFiles
        EXEC @hr = sp_OADestroy @sharepointToken
        EXEC @hr = sp_OADestroy @downloadResponse
        EXEC @hr = sp_OADestroy @fac
        RETURN
      END
    DECLARE @uuid int
    EXEC @hr = sp_OACreate 'Chilkat.Crypt2', @uuid OUT

    DECLARE @localId nvarchar(4000)
    EXEC sp_OAMethod @uuid, 'GenerateUuid', @localId OUT
    EXEC sp_OAGetProperty @uuid, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 <> 1
      BEGIN

        PRINT 'Could not generate a local filename.'
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @nextLink
        EXEC @hr = sp_OADestroy @seenLinks
        EXEC @hr = sp_OADestroy @contents
        EXEC @hr = sp_OADestroy @serviceUrl
        EXEC @hr = sp_OADestroy @downloadHttp
        EXEC @hr = sp_OADestroy @serviceHost
        EXEC @hr = sp_OADestroy @tokenFiles
        EXEC @hr = sp_OADestroy @sharepointToken
        EXEC @hr = sp_OADestroy @downloadResponse
        EXEC @hr = sp_OADestroy @fac
        EXEC @hr = sp_OADestroy @uuid
        RETURN
      END
    DECLARE @target int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @target OUT

    EXEC sp_OAMethod @target, 'Append', @success OUT, 'qa_output/oneDrive/'
    EXEC sp_OAMethod @target, 'Append', @success OUT, @localId
    EXEC sp_OAMethod @target, 'Append', @success OUT, '.bin'
    EXEC sp_OAMethod @target, 'GetAsString', @sTmp0 OUT
    EXEC sp_OAMethod @fac, 'FileExists', @iTmp0 OUT, @sTmp0
    IF @iTmp0 = 1
      BEGIN

        PRINT 'The output file already exists; nothing overwritten.'
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @nextLink
        EXEC @hr = sp_OADestroy @seenLinks
        EXEC @hr = sp_OADestroy @contents
        EXEC @hr = sp_OADestroy @serviceUrl
        EXEC @hr = sp_OADestroy @downloadHttp
        EXEC @hr = sp_OADestroy @serviceHost
        EXEC @hr = sp_OADestroy @tokenFiles
        EXEC @hr = sp_OADestroy @sharepointToken
        EXEC @hr = sp_OADestroy @downloadResponse
        EXEC @hr = sp_OADestroy @fac
        EXEC @hr = sp_OADestroy @uuid
        EXEC @hr = sp_OADestroy @target
        RETURN
      END
    EXEC sp_OAMethod @target, 'GetAsString', @sTmp0 OUT
    EXEC sp_OAMethod @contents, 'WriteFile', @success OUT, @sTmp0
    IF @success <> 1
      BEGIN

        PRINT 'Could not write the downloaded version.'
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @nextLink
        EXEC @hr = sp_OADestroy @seenLinks
        EXEC @hr = sp_OADestroy @contents
        EXEC @hr = sp_OADestroy @serviceUrl
        EXEC @hr = sp_OADestroy @downloadHttp
        EXEC @hr = sp_OADestroy @serviceHost
        EXEC @hr = sp_OADestroy @tokenFiles
        EXEC @hr = sp_OADestroy @sharepointToken
        EXEC @hr = sp_OADestroy @downloadResponse
        EXEC @hr = sp_OADestroy @fac
        EXEC @hr = sp_OADestroy @uuid
        EXEC @hr = sp_OADestroy @target
        RETURN
      END

    EXEC sp_OAMethod @target, 'GetAsString', @sTmp0 OUT
    PRINT 'Saved previous version: ' + @sTmp0

    EXEC @hr = sp_OADestroy @token
    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @resp
    EXEC @hr = sp_OADestroy @result
    EXEC @hr = sp_OADestroy @endpoint
    EXEC @hr = sp_OADestroy @nextLink
    EXEC @hr = sp_OADestroy @seenLinks
    EXEC @hr = sp_OADestroy @contents
    EXEC @hr = sp_OADestroy @serviceUrl
    EXEC @hr = sp_OADestroy @downloadHttp
    EXEC @hr = sp_OADestroy @serviceHost
    EXEC @hr = sp_OADestroy @tokenFiles
    EXEC @hr = sp_OADestroy @sharepointToken
    EXEC @hr = sp_OADestroy @downloadResponse
    EXEC @hr = sp_OADestroy @fac
    EXEC @hr = sp_OADestroy @uuid
    EXEC @hr = sp_OADestroy @target


END
GO