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

OneDrive: Resolve a Sharing URL to Drive and Item IDs

See more OneDrive Examples

Encode an existing sharing URL as unpadded base64url of its UTF-8 bytes, prefix u!, and retrieve its driveItem. If a remoteItem facet is returned, use the referenced drive and item IDs rather than the local wrapper. Retain both IDs as strings for later requests.

Authentication is still required. The URL does not bypass revoked links, sign-in restrictions, or sharing policy. The redeemSharingLinkIfNecessary preference can redeem access as needed for this request; it does not guarantee durable access for subsequent requests. Omit that preference when the application must only use access it already has. Do not print or log the URL or its reversible encoding.

Permission: Delegated Files.ReadWrite; application Files.ReadWrite.All (despite the GET request).

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 AutoIt Downloads

AutoIt
Local $bSuccess = False

;  Assumes Chilkat is unlocked. See Global Unlock Sample.
$bSuccess = False
;  Obtain a delegated Graph token with this example's file permissions.
;  See OneDrive OAuth2 with PKCE.
$oToken = ObjCreate("Chilkat.JsonObject")
$oToken.Strict = True
$bSuccess = $oToken.LoadFile("qa_data/tokens/oneDrive.json")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not load the private OneDrive token cache." & @CRLF)
    Exit
EndIf

If (($oToken.JsonTypeOf("access_token") <> 1) Or ($oToken.StringOfEquals("access_token","",True) = True)) Then
    ConsoleWrite("Expected a nonempty access_token in the JSON." & @CRLF)
    Exit
EndIf

$oHttp = ObjCreate("Chilkat.Http")
$oHttp.AuthToken = $oToken.StringOf("access_token")
$oHttp.RequireSslCertVerify = True
$oHttp.FollowRedirects = False
$oHttp.ConnectTimeout = 20
$oHttp.ReadTimeout = 120
$oHttp.SetRequestHeader "Accept","application/json"
$oResp = ObjCreate("Chilkat.HttpResponse")
$oResult = ObjCreate("Chilkat.JsonObject")
$oResult.Strict = True
;  Treat this input as private. A sharing URL is not an item ID or a direct download URL.
Local $sharingUrl = "YOUR_SHARING_URL"
$oEncodedUrl = ObjCreate("Chilkat.BinData")
$bSuccess = $oEncodedUrl.AppendString($sharingUrl,"utf-8")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not encode the sharing URL." & @CRLF)
    Exit
EndIf

Local $sEncoded = $oEncodedUrl.GetEncoded("base64url")
If ($oEncodedUrl.LastMethodSuccess <> True) Then
    ConsoleWrite("Could not encode the sharing URL." & @CRLF)
    Exit
EndIf

$oShareToken = ObjCreate("Chilkat.StringBuilder")
$oShareToken.Append("u!")
$oShareToken.Append($sEncoded)
;  base64url is URL-safe and unpadded; u! marks an encoded sharing URL.
;  This preference may redeem the link. Access is guaranteed only for this request.
$oHttp.SetRequestHeader "Prefer","redeemSharingLinkIfNecessary"
$oEndpoint = ObjCreate("Chilkat.StringBuilder")
$oEndpoint.Append("https://graph.microsoft.com/v1.0/shares/")
$oEndpoint.Append($oShareToken.GetAsString())
$oEndpoint.Append("/driveItem")
$bSuccess = $oHttp.HttpNoBody("GET",$oEndpoint.GetAsString(),$oResp)
If (($bSuccess <> True) And ($oResp.StatusCode = 0)) Then
    ConsoleWrite("No HTTP response received; inspect diagnostics privately and reconcile writes before retrying." & @CRLF)
    Exit
EndIf

If (($oResp.StatusCode <> 200)) Then
    ConsoleWrite("Unexpected HTTP status: " & $oResp.StatusCode & @CRLF)
    ConsoleWrite("Request ID: " & $oResp.GetHeaderField("request-id") & @CRLF)
    If ($oResp.StatusCode = 429) Then
        ConsoleWrite("Retry-After: " & $oResp.GetHeaderField("Retry-After") & @CRLF)
    EndIf

    Exit
EndIf

$bSuccess = $oResult.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
    ConsoleWrite("Expected a well-formed JSON response." & @CRLF)
    Exit
EndIf

Local $sDriveId
Local $sItemId
If ($oResult.JsonTypeOf("remoteItem") = 3) Then
    If (($oResult.JsonTypeOf("remoteItem.id") <> 1) Or ($oResult.StringOfEquals("remoteItem.id","",True) = True)) Then
        ConsoleWrite("Expected a nonempty remoteItem.id in the JSON." & @CRLF)
        Exit
    EndIf

    If (($oResult.JsonTypeOf("remoteItem.parentReference.driveId") <> 1) Or ($oResult.StringOfEquals("remoteItem.parentReference.driveId","",True) = True)) Then
        ConsoleWrite("Expected a nonempty remoteItem.parentReference.driveId in the JSON." & @CRLF)
        Exit
    EndIf

    $sItemId = $oResult.StringOf("remoteItem.id")
    $sDriveId = $oResult.StringOf("remoteItem.parentReference.driveId")
Else
    If (($oResult.JsonTypeOf("id") <> 1) Or ($oResult.StringOfEquals("id","",True) = True)) Then
        ConsoleWrite("Expected a nonempty id in the JSON." & @CRLF)
        Exit
    EndIf

    If (($oResult.JsonTypeOf("parentReference.driveId") <> 1) Or ($oResult.StringOfEquals("parentReference.driveId","",True) = True)) Then
        ConsoleWrite("Expected a nonempty parentReference.driveId in the JSON." & @CRLF)
        Exit
    EndIf

    $sItemId = $oResult.StringOf("id")
    $sDriveId = $oResult.StringOf("parentReference.driveId")
EndIf

ConsoleWrite("Drive ID: " & $sDriveId & @CRLF)
ConsoleWrite("Item ID: " & $sItemId & @CRLF)
;  Keep both strings together. Future requests still require appropriate access.