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

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.

Microsoft documentation

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Config
oleobject loo_Http
oleobject loo_Credentials
oleobject loo_TokenEndpoint
oleobject loo_Resp
oleobject loo_Result
oleobject loo_Endpoint
string ls_Url
oleobject loo_NextLink
oleobject loo_SeenLinks
long li_PageCount
long i
long li_NextType

li_Success = 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
li_Success = 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.
loo_Config = create oleobject
li_rc = loo_Config.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
    destroy loo_Config
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Config.Strict = 1
li_Success = loo_Config.LoadFile("onedrive-app.json")
if li_Success <> 1 then
    Write-Debug "Could not load the private application configuration."
    destroy loo_Config
    return
end if

if (loo_Config.JsonTypeOf("tenant_id") <> 1) OR (loo_Config.StringOfEquals("tenant_id","",1) = 1) then
    Write-Debug "Expected a nonempty tenant_id in the JSON."
    destroy loo_Config
    return
end if

if (loo_Config.JsonTypeOf("client_id") <> 1) OR (loo_Config.StringOfEquals("client_id","",1) = 1) then
    Write-Debug "Expected a nonempty client_id in the JSON."
    destroy loo_Config
    return
end if

if (loo_Config.JsonTypeOf("client_secret") <> 1) OR (loo_Config.StringOfEquals("client_secret","",1) = 1) then
    Write-Debug "Expected a nonempty client_secret in the JSON."
    destroy loo_Config
    return
end if

if (loo_Config.JsonTypeOf("user_id") <> 1) OR (loo_Config.StringOfEquals("user_id","",1) = 1) then
    Write-Debug "Expected a nonempty user_id in the JSON."
    destroy loo_Config
    return
end if

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

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

loo_Credentials.UpdateString("client_id",loo_Config.StringOf("client_id"))
loo_Credentials.UpdateString("client_secret",loo_Config.StringOf("client_secret"))
loo_Credentials.UpdateString("scope","https://graph.microsoft.com/.default")
loo_TokenEndpoint = create oleobject
li_rc = loo_TokenEndpoint.ConnectToNewObject("Chilkat.StringBuilder")

loo_TokenEndpoint.Append("https://login.microsoftonline.com/")
loo_TokenEndpoint.Append(loo_Http.UrlEncode(loo_Config.StringOf("tenant_id")))
loo_TokenEndpoint.Append("/oauth2/v2.0/token")
loo_Credentials.UpdateString("token_endpoint",loo_TokenEndpoint.GetAsString())
//  Http recognizes this credential JSON and acquires/caches application tokens internally.
//  Reuse the Http object for subsequent requests; never print the credential JSON.
loo_Http.AuthToken = loo_Credentials.Emit()
loo_Http.RequireSslCertVerify = 1
loo_Http.FollowRedirects = 0
loo_Http.ConnectTimeout = 20
loo_Http.ReadTimeout = 120
loo_Http.SetRequestHeader("Accept","application/json")
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

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

loo_Result.Strict = 1
loo_Endpoint = create oleobject
li_rc = loo_Endpoint.ConnectToNewObject("Chilkat.StringBuilder")

loo_Endpoint.Append("https://graph.microsoft.com/v1.0/users/")
loo_Endpoint.Append(loo_Http.UrlEncode(loo_Config.StringOf("user_id")))
loo_Endpoint.Append("/drives?$select=id,name,driveType,quota&$top=50")
ls_Url = loo_Endpoint.GetAsString()
loo_NextLink = create oleobject
li_rc = loo_NextLink.ConnectToNewObject("Chilkat.StringBuilder")

loo_SeenLinks = create oleobject
li_rc = loo_SeenLinks.ConnectToNewObject("Chilkat.Hashtable")

li_PageCount = 0
do while ((ls_Url <> "") AND (li_PageCount < 100))
    loo_NextLink.SetString(ls_Url)
    if loo_NextLink.StartsWith("https://graph.microsoft.com/v1.0/",1) <> 1 then
        Write-Debug "Unexpected pagination host or API version; no request sent."
        destroy loo_Config
        destroy loo_Http
        destroy loo_Credentials
        destroy loo_TokenEndpoint
        destroy loo_Resp
        destroy loo_Result
        destroy loo_Endpoint
        destroy loo_NextLink
        destroy loo_SeenLinks
        return
    end if

    if loo_SeenLinks.Contains(ls_Url) = 1 then
        Write-Debug "Repeated pagination URL; stopping."
        destroy loo_Config
        destroy loo_Http
        destroy loo_Credentials
        destroy loo_TokenEndpoint
        destroy loo_Resp
        destroy loo_Result
        destroy loo_Endpoint
        destroy loo_NextLink
        destroy loo_SeenLinks
        return
    end if

    loo_SeenLinks.AddInt(ls_Url,1)
    li_Success = loo_Http.HttpNoBody("GET",ls_Url,loo_Resp)
    if (li_Success <> 1) AND (loo_Resp.StatusCode = 0) then
        Write-Debug "No HTTP response received; inspect diagnostics privately and reconcile writes before retrying."
        destroy loo_Config
        destroy loo_Http
        destroy loo_Credentials
        destroy loo_TokenEndpoint
        destroy loo_Resp
        destroy loo_Result
        destroy loo_Endpoint
        destroy loo_NextLink
        destroy loo_SeenLinks
        return
    end if

    if (loo_Resp.StatusCode <> 200) then
        Write-Debug "Unexpected HTTP status: " + string(loo_Resp.StatusCode)
        Write-Debug "Request ID: " + loo_Resp.GetHeaderField("request-id")
        if loo_Resp.StatusCode = 429 then
            Write-Debug "Retry-After: " + loo_Resp.GetHeaderField("Retry-After")
        end if

        destroy loo_Config
        destroy loo_Http
        destroy loo_Credentials
        destroy loo_TokenEndpoint
        destroy loo_Resp
        destroy loo_Result
        destroy loo_Endpoint
        destroy loo_NextLink
        destroy loo_SeenLinks
        return
    end if

    li_Success = loo_Result.Load(loo_Resp.BodyStr)
    if li_Success <> 1 then
        Write-Debug "Expected a well-formed JSON response."
        destroy loo_Config
        destroy loo_Http
        destroy loo_Credentials
        destroy loo_TokenEndpoint
        destroy loo_Resp
        destroy loo_Result
        destroy loo_Endpoint
        destroy loo_NextLink
        destroy loo_SeenLinks
        return
    end if

    if loo_Result.JsonTypeOf("value") <> 4 then
        Write-Debug "Expected a value array."
        destroy loo_Config
        destroy loo_Http
        destroy loo_Credentials
        destroy loo_TokenEndpoint
        destroy loo_Resp
        destroy loo_Result
        destroy loo_Endpoint
        destroy loo_NextLink
        destroy loo_SeenLinks
        return
    end if

    i = 0
    do while (i < loo_Result.SizeOfArray("value"))
        loo_Result.I = i
        if (loo_Result.JsonTypeOf("value[i].id") <> 1) OR (loo_Result.StringOfEquals("value[i].id","",1) = 1) then
            Write-Debug "Expected a nonempty value[i].id in the JSON."
            destroy loo_Config
            destroy loo_Http
            destroy loo_Credentials
            destroy loo_TokenEndpoint
            destroy loo_Resp
            destroy loo_Result
            destroy loo_Endpoint
            destroy loo_NextLink
            destroy loo_SeenLinks
            return
        end if

        Write-Debug "Drive ID: " + loo_Result.StringOf("value[i].id")
        Write-Debug "Name: " + loo_Result.StringOf("value[i].name")
        Write-Debug "Drive type: " + loo_Result.StringOf("value[i].driveType")
        //  Quota values can be larger than int. Missing quota fields are not zero.
        if loo_Result.HasMember("value[i].quota.total") = 1 then
            Write-Debug "Total bytes: " + loo_Result.StringOf("value[i].quota.total")
            Write-Debug "Remaining bytes: " + loo_Result.StringOf("value[i].quota.remaining")
        end if

        i = i + 1
    loop
    li_PageCount = li_PageCount + 1
    //  A quoted JSON path treats the period in @odata.nextLink literally.
    li_NextType = loo_Result.JsonTypeOf("~"@odata.nextLink~"")
    if (li_NextType = -1) OR (li_NextType = 6) then
        ls_Url = ""
    else
        if (li_NextType <> 1) OR (loo_Result.StringOfEquals("~"@odata.nextLink~"","",1) = 1) then
            Write-Debug "Invalid continuation link; cannot establish completeness."
            destroy loo_Config
            destroy loo_Http
            destroy loo_Credentials
            destroy loo_TokenEndpoint
            destroy loo_Resp
            destroy loo_Result
            destroy loo_Endpoint
            destroy loo_NextLink
            destroy loo_SeenLinks
            return
        end if

        ls_Url = loo_Result.StringOf("~"@odata.nextLink~"")
    end if

loop
if ls_Url <> "" then
    Write-Debug "Stopped at the example's 100-page limit; save the continuation privately to resume."
    destroy loo_Config
    destroy loo_Http
    destroy loo_Credentials
    destroy loo_TokenEndpoint
    destroy loo_Resp
    destroy loo_Result
    destroy loo_Endpoint
    destroy loo_NextLink
    destroy loo_SeenLinks
    return
end if

Write-Debug "Completed pages: " + string(li_PageCount)


destroy loo_Config
destroy loo_Http
destroy loo_Credentials
destroy loo_TokenEndpoint
destroy loo_Resp
destroy loo_Result
destroy loo_Endpoint
destroy loo_NextLink
destroy loo_SeenLinks