OneDrive: Access Microsoft 365 OneDrive with a Certificate
See more OneDrive Examples
Acquire an application token using an RSA certificate and list a selected organizational user's drives. This is for OneDrive for Business and SharePoint, not personal Microsoft accounts. Configure Graph application Files.Read.All and administrator consent for this listing. Provision the user's OneDrive separately; consent does not create it. Use an appropriate restricted-access design when broad application access is unnecessary.
Private onedrive-certificate-app.json contains tenant_id, client_id, user_id (object ID or UPN), pfx_path, and pfx_password. Upload the public certificate to the Entra application and protect the RSA private key in an environment you control. Use a tenant-specific authority. Never distribute this private key in a public application.
The five-minute PS256 assertion includes x5t#S256, the SHA-256 certificate thumbprint. It is sent only to the token endpoint. The resulting Graph token is sent to Graph. .default selects configured application permissions; no refresh token is issued. Acquire a new token with a fresh assertion before expiry.
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.
Permission: Application Files.Read.All with administrator consent.
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.
Chilkat SQL Server Downloads
-- 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 @success int
SELECT @success = 0
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
SELECT @success = 0
DECLARE @config int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @config OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OASetProperty @config, 'Strict', 1
EXEC sp_OAMethod @config, 'LoadFile', @success OUT, 'onedrive-certificate-app.json'
IF @success <> 1
BEGIN
PRINT 'Could not load private app configuration.'
EXEC @hr = sp_OADestroy @config
RETURN
END
EXEC sp_OAMethod @config, 'JsonTypeOf', @iTmp0 OUT, 'tenant_id'
EXEC sp_OAMethod @config, 'StringOfEquals', @iTmp1 OUT, 'tenant_id', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
PRINT 'Expected a nonempty tenant_id in the JSON.'
EXEC @hr = sp_OADestroy @config
RETURN
END
EXEC sp_OAMethod @config, 'JsonTypeOf', @iTmp0 OUT, 'client_id'
EXEC sp_OAMethod @config, 'StringOfEquals', @iTmp1 OUT, 'client_id', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
PRINT 'Expected a nonempty client_id in the JSON.'
EXEC @hr = sp_OADestroy @config
RETURN
END
EXEC sp_OAMethod @config, 'JsonTypeOf', @iTmp0 OUT, 'user_id'
EXEC sp_OAMethod @config, 'StringOfEquals', @iTmp1 OUT, 'user_id', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
PRINT 'Expected a nonempty user_id in the JSON.'
EXEC @hr = sp_OADestroy @config
RETURN
END
EXEC sp_OAMethod @config, 'JsonTypeOf', @iTmp0 OUT, 'pfx_path'
EXEC sp_OAMethod @config, 'StringOfEquals', @iTmp1 OUT, 'pfx_path', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
PRINT 'Expected a nonempty pfx_path in the JSON.'
EXEC @hr = sp_OADestroy @config
RETURN
END
-- pfx_password may be empty for an unencrypted test PFX; protect the private key in production.
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
EXEC sp_OASetProperty @http, 'RequireSslCertVerify', 1
EXEC sp_OASetProperty @http, 'FollowRedirects', 0
EXEC sp_OASetProperty @http, 'ConnectTimeout', 20
EXEC sp_OASetProperty @http, 'ReadTimeout', 30
DECLARE @tokenEndpoint int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @tokenEndpoint OUT
EXEC sp_OAMethod @tokenEndpoint, 'Append', @success OUT, 'https://login.microsoftonline.com/'
EXEC sp_OAMethod @config, 'StringOf', @sTmp1 OUT, 'tenant_id'
EXEC sp_OAMethod @http, 'UrlEncode', @sTmp0 OUT, @sTmp1
EXEC sp_OAMethod @tokenEndpoint, 'Append', @success OUT, @sTmp0
EXEC sp_OAMethod @tokenEndpoint, 'Append', @success OUT, '/oauth2/v2.0/token'
DECLARE @tokenUrl nvarchar(4000)
EXEC sp_OAMethod @tokenEndpoint, 'GetAsString', @tokenUrl OUT
DECLARE @cert int
EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT
EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'pfx_path'
EXEC sp_OAMethod @config, 'StringOf', @sTmp1 OUT, 'pfx_password'
EXEC sp_OAMethod @cert, 'LoadPfxFile', @success OUT, @sTmp0, @sTmp1
IF @success <> 1
BEGIN
PRINT 'Could not load the application certificate.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @tokenEndpoint
EXEC @hr = sp_OADestroy @cert
RETURN
END
DECLARE @key int
EXEC @hr = sp_OACreate 'Chilkat.PrivateKey', @key OUT
EXEC sp_OAMethod @cert, 'GetPrivateKey', @success OUT, @key
IF @success <> 1
BEGIN
PRINT 'The certificate needs an accessible RSA private key.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @tokenEndpoint
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @key
RETURN
END
DECLARE @der int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @der OUT
EXEC sp_OAMethod @cert, 'ExportCertDerBd', @success OUT, @der
IF @success <> 1
BEGIN
PRINT 'Could not read the certificate DER bytes.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @tokenEndpoint
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @key
EXEC @hr = sp_OADestroy @der
RETURN
END
DECLARE @crypt int
EXEC @hr = sp_OACreate 'Chilkat.Crypt2', @crypt OUT
EXEC sp_OASetProperty @crypt, 'HashAlgorithm', 'sha256'
EXEC sp_OASetProperty @crypt, 'EncodingMode', 'base64url'
DECLARE @thumbprint nvarchar(4000)
EXEC sp_OAMethod @crypt, 'HashBdENC', @thumbprint OUT, @der
EXEC sp_OAGetProperty @crypt, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 <> 1
BEGIN
PRINT 'Could not hash the certificate.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @tokenEndpoint
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @key
EXEC @hr = sp_OADestroy @der
EXEC @hr = sp_OADestroy @crypt
RETURN
END
DECLARE @header int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @header OUT
EXEC sp_OAMethod @header, 'UpdateString', @success OUT, 'alg', 'PS256'
EXEC sp_OAMethod @header, 'UpdateString', @success OUT, 'typ', 'JWT'
EXEC sp_OAMethod @header, 'UpdateString', @success OUT, 'x5t#S256', @thumbprint
DECLARE @claims int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @claims OUT
EXEC sp_OAMethod @claims, 'UpdateString', @success OUT, 'aud', @tokenUrl
EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'client_id'
EXEC sp_OAMethod @claims, 'UpdateString', @success OUT, 'iss', @sTmp0
EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'client_id'
EXEC sp_OAMethod @claims, 'UpdateString', @success OUT, 'sub', @sTmp0
DECLARE @assertionId nvarchar(4000)
EXEC sp_OAMethod @crypt, 'GenerateUuid', @assertionId OUT
EXEC sp_OAGetProperty @crypt, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 <> 1
BEGIN
PRINT 'Could not create the assertion ID.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @tokenEndpoint
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @key
EXEC @hr = sp_OADestroy @der
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @header
EXEC @hr = sp_OADestroy @claims
RETURN
END
EXEC sp_OAMethod @claims, 'UpdateString', @success OUT, 'jti', @assertionId
DECLARE @clock int
EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @clock OUT
EXEC sp_OAMethod @clock, 'SetFromCurrentSystemTime', @success OUT
IF @success <> 1
BEGIN
PRINT 'Could not read the clock.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @tokenEndpoint
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @key
EXEC @hr = sp_OADestroy @der
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @header
EXEC @hr = sp_OADestroy @claims
EXEC @hr = sp_OADestroy @clock
RETURN
END
-- Numeric dates are JSON numbers built from decimal strings, without converting timestamps to a platform-sized integer.
EXEC sp_OAMethod @clock, 'GetAsUnixTimeStr', @sTmp0 OUT, 0
EXEC sp_OAMethod @claims, 'UpdateNumber', @success OUT, 'iat', @sTmp0
EXEC sp_OAMethod @clock, 'GetAsUnixTimeStr', @sTmp0 OUT, 0
EXEC sp_OAMethod @claims, 'UpdateNumber', @success OUT, 'nbf', @sTmp0
EXEC sp_OAMethod @clock, 'AddSeconds', @success OUT, 300
IF @success <> 1
BEGIN
PRINT 'Could not set assertion expiry.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @tokenEndpoint
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @key
EXEC @hr = sp_OADestroy @der
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @header
EXEC @hr = sp_OADestroy @claims
EXEC @hr = sp_OADestroy @clock
RETURN
END
EXEC sp_OAMethod @clock, 'GetAsUnixTimeStr', @sTmp0 OUT, 0
EXEC sp_OAMethod @claims, 'UpdateNumber', @success OUT, 'exp', @sTmp0
DECLARE @jwt int
EXEC @hr = sp_OACreate 'Chilkat.Jwt', @jwt OUT
DECLARE @assertion nvarchar(4000)
EXEC sp_OAMethod @header, 'Emit', @sTmp0 OUT
EXEC sp_OAMethod @claims, 'Emit', @sTmp1 OUT
EXEC sp_OAMethod @jwt, 'CreateJwtPk', @assertion OUT, @sTmp0, @sTmp1, @key
EXEC sp_OAGetProperty @jwt, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 <> 1
BEGIN
PRINT 'Could not sign the client assertion.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @tokenEndpoint
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @key
EXEC @hr = sp_OADestroy @der
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @header
EXEC @hr = sp_OADestroy @claims
EXEC @hr = sp_OADestroy @clock
EXEC @hr = sp_OADestroy @jwt
RETURN
END
DECLARE @req int
EXEC @hr = sp_OACreate 'Chilkat.HttpRequest', @req OUT
EXEC sp_OASetProperty @req, 'HttpVerb', 'POST'
EXEC sp_OASetProperty @req, 'ContentType', 'application/x-www-form-urlencoded'
EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'client_id'
EXEC sp_OAMethod @req, 'AddParam', NULL, 'client_id', @sTmp0
EXEC sp_OAMethod @req, 'AddParam', NULL, 'grant_type', 'client_credentials'
EXEC sp_OAMethod @req, 'AddParam', NULL, 'scope', 'https://graph.microsoft.com/.default'
EXEC sp_OAMethod @req, 'AddParam', NULL, 'client_assertion_type', 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer'
EXEC sp_OAMethod @req, 'AddParam', NULL, 'client_assertion', @assertion
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
EXEC sp_OAMethod @http, 'HttpReq', @success OUT, @tokenUrl, @req, @resp
-- A false result can still include an HTTP error response.
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF (@success <> 1) and (@iTmp0 = 0)
BEGIN
PRINT 'No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @tokenEndpoint
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @key
EXEC @hr = sp_OADestroy @der
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @header
EXEC @hr = sp_OADestroy @claims
EXEC @hr = sp_OADestroy @clock
EXEC @hr = sp_OADestroy @jwt
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
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 sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF @iTmp0 = 401
BEGIN
PRINT 'Refresh the Graph access token or authorize again.'
END
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @tokenEndpoint
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @key
EXEC @hr = sp_OADestroy @der
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @header
EXEC @hr = sp_OADestroy @claims
EXEC @hr = sp_OADestroy @clock
EXEC @hr = sp_OADestroy @jwt
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
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 @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @tokenEndpoint
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @key
EXEC @hr = sp_OADestroy @der
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @header
EXEC @hr = sp_OADestroy @claims
EXEC @hr = sp_OADestroy @clock
EXEC @hr = sp_OADestroy @jwt
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
RETURN
END
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'access_token'
EXEC sp_OAMethod @result, '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 @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @tokenEndpoint
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @key
EXEC @hr = sp_OADestroy @der
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @header
EXEC @hr = sp_OADestroy @claims
EXEC @hr = sp_OADestroy @clock
EXEC @hr = sp_OADestroy @jwt
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
RETURN
END
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'access_token'
EXEC sp_OASetProperty @http, 'AuthToken', @sTmp0
-- Application access addresses a specific organizational user; /me is not available.
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Accept', 'application/json'
DECLARE @endpoint int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @endpoint OUT
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, 'https://graph.microsoft.com/v1.0/users/'
EXEC sp_OAMethod @config, 'StringOf', @sTmp1 OUT, 'user_id'
EXEC sp_OAMethod @http, 'UrlEncode', @sTmp0 OUT, @sTmp1
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, @sTmp0
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, '/drives?$select=id,name,driveType,quota&$top=50'
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 @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @tokenEndpoint
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @key
EXEC @hr = sp_OADestroy @der
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @header
EXEC @hr = sp_OADestroy @claims
EXEC @hr = sp_OADestroy @clock
EXEC @hr = sp_OADestroy @jwt
EXEC @hr = sp_OADestroy @req
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 @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @tokenEndpoint
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @key
EXEC @hr = sp_OADestroy @der
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @header
EXEC @hr = sp_OADestroy @claims
EXEC @hr = sp_OADestroy @clock
EXEC @hr = sp_OADestroy @jwt
EXEC @hr = sp_OADestroy @req
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 @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @tokenEndpoint
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @key
EXEC @hr = sp_OADestroy @der
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @header
EXEC @hr = sp_OADestroy @claims
EXEC @hr = sp_OADestroy @clock
EXEC @hr = sp_OADestroy @jwt
EXEC @hr = sp_OADestroy @req
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 @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @tokenEndpoint
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @key
EXEC @hr = sp_OADestroy @der
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @header
EXEC @hr = sp_OADestroy @claims
EXEC @hr = sp_OADestroy @clock
EXEC @hr = sp_OADestroy @jwt
EXEC @hr = sp_OADestroy @req
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 @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @tokenEndpoint
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @key
EXEC @hr = sp_OADestroy @der
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @header
EXEC @hr = sp_OADestroy @claims
EXEC @hr = sp_OADestroy @clock
EXEC @hr = sp_OADestroy @jwt
EXEC @hr = sp_OADestroy @req
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 @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @tokenEndpoint
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @key
EXEC @hr = sp_OADestroy @der
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @header
EXEC @hr = sp_OADestroy @claims
EXEC @hr = sp_OADestroy @clock
EXEC @hr = sp_OADestroy @jwt
EXEC @hr = sp_OADestroy @req
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 @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @tokenEndpoint
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @key
EXEC @hr = sp_OADestroy @der
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @header
EXEC @hr = sp_OADestroy @claims
EXEC @hr = sp_OADestroy @clock
EXEC @hr = sp_OADestroy @jwt
EXEC @hr = sp_OADestroy @req
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 'Drive ID: ' + @sTmp0
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'value[i].name'
PRINT 'Name: ' + @sTmp0
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'value[i].driveType'
PRINT 'Drive type: ' + @sTmp0
-- Quota values can be larger than int. Missing quota fields are not zero.
EXEC sp_OAMethod @result, 'HasMember', @iTmp0 OUT, 'value[i].quota.total'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'value[i].quota.total'
PRINT 'Total bytes: ' + @sTmp0
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'value[i].quota.remaining'
PRINT 'Remaining bytes: ' + @sTmp0
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 @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @tokenEndpoint
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @key
EXEC @hr = sp_OADestroy @der
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @header
EXEC @hr = sp_OADestroy @claims
EXEC @hr = sp_OADestroy @clock
EXEC @hr = sp_OADestroy @jwt
EXEC @hr = sp_OADestroy @req
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 @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @tokenEndpoint
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @key
EXEC @hr = sp_OADestroy @der
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @header
EXEC @hr = sp_OADestroy @claims
EXEC @hr = sp_OADestroy @clock
EXEC @hr = sp_OADestroy @jwt
EXEC @hr = sp_OADestroy @req
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
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @tokenEndpoint
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @key
EXEC @hr = sp_OADestroy @der
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @header
EXEC @hr = sp_OADestroy @claims
EXEC @hr = sp_OADestroy @clock
EXEC @hr = sp_OADestroy @jwt
EXEC @hr = sp_OADestroy @req
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
END
GO