Chilkat HOME Android™ Classic ASP C C++ C# Mono C# .NET Core C# C# UWP/WinRT DataFlex Delphi ActiveX Delphi DLL Visual FoxPro Java Lianja MFC Objective-C Perl PHP ActiveX PHP Extension PowerBuilder PowerShell PureBasic CkPython Chilkat2-Python Ruby SQL Server Swift 2 Swift 3/4 Tcl Unicode C Unicode C++ Visual Basic 6.0 VB.NET VB.NET UWP/WinRT VBScript Xojo Plugin Node.js Excel Go
(Tcl) 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.
load ./chilkat.dll # This example requires the Chilkat API to have been previously unlocked. # See Global Unlock Sample for sample code. set rest [new_CkRest] # 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. set success [CkRest_Connect $rest "graph.microsoft.com" 443 1 1] if {[expr $success != 1]} then { puts [CkRest_lastErrorText $rest] delete_CkRest $rest exit } # (Make sure your token was obtained with the FilesRead or Files.ReadWrite scope.) set oauth2 [new_CkOAuth2] CkOAuth2_put_AccessToken $oauth2 "MICROSOFT_GRAPH_ACCESS_TOKEN" CkRest_SetAuthOAuth2 $rest $oauth2 # Send the GET request to download the file. set uriPath "/v1.0/me/drive/root:/Misc/wildlife/penguins.jpg:/content" set success [CkRest_SendReqNoBody $rest "GET" $uriPath] if {[expr [CkRest_get_LastMethodSuccess $rest] != 1]} then { puts [CkRest_lastErrorText $rest] delete_CkRest $rest delete_CkOAuth2 $oauth2 exit } # 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. set statusCode [CkRest_ReadResponseHeader $rest] puts "Response Status Code = $statusCode" if {[expr $statusCode == 302]} then { # 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. set discard [new_CkBinData] [CkRest_ReadRespBd $rest $discard] [CkRest_Disconnect $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_ClearAuth $rest] # Follow the redirect URL... # redirectUrl is a CkUrl set redirectUrl [CkRest_RedirectUrl $rest] puts "Redirect Host: [CkUrl_host $redirectUrl]" puts "Redirect URI Path: [CkUrl_pathWithQueryParams $redirectUrl]" set success [CkRest_Connect $rest [CkUrl_host $redirectUrl] [CkUrl_get_Port $redirectUrl] [CkUrl_get_Ssl $redirectUrl] 1] if {[expr $success != 1]} then { puts [CkRest_lastErrorText $rest] delete_CkRest $rest delete_CkOAuth2 $oauth2 delete_CkBinData $discard exit } # Send the request.. set success [CkRest_SendReqNoBody $rest "GET" [CkUrl_path $redirectUrl]] delete_CkUrl $redirectUrl if {[expr [CkRest_get_LastMethodSuccess $rest] != 1]} then { puts [CkRest_lastErrorText $rest] delete_CkRest $rest delete_CkOAuth2 $oauth2 delete_CkBinData $discard exit } set statusCode [CkRest_ReadResponseHeader $rest] puts [CkRest_lastErrorText $rest] puts "Redirect Response Status Code = $statusCode" } if {[expr $statusCode >= 300]} then { # Read the error response body. set sbJson [new_CkStringBuilder] set success [CkRest_ReadRespSb $rest $sbJson] if {[expr $success != 1]} then { puts [CkRest_lastErrorText $rest] delete_CkRest $rest delete_CkOAuth2 $oauth2 delete_CkBinData $discard delete_CkStringBuilder $sbJson exit } set jsonErr [new_CkJsonObject] CkJsonObject_put_EmitCompact $jsonErr 0 [CkJsonObject_LoadSb $jsonErr $sbJson] puts [CkJsonObject_emit $jsonErr] delete_CkRest $rest delete_CkOAuth2 $oauth2 delete_CkBinData $discard delete_CkStringBuilder $sbJson delete_CkJsonObject $jsonErr exit } # Stream the response body directly to a local file. set localPath "qa_output/penguins.jpg" set stream [new_CkStream] CkStream_put_SinkFile $stream $localPath set success [CkRest_ReadRespBodyStream $rest $stream 1] if {[expr $success != 1]} then { puts [CkRest_lastErrorText $rest] delete_CkRest $rest delete_CkOAuth2 $oauth2 delete_CkBinData $discard delete_CkStringBuilder $sbJson delete_CkJsonObject $jsonErr delete_CkStream $stream exit } puts "Successfully streamed a OneDrive file to the local filesystem." delete_CkRest $rest delete_CkOAuth2 $oauth2 delete_CkBinData $discard delete_CkStringBuilder $sbJson delete_CkJsonObject $jsonErr delete_CkStream $stream |
© 2000-2016 Chilkat Software, Inc. All Rights Reserved.