PowerBuilder 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


integer li_rc
oleobject loo_Rest
integer li_Success
oleobject loo_Oauth2
integer li_StatusCode
oleobject loo_SbErrResponse
oleobject loo_RespBodyStream

loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat_9_5_0.Rest")
if li_rc < 0 then
    destroy loo_Rest
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//   Provide a previously obtained OAuth2 access token.
loo_Oauth2 = create oleobject
li_rc = loo_Oauth2.ConnectToNewObject("Chilkat_9_5_0.OAuth2")

loo_Oauth2.AccessToken = "OAUTH2_ACCESS_TOKEN"
loo_Rest.SetAuthOAuth2(loo_Oauth2)

li_Success = loo_Rest.Connect("www.googleapis.com",443,1,1)
if li_Success <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_Oauth2
    return
end if

loo_Rest.AddQueryParam("includeTeamDriveItems","true")
loo_Rest.AddQueryParam("supportsTeamDrives","true")
loo_Rest.AddQueryParam("alt","media")

//  First send the HTTP request.
li_Success = loo_Rest.SendReqNoBody("GET","/drive/v3/files/1R_70heIyzIAu1_u0prXbYcaIiJRVkgBl")
if li_Success <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_Oauth2
    return
end if

//  Read the response header.  If the response status code is success, stream to the file.
//  Otherwise receive the error response text.
li_StatusCode = loo_Rest.ReadResponseHeader()
if li_StatusCode < 0 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_Oauth2
    return
end if

if li_StatusCode <> 200 then
    Write-Debug "Received error response code: " + string(li_StatusCode)
    //  Read the error response body.
    loo_SbErrResponse = create oleobject
    li_rc = loo_SbErrResponse.ConnectToNewObject("Chilkat_9_5_0.StringBuilder")

    li_Success = loo_Rest.ReadRespSb(loo_SbErrResponse)
    if li_Success <> 1 then
        Write-Debug loo_Rest.LastErrorText
        destroy loo_Rest
        destroy loo_Oauth2
        destroy loo_SbErrResponse
        return
    end if

    Write-Debug "Error response:" + loo_SbErrResponse.GetAsString()
    destroy loo_Rest
    destroy loo_Oauth2
    destroy loo_SbErrResponse
    return
end if

//  Stream the response body to the output file.
loo_RespBodyStream = create oleobject
li_rc = loo_RespBodyStream.ConnectToNewObject("Chilkat_9_5_0.Stream")

loo_RespBodyStream.SinkFile = "/someDirectory/penguins.jpg"
li_Success = loo_Rest.ReadRespBodyStream(loo_RespBodyStream,1)
if li_Success <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_Oauth2
    destroy loo_SbErrResponse
    destroy loo_RespBodyStream
    return
end if

Write-Debug "Example Completed."


destroy loo_Rest
destroy loo_Oauth2
destroy loo_SbErrResponse
destroy loo_RespBodyStream