Excel 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


Dim rest As Chilkat.Rest
Set rest = Chilkat.NewRest

'   Provide a previously obtained OAuth2 access token.
Dim oauth2 As Chilkat.OAuth2
Set oauth2 = Chilkat.NewOAuth2
oauth2.AccessToken = "OAUTH2_ACCESS_TOKEN"
success = rest.SetAuthOAuth2(oauth2)

success = rest.Connect("www.googleapis.com",443,True,True)
If (success <> True) Then
    Debug.Print rest.LastErrorText
    Exit Sub
End If

success = rest.AddQueryParam("includeTeamDriveItems","true")
success = rest.AddQueryParam("supportsTeamDrives","true")
success = rest.AddQueryParam("alt","media")

'  First send the HTTP request.
success = rest.SendReqNoBody("GET","/drive/v3/files/1R_70heIyzIAu1_u0prXbYcaIiJRVkgBl")
If (success <> True) Then
    Debug.Print rest.LastErrorText
    Exit Sub
End If

'  Read the response header.  If the response status code is success, stream to the file.
'  Otherwise receive the error response text.

statusCode = rest.ReadResponseHeader()
If (statusCode < 0) Then
    Debug.Print rest.LastErrorText
    Exit Sub
End If

If (statusCode <> 200) Then
    Debug.Print "Received error response code: "; statusCode
    '  Read the error response body.
    Dim sbErrResponse As Chilkat.StringBuilder
    Set sbErrResponse = Chilkat.NewStringBuilder
    success = rest.ReadRespSb(sbErrResponse)
    If (success <> True) Then
        Debug.Print rest.LastErrorText
        Exit Sub
    End If

    Debug.Print "Error response:"; sbErrResponse.GetAsString()
    Exit Sub
End If

'  Stream the response body to the output file.
Dim respBodyStream As Chilkat.Stream
Set respBodyStream = Chilkat.NewStream
respBodyStream.SinkFile = "/someDirectory/penguins.jpg"
success = rest.ReadRespBodyStream(respBodyStream,True)
If (success <> True) Then
    Debug.Print rest.LastErrorText
    Exit Sub
End If

Debug.Print "Example Completed."