Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

DataFlex Examples

ASN.1
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
Facebook
FileAccess
Firebase
GMail REST API
GeoOp
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Sheets
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)

Java KeyStore (JKS)
Jira
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
PayPal
Peoplevox
QuickBooks
REST
REST Misc
RSA Encryption
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Shopify
Socket/SSL/TLS
Spider
Stream
Stripe
SugarCRM
Tar Archive
Twitter
Upload
VoiceBase
Walmart
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Xero
Zip
curl
eBay

 

 

 

(DataFlex) OneDrive -- Streaming REST Download to File

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

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

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-2019 Chilkat Software, Inc. All Rights Reserved.