OneDrive: Use Application Credentials for Microsoft 365
See more OneDrive Examples
Retains Chilkat.Http's automatic OAuth2 client-credentials support: AuthToken can contain the credential JSON, and Http obtains and caches tokens internally. Load tenant_id, client_id, client_secret, and user_id from private onedrive-app.json. Keep this file outside public web content and source control; use protected configuration in production. Reuse the Http object to benefit from its token cache.
This is for a controlled application accessing Microsoft 365 OneDrive for Business or SharePoint. It is not personal OneDrive authentication and must not embed an application secret in software distributed to the public. Grant application Files.Read.All and administrator consent for this listing request, or use an appropriate supported restricted-access design. A consented permission does not provision a user's OneDrive. Use a tenant-specific authority and explicitly address the user, site, or drive; never /me. App-only tokens have no refresh token.
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 for this request.
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 Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loConfig
LOCAL loHttp
LOCAL loCredentials
LOCAL loTokenEndpoint
LOCAL loResp
LOCAL loResult
LOCAL loEndpoint
LOCAL lcUrl
LOCAL loNextLink
LOCAL loSeenLinks
LOCAL lnPageCount
LOCAL i
LOCAL lnNextType
lnSuccess = 0
* Assumes Chilkat is unlocked. See Global Unlock Sample.
lnSuccess = 0
* Private file: tenant_id, client_id, client_secret, and user_id (object ID or UPN).
* The secret VALUE is required, not its secret identifier.
loConfig = CreateObject('Chilkat.JsonObject')
loConfig.Strict = 1
lnSuccess = loConfig.LoadFile("onedrive-app.json")
IF (lnSuccess <> 1) THEN
? "Could not load the private application configuration."
RELEASE loConfig
CANCEL
ENDIF
IF ((loConfig.JsonTypeOf("tenant_id") <> 1) OR (loConfig.StringOfEquals("tenant_id","",1) = 1)) THEN
? "Expected a nonempty tenant_id in the JSON."
RELEASE loConfig
CANCEL
ENDIF
IF ((loConfig.JsonTypeOf("client_id") <> 1) OR (loConfig.StringOfEquals("client_id","",1) = 1)) THEN
? "Expected a nonempty client_id in the JSON."
RELEASE loConfig
CANCEL
ENDIF
IF ((loConfig.JsonTypeOf("client_secret") <> 1) OR (loConfig.StringOfEquals("client_secret","",1) = 1)) THEN
? "Expected a nonempty client_secret in the JSON."
RELEASE loConfig
CANCEL
ENDIF
IF ((loConfig.JsonTypeOf("user_id") <> 1) OR (loConfig.StringOfEquals("user_id","",1) = 1)) THEN
? "Expected a nonempty user_id in the JSON."
RELEASE loConfig
CANCEL
ENDIF
loHttp = CreateObject('Chilkat.Http')
loCredentials = CreateObject('Chilkat.JsonObject')
loCredentials.UpdateString("client_id",loConfig.StringOf("client_id"))
loCredentials.UpdateString("client_secret",loConfig.StringOf("client_secret"))
loCredentials.UpdateString("scope","https://graph.microsoft.com/.default")
loTokenEndpoint = CreateObject('Chilkat.StringBuilder')
loTokenEndpoint.Append("https://login.microsoftonline.com/")
loTokenEndpoint.Append(loHttp.UrlEncode(loConfig.StringOf("tenant_id")))
loTokenEndpoint.Append("/oauth2/v2.0/token")
loCredentials.UpdateString("token_endpoint",loTokenEndpoint.GetAsString())
* Http recognizes this credential JSON and acquires/caches application tokens internally.
* Reuse the Http object for subsequent requests; never print the credential JSON.
loHttp.AuthToken = loCredentials.Emit()
loHttp.RequireSslCertVerify = 1
loHttp.FollowRedirects = 0
loHttp.ConnectTimeout = 20
loHttp.ReadTimeout = 120
loHttp.SetRequestHeader("Accept","application/json")
loResp = CreateObject('Chilkat.HttpResponse')
loResult = CreateObject('Chilkat.JsonObject')
loResult.Strict = 1
loEndpoint = CreateObject('Chilkat.StringBuilder')
loEndpoint.Append("https://graph.microsoft.com/v1.0/users/")
loEndpoint.Append(loHttp.UrlEncode(loConfig.StringOf("user_id")))
loEndpoint.Append("/drives?$select=id,name,driveType,quota&$top=50")
lcUrl = loEndpoint.GetAsString()
loNextLink = CreateObject('Chilkat.StringBuilder')
loSeenLinks = CreateObject('Chilkat.Hashtable')
lnPageCount = 0
DO WHILE ((lcUrl <> "") AND (lnPageCount < 100))
loNextLink.SetString(lcUrl)
IF (loNextLink.StartsWith("https://graph.microsoft.com/v1.0/",1) <> 1) THEN
? "Unexpected pagination host or API version; no request sent."
RELEASE loConfig
RELEASE loHttp
RELEASE loCredentials
RELEASE loTokenEndpoint
RELEASE loResp
RELEASE loResult
RELEASE loEndpoint
RELEASE loNextLink
RELEASE loSeenLinks
CANCEL
ENDIF
IF (loSeenLinks.Contains(lcUrl) = 1) THEN
? "Repeated pagination URL; stopping."
RELEASE loConfig
RELEASE loHttp
RELEASE loCredentials
RELEASE loTokenEndpoint
RELEASE loResp
RELEASE loResult
RELEASE loEndpoint
RELEASE loNextLink
RELEASE loSeenLinks
CANCEL
ENDIF
loSeenLinks.AddInt(lcUrl,1)
lnSuccess = loHttp.HttpNoBody("GET",lcUrl,loResp)
IF ((lnSuccess <> 1) AND (loResp.StatusCode = 0)) THEN
? "No HTTP response received; inspect diagnostics privately and reconcile writes before retrying."
RELEASE loConfig
RELEASE loHttp
RELEASE loCredentials
RELEASE loTokenEndpoint
RELEASE loResp
RELEASE loResult
RELEASE loEndpoint
RELEASE loNextLink
RELEASE loSeenLinks
CANCEL
ENDIF
IF ((loResp.StatusCode <> 200)) THEN
? "Unexpected HTTP status: " + STR(loResp.StatusCode)
? "Request ID: " + loResp.GetHeaderField("request-id")
IF (loResp.StatusCode = 429) THEN
? "Retry-After: " + loResp.GetHeaderField("Retry-After")
ENDIF
RELEASE loConfig
RELEASE loHttp
RELEASE loCredentials
RELEASE loTokenEndpoint
RELEASE loResp
RELEASE loResult
RELEASE loEndpoint
RELEASE loNextLink
RELEASE loSeenLinks
CANCEL
ENDIF
lnSuccess = loResult.Load(loResp.BodyStr)
IF (lnSuccess <> 1) THEN
? "Expected a well-formed JSON response."
RELEASE loConfig
RELEASE loHttp
RELEASE loCredentials
RELEASE loTokenEndpoint
RELEASE loResp
RELEASE loResult
RELEASE loEndpoint
RELEASE loNextLink
RELEASE loSeenLinks
CANCEL
ENDIF
IF (loResult.JsonTypeOf("value") <> 4) THEN
? "Expected a value array."
RELEASE loConfig
RELEASE loHttp
RELEASE loCredentials
RELEASE loTokenEndpoint
RELEASE loResp
RELEASE loResult
RELEASE loEndpoint
RELEASE loNextLink
RELEASE loSeenLinks
CANCEL
ENDIF
i = 0
DO WHILE (i < loResult.SizeOfArray("value"))
loResult.I = i
IF ((loResult.JsonTypeOf("value[i].id") <> 1) OR (loResult.StringOfEquals("value[i].id","",1) = 1)) THEN
? "Expected a nonempty value[i].id in the JSON."
RELEASE loConfig
RELEASE loHttp
RELEASE loCredentials
RELEASE loTokenEndpoint
RELEASE loResp
RELEASE loResult
RELEASE loEndpoint
RELEASE loNextLink
RELEASE loSeenLinks
CANCEL
ENDIF
? "Drive ID: " + loResult.StringOf("value[i].id")
? "Name: " + loResult.StringOf("value[i].name")
? "Drive type: " + loResult.StringOf("value[i].driveType")
* Quota values can be larger than int. Missing quota fields are not zero.
IF (loResult.HasMember("value[i].quota.total") = 1) THEN
? "Total bytes: " + loResult.StringOf("value[i].quota.total")
? "Remaining bytes: " + loResult.StringOf("value[i].quota.remaining")
ENDIF
i = i + 1
ENDDO
lnPageCount = lnPageCount + 1
* A quoted JSON path treats the period in @odata.nextLink literally.
lnNextType = loResult.JsonTypeOf('"@odata.nextLink"')
IF ((lnNextType = -1) OR (lnNextType = 6)) THEN
lcUrl = ""
ELSE
IF ((lnNextType <> 1) OR (loResult.StringOfEquals('"@odata.nextLink"',"",1) = 1)) THEN
? "Invalid continuation link; cannot establish completeness."
RELEASE loConfig
RELEASE loHttp
RELEASE loCredentials
RELEASE loTokenEndpoint
RELEASE loResp
RELEASE loResult
RELEASE loEndpoint
RELEASE loNextLink
RELEASE loSeenLinks
CANCEL
ENDIF
lcUrl = loResult.StringOf('"@odata.nextLink"')
ENDIF
ENDDO
IF (lcUrl <> "") THEN
? "Stopped at the example's 100-page limit; save the continuation privately to resume."
RELEASE loConfig
RELEASE loHttp
RELEASE loCredentials
RELEASE loTokenEndpoint
RELEASE loResp
RELEASE loResult
RELEASE loEndpoint
RELEASE loNextLink
RELEASE loSeenLinks
CANCEL
ENDIF
? "Completed pages: " + STR(lnPageCount)
RELEASE loConfig
RELEASE loHttp
RELEASE loCredentials
RELEASE loTokenEndpoint
RELEASE loResp
RELEASE loResult
RELEASE loEndpoint
RELEASE loNextLink
RELEASE loSeenLinks