PureBasic 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


IncludeFile "CkStream.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkRest.pb"
IncludeFile "CkOAuth2.pb"

Procedure ChilkatExample()

    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success.i

    ;   Provide a previously obtained OAuth2 access token.
    oauth2.i = CkOAuth2::ckCreate()
    If oauth2.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkOAuth2::setCkAccessToken(oauth2, "OAUTH2_ACCESS_TOKEN")
    CkRest::ckSetAuthOAuth2(rest,oauth2)

    success = CkRest::ckConnect(rest,"www.googleapis.com",443,1,1)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkOAuth2::ckDispose(oauth2)
        ProcedureReturn
    EndIf

    CkRest::ckAddQueryParam(rest,"includeTeamDriveItems","true")
    CkRest::ckAddQueryParam(rest,"supportsTeamDrives","true")
    CkRest::ckAddQueryParam(rest,"alt","media")

    ;  First send the HTTP request.
    success = CkRest::ckSendReqNoBody(rest,"GET","/drive/v3/files/1R_70heIyzIAu1_u0prXbYcaIiJRVkgBl")
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkOAuth2::ckDispose(oauth2)
        ProcedureReturn
    EndIf

    ;  Read the response header.  If the response status code is success, stream to the file.
    ;  Otherwise receive the error response text.
    statusCode.i = CkRest::ckReadResponseHeader(rest)
    If statusCode < 0
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkOAuth2::ckDispose(oauth2)
        ProcedureReturn
    EndIf

    If statusCode <> 200
        Debug "Received error response code: " + Str(statusCode)
        ;  Read the error response body.
        sbErrResponse.i = CkStringBuilder::ckCreate()
        If sbErrResponse.i = 0
            Debug "Failed to create object."
            ProcedureReturn
        EndIf

        success = CkRest::ckReadRespSb(rest,sbErrResponse)
        If success <> 1
            Debug CkRest::ckLastErrorText(rest)
            CkRest::ckDispose(rest)
            CkOAuth2::ckDispose(oauth2)
            CkStringBuilder::ckDispose(sbErrResponse)
            ProcedureReturn
        EndIf

        Debug "Error response:" + CkStringBuilder::ckGetAsString(sbErrResponse)
        CkRest::ckDispose(rest)
        CkOAuth2::ckDispose(oauth2)
        CkStringBuilder::ckDispose(sbErrResponse)
        ProcedureReturn
    EndIf

    ;  Stream the response body to the output file.
    respBodyStream.i = CkStream::ckCreate()
    If respBodyStream.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStream::setCkSinkFile(respBodyStream, "/someDirectory/penguins.jpg")
    success = CkRest::ckReadRespBodyStream(rest,respBodyStream,1)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkOAuth2::ckDispose(oauth2)
        CkStringBuilder::ckDispose(sbErrResponse)
        CkStream::ckDispose(respBodyStream)
        ProcedureReturn
    EndIf

    Debug "Example Completed."


    CkRest::ckDispose(rest)
    CkOAuth2::ckDispose(oauth2)
    CkStringBuilder::ckDispose(sbErrResponse)
    CkStream::ckDispose(respBodyStream)


    ProcedureReturn
EndProcedure