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
(PureBasic) 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.
IncludeFile "CkBinData.pb" IncludeFile "CkOAuth2.pb" IncludeFile "CkJsonObject.pb" IncludeFile "CkUrl.pb" IncludeFile "CkStream.pb" IncludeFile "CkStringBuilder.pb" IncludeFile "CkRest.pb" Procedure ChilkatExample() ; This example requires the Chilkat API to have been previously unlocked. ; See Global Unlock Sample for sample code. rest.i = CkRest::ckCreate() If rest.i = 0 Debug "Failed to create object." ProcedureReturn EndIf ; 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. success.i = CkRest::ckConnect(rest,"graph.microsoft.com",443,1,1) If success <> 1 Debug CkRest::ckLastErrorText(rest) CkRest::ckDispose(rest) ProcedureReturn EndIf ; (Make sure your token was obtained with the FilesRead or Files.ReadWrite scope.) oauth2.i = CkOAuth2::ckCreate() If oauth2.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkOAuth2::setCkAccessToken(oauth2, "MICROSOFT_GRAPH_ACCESS_TOKEN") CkRest::ckSetAuthOAuth2(rest,oauth2) ; Send the GET request to download the file. uriPath.s = "/v1.0/me/drive/root:/Misc/wildlife/penguins.jpg:/content" success = CkRest::ckSendReqNoBody(rest,"GET",uriPath) If CkRest::ckLastMethodSuccess(rest) <> 1 Debug CkRest::ckLastErrorText(rest) CkRest::ckDispose(rest) CkOAuth2::ckDispose(oauth2) ProcedureReturn EndIf ; 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. statusCode.i = CkRest::ckReadResponseHeader(rest) Debug "Response Status Code = " + Str(statusCode) If statusCode = 302 ; 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. discard.i = CkBinData::ckCreate() If discard.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkRest::ckReadRespBd(rest,discard) CkRest::ckDisconnect(rest,10) ; 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. CkRest::ckClearAuth(rest) ; Follow the redirect URL... redirectUrl.i = CkRest::ckRedirectUrl(rest) Debug "Redirect Host: " + CkUrl::ckHost(redirectUrl) Debug "Redirect URI Path: " + CkUrl::ckPathWithQueryParams(redirectUrl) success = CkRest::ckConnect(rest,CkUrl::ckHost(redirectUrl),CkUrl::ckPort(redirectUrl),CkUrl::ckSsl(redirectUrl),1) If success <> 1 Debug CkRest::ckLastErrorText(rest) CkRest::ckDispose(rest) CkOAuth2::ckDispose(oauth2) CkBinData::ckDispose(discard) ProcedureReturn EndIf ; Send the request.. success = CkRest::ckSendReqNoBody(rest,"GET",CkUrl::ckPath(redirectUrl)) CkUrl::ckDispose(redirectUrl) If CkRest::ckLastMethodSuccess(rest) <> 1 Debug CkRest::ckLastErrorText(rest) CkRest::ckDispose(rest) CkOAuth2::ckDispose(oauth2) CkBinData::ckDispose(discard) ProcedureReturn EndIf statusCode = CkRest::ckReadResponseHeader(rest) Debug CkRest::ckLastErrorText(rest) Debug "Redirect Response Status Code = " + Str(statusCode) EndIf If statusCode >= 300 ; Read the error response body. sbJson.i = CkStringBuilder::ckCreate() If sbJson.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkRest::ckReadRespSb(rest,sbJson) If success <> 1 Debug CkRest::ckLastErrorText(rest) CkRest::ckDispose(rest) CkOAuth2::ckDispose(oauth2) CkBinData::ckDispose(discard) CkStringBuilder::ckDispose(sbJson) ProcedureReturn EndIf jsonErr.i = CkJsonObject::ckCreate() If jsonErr.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkJsonObject::setCkEmitCompact(jsonErr, 0) CkJsonObject::ckLoadSb(jsonErr,sbJson) Debug CkJsonObject::ckEmit(jsonErr) CkRest::ckDispose(rest) CkOAuth2::ckDispose(oauth2) CkBinData::ckDispose(discard) CkStringBuilder::ckDispose(sbJson) CkJsonObject::ckDispose(jsonErr) ProcedureReturn EndIf ; Stream the response body directly to a local file. localPath.s = "qa_output/penguins.jpg" stream.i = CkStream::ckCreate() If stream.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkStream::setCkSinkFile(stream, localPath) success = CkRest::ckReadRespBodyStream(rest,stream,1) If success <> 1 Debug CkRest::ckLastErrorText(rest) CkRest::ckDispose(rest) CkOAuth2::ckDispose(oauth2) CkBinData::ckDispose(discard) CkStringBuilder::ckDispose(sbJson) CkJsonObject::ckDispose(jsonErr) CkStream::ckDispose(stream) ProcedureReturn EndIf Debug "Successfully streamed a OneDrive file to the local filesystem." CkRest::ckDispose(rest) CkOAuth2::ckDispose(oauth2) CkBinData::ckDispose(discard) CkStringBuilder::ckDispose(sbJson) CkJsonObject::ckDispose(jsonErr) CkStream::ckDispose(stream) ProcedureReturn EndProcedure |
© 2000-2023 Chilkat Software, Inc. All Rights Reserved.