DataFlex Google Drive: Download a File (Stream to the Filesystem)

Back to Index

Downloads the content of a file by ID. (In this example the file id = 1R_70heIyzIAu1_u0prXbYcaIiJRVkgBl) The file is streamed directly to the filesystem. Note: The alt=media query param must be used to download the file content (as opposed to the file metadata).

Documentation: https://developers.google.com/drive/v3/reference/files/get


Use ChilkatAx-9.5.0-win32.pkg

Procedure Test
    Handle hoRest
    Boolean iSuccess
    Variant vOauth2
    Handle hoOauth2
    Integer iStatusCode
    Variant vSbErrResponse
    Handle hoSbErrResponse
    Variant vRespBodyStream
    Handle hoRespBodyStream
    String sTemp1

    Get Create (RefClass(cComChilkatRest)) To hoRest
    If (Not(IsComObjectCreated(hoRest))) Begin
        Send CreateComObject of hoRest
    End

    //   Provide a previously obtained OAuth2 access token.
    Get Create (RefClass(cComChilkatOAuth2)) To hoOauth2
    If (Not(IsComObjectCreated(hoOauth2))) Begin
        Send CreateComObject of hoOauth2
    End
    Set ComAccessToken Of hoOauth2 To "OAUTH2_ACCESS_TOKEN"
    Get pvComObject of hoOauth2 to vOauth2
    Get ComSetAuthOAuth2 Of hoRest vOauth2 To iSuccess

    Get ComConnect Of hoRest "www.googleapis.com" 443 True True To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComAddQueryParam Of hoRest "includeTeamDriveItems" "true" To iSuccess
    Get ComAddQueryParam Of hoRest "supportsTeamDrives" "true" To iSuccess
    Get ComAddQueryParam Of hoRest "alt" "media" To iSuccess

    //  First send the HTTP request.
    Get ComSendReqNoBody Of hoRest "GET" "/drive/v3/files/1R_70heIyzIAu1_u0prXbYcaIiJRVkgBl" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Read the response header.  If the response status code is success, stream to the file.
    //  Otherwise receive the error response text.
    Get ComReadResponseHeader Of hoRest To iStatusCode
    If (iStatusCode < 0) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    If (iStatusCode <> 200) Begin
        Showln "Received error response code: " iStatusCode
        //  Read the error response body.
        Get Create (RefClass(cComChilkatStringBuilder)) To hoSbErrResponse
        If (Not(IsComObjectCreated(hoSbErrResponse))) Begin
            Send CreateComObject of hoSbErrResponse
        End
        Get pvComObject of hoSbErrResponse to vSbErrResponse
        Get ComReadRespSb Of hoRest vSbErrResponse To iSuccess
        If (iSuccess <> True) Begin
            Get ComLastErrorText Of hoRest To sTemp1
            Showln sTemp1
            Procedure_Return
        End

        Get ComGetAsString Of hoSbErrResponse To sTemp1
        Showln "Error response:" sTemp1
        Procedure_Return
    End

    //  Stream the response body to the output file.
    Get Create (RefClass(cComChilkatStream)) To hoRespBodyStream
    If (Not(IsComObjectCreated(hoRespBodyStream))) Begin
        Send CreateComObject of hoRespBodyStream
    End
    Set ComSinkFile Of hoRespBodyStream To "/someDirectory/penguins.jpg"
    Get pvComObject of hoRespBodyStream to vRespBodyStream
    Get ComReadRespBodyStream Of hoRest vRespBodyStream True To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Example Completed."


End_Procedure