Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(DataFlex) OneDrive -- Streaming REST Download to FileDownloads the contents of a DriveItem directly to a file in the local filesystem using the Chilkat REST class. Note: This example requires Chilkat v9.5.0.69 or greater.
Use ChilkatAx-9.5.0-win32.pkg Procedure Test Handle hoRest Boolean iSuccess Variant vOauth2 Handle hoOauth2 String sUriPath Integer iStatusCode Variant vDiscard Handle hoDiscard Variant vRedirectUrl Handle hoRedirectUrl Variant vSbJson Handle hoSbJson Handle hoJsonErr String sLocalPath Variant vStream Handle hoStream String sTemp1 Integer iTemp1 Boolean bTemp1 // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. Get Create (RefClass(cComChilkatRest)) To hoRest If (Not(IsComObjectCreated(hoRest))) Begin Send CreateComObject of hoRest End // Use your previously obtained access token here: // See the following examples for getting an access token: // Get Microsoft Graph OAuth2 Access Token (Azure AD v2.0 Endpoint). // Get Microsoft Graph OAuth2 Access Token (Azure AD Endpoint). // Refresh Access Token (Azure AD v2.0 Endpoint). // Refresh Access Token (Azure AD Endpoint). // First connect to graph.microsoft.com. If there's a connectivity problem, we'll find out here. Get ComConnect Of hoRest "graph.microsoft.com" 443 True True To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoRest To sTemp1 Showln sTemp1 Procedure_Return End // (Make sure your token was obtained with the FilesRead or Files.ReadWrite scope.) Get Create (RefClass(cComChilkatOAuth2)) To hoOauth2 If (Not(IsComObjectCreated(hoOauth2))) Begin Send CreateComObject of hoOauth2 End Set ComAccessToken Of hoOauth2 To "MICROSOFT_GRAPH_ACCESS_TOKEN" Get pvComObject of hoOauth2 to vOauth2 Get ComSetAuthOAuth2 Of hoRest vOauth2 To iSuccess // Send the GET request to download the file. Move "/v1.0/me/drive/root:/Misc/wildlife/penguins.jpg:/content" To sUriPath Get ComSendReqNoBody Of hoRest "GET" sUriPath To iSuccess Get ComLastMethodSuccess Of hoRest To bTemp1 If (bTemp1 <> True) Begin Get ComLastErrorText Of hoRest To sTemp1 Showln sTemp1 Procedure_Return End // NOTE: This way of doing the HTTP GET (i.e. download) may be more cumbersome, but it // allows for finer control of handling errors. The connection establishment, the sending of the // request, the reading of the response header, and the reading of the response body (i.e. the file data) // are handled by separate method calls. If the response header indicates an error, we can read // the response body and treat it differently than if reading the file data. // Read the response header. Get ComReadResponseHeader Of hoRest To iStatusCode Showln "Response Status Code = " iStatusCode If (iStatusCode = 302) Begin // This is a redirect. Read the response body, if any, and then follow the redirect. // Usually the response body will be empty for a redirect, but we need to be sure to read // the response body just in case it exists. Get Create (RefClass(cComChilkatBinData)) To hoDiscard If (Not(IsComObjectCreated(hoDiscard))) Begin Send CreateComObject of hoDiscard End Get pvComObject of hoDiscard to vDiscard Get ComReadRespBd Of hoRest vDiscard To iSuccess Get ComDisconnect Of hoRest 10 To iSuccess // For OneDrive, the redirect URL does not need authorization because the only way // to have obtained the direct download URL is from an authenticated request. // In fact, if we leave the authentication present, the GET request to the redirect URL will fail. // Note: The ClearAuth method is introduced in v9.5.0.69. Get ComClearAuth Of hoRest To iSuccess // Follow the redirect URL... Get ComRedirectUrl Of hoRest To vRedirectUrl If (IsComObject(vRedirectUrl)) Begin Get Create (RefClass(cComChilkatUrl)) To hoRedirectUrl Set pvComObject Of hoRedirectUrl To vRedirectUrl End Get ComHost Of hoRedirectUrl To sTemp1 Showln "Redirect Host: " sTemp1 Get ComPathWithQueryParams Of hoRedirectUrl To sTemp1 Showln "Redirect URI Path: " sTemp1 Get ComHost Of hoRedirectUrl To sTemp1 Get ComPort Of hoRedirectUrl To iTemp1 Get ComSsl Of hoRedirectUrl To bTemp1 Get ComConnect Of hoRest sTemp1 iTemp1 bTemp1 True To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoRest To sTemp1 Showln sTemp1 Procedure_Return End // Send the request.. Get ComPath Of hoRedirectUrl To sTemp1 Get ComSendReqNoBody Of hoRest "GET" sTemp1 To iSuccess Send Destroy of hoRedirectUrl Get ComLastMethodSuccess Of hoRest To bTemp1 If (bTemp1 <> True) Begin Get ComLastErrorText Of hoRest To sTemp1 Showln sTemp1 Procedure_Return End Get ComReadResponseHeader Of hoRest To iStatusCode Get ComLastErrorText Of hoRest To sTemp1 Showln sTemp1 Showln "Redirect Response Status Code = " iStatusCode End If (iStatusCode >= 300) Begin // Read the error response body. Get Create (RefClass(cComChilkatStringBuilder)) To hoSbJson If (Not(IsComObjectCreated(hoSbJson))) Begin Send CreateComObject of hoSbJson End Get pvComObject of hoSbJson to vSbJson Get ComReadRespSb Of hoRest vSbJson To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoRest To sTemp1 Showln sTemp1 Procedure_Return End Get Create (RefClass(cComChilkatJsonObject)) To hoJsonErr If (Not(IsComObjectCreated(hoJsonErr))) Begin Send CreateComObject of hoJsonErr End Set ComEmitCompact Of hoJsonErr To False Get pvComObject of hoSbJson to vSbJson Get ComLoadSb Of hoJsonErr vSbJson To iSuccess Get ComEmit Of hoJsonErr To sTemp1 Showln sTemp1 Procedure_Return End // Stream the response body directly to a local file. Move "qa_output/penguins.jpg" To sLocalPath Get Create (RefClass(cComChilkatStream)) To hoStream If (Not(IsComObjectCreated(hoStream))) Begin Send CreateComObject of hoStream End Set ComSinkFile Of hoStream To sLocalPath Get pvComObject of hoStream to vStream Get ComReadRespBodyStream Of hoRest vStream True To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoRest To sTemp1 Showln sTemp1 Procedure_Return End Showln "Successfully streamed a OneDrive file to the local filesystem." End_Procedure |
© 2000-2023 Chilkat Software, Inc. All Rights Reserved.