Sample code for 30+ languages & platforms
PowerBuilder

Lookup Google Drive Folder ID Given a Folder Path

See more Google Drive Examples

Demonstrates how to find the Google Drive folder ID given a Folder Path.

This example demonstrates that we cannot simply query by folder name. (We can if every folder name is unique, but if not...) For example, imagine we have the following directory structure on Google Drive:

/AAWorkArea
/AAWorkArea/FolderA
/AAWorkArea/FolderB
/Folder2
/Folder2/FolderA

There are two directories named "FolderA". One is contained within AAWorkArea, and one is contained within "Folder2". To say it differently: One has the parent "AAWorkArea" and the other's parent is "Folder2".

To find the id of "/AAWorkArea/FolderA", we need to first find the id for "AAWorkArea", and then find the id for the folder with name="FolderA" and with AAWorkArea's id in FolderA's parents.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_GAuth
oleobject loo_Rest
integer li_BAutoReconnect
oleobject loo_Json
string ls_JsonResponse
oleobject loo_SbQuery

li_Success = 0

li_Success = 1

// It requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

// This example uses a previously obtained access token having permission for the 
// Google Drive scope.

loo_GAuth = create oleobject
li_rc = loo_GAuth.ConnectToNewObject("Chilkat.AuthGoogle")
if li_rc < 0 then
    destroy loo_GAuth
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_GAuth.AccessToken = "GOOGLE-DRIVE-ACCESS-TOKEN"

loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")

// Connect using TLS.
li_BAutoReconnect = 1
li_Success = loo_Rest.Connect("www.googleapis.com",443,1,li_BAutoReconnect)

// Provide the authentication credentials (i.e. the access token)
loo_Rest.SetAuthGoogle(loo_GAuth)

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.EmitCompact = 0

// Get the AAWorkArea folder that is in the Google Drive root.
loo_Rest.AddQueryParam("q","'root' in parents and name='AAWorkArea'")
ls_JsonResponse = loo_Rest.FullRequestNoBody("GET","/drive/v3/files")
if loo_Rest.LastMethodSuccess <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_GAuth
    destroy loo_Rest
    destroy loo_Json
    return
end if

loo_Json.Load(ls_JsonResponse)
Write-Debug loo_Json.Emit()
Write-Debug "name: " + loo_Json.StringOf("files[0].name")
Write-Debug "id: " + loo_Json.StringOf("files[0].id")
Write-Debug "mimeType: " + loo_Json.StringOf("files[0].mimeType")
Write-Debug "-"

loo_Rest.ClearAllQueryParams()

// Now that we know the ID for the AAWorkarea directory, get the id for the FolderA having AAWorkArea as the parent.
loo_SbQuery = create oleobject
li_rc = loo_SbQuery.ConnectToNewObject("Chilkat.StringBuilder")

loo_SbQuery.Append("name = 'FolderA' and '")
loo_SbQuery.Append(loo_Json.StringOf("files[0].id"))
loo_SbQuery.Append("' in parents")
loo_Rest.AddQueryParamSb("q",loo_SbQuery)

ls_JsonResponse = loo_Rest.FullRequestNoBody("GET","/drive/v3/files")
if loo_Rest.LastMethodSuccess <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_GAuth
    destroy loo_Rest
    destroy loo_Json
    destroy loo_SbQuery
    return
end if

loo_Json.Load(ls_JsonResponse)
Write-Debug loo_Json.Emit()
Write-Debug "name: " + loo_Json.StringOf("files[0].name")
Write-Debug "id: " + loo_Json.StringOf("files[0].id")
Write-Debug "mimeType: " + loo_Json.StringOf("files[0].mimeType")
Write-Debug "-"


destroy loo_GAuth
destroy loo_Rest
destroy loo_Json
destroy loo_SbQuery