Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkAuthGoogle.pb"
IncludeFile "CkRest.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

    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.

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

    CkAuthGoogle::setCkAccessToken(gAuth, "GOOGLE-DRIVE-ACCESS-TOKEN")

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

    ; Connect using TLS.
    bAutoReconnect.i = 1
    success = CkRest::ckConnect(rest,"www.googleapis.com",443,1,bAutoReconnect)

    ; Provide the authentication credentials (i.e. the access token)
    CkRest::ckSetAuthGoogle(rest,gAuth)

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

    CkJsonObject::setCkEmitCompact(json, 0)

    ; Get the AAWorkArea folder that is in the Google Drive root.
    CkRest::ckAddQueryParam(rest,"q","'root' in parents and name='AAWorkArea'")
    jsonResponse.s = CkRest::ckFullRequestNoBody(rest,"GET","/drive/v3/files")
    If CkRest::ckLastMethodSuccess(rest) <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkAuthGoogle::ckDispose(gAuth)
        CkRest::ckDispose(rest)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoad(json,jsonResponse)
    Debug CkJsonObject::ckEmit(json)
    Debug "name: " + CkJsonObject::ckStringOf(json,"files[0].name")
    Debug "id: " + CkJsonObject::ckStringOf(json,"files[0].id")
    Debug "mimeType: " + CkJsonObject::ckStringOf(json,"files[0].mimeType")
    Debug "-"

    CkRest::ckClearAllQueryParams(rest)

    ; Now that we know the ID for the AAWorkarea directory, get the id for the FolderA having AAWorkArea as the parent.
    sbQuery.i = CkStringBuilder::ckCreate()
    If sbQuery.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(sbQuery,"name = 'FolderA' and '")
    CkStringBuilder::ckAppend(sbQuery,CkJsonObject::ckStringOf(json,"files[0].id"))
    CkStringBuilder::ckAppend(sbQuery,"' in parents")
    CkRest::ckAddQueryParamSb(rest,"q",sbQuery)

    jsonResponse = CkRest::ckFullRequestNoBody(rest,"GET","/drive/v3/files")
    If CkRest::ckLastMethodSuccess(rest) <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkAuthGoogle::ckDispose(gAuth)
        CkRest::ckDispose(rest)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbQuery)
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoad(json,jsonResponse)
    Debug CkJsonObject::ckEmit(json)
    Debug "name: " + CkJsonObject::ckStringOf(json,"files[0].name")
    Debug "id: " + CkJsonObject::ckStringOf(json,"files[0].id")
    Debug "mimeType: " + CkJsonObject::ckStringOf(json,"files[0].mimeType")
    Debug "-"


    CkAuthGoogle::ckDispose(gAuth)
    CkRest::ckDispose(rest)
    CkJsonObject::ckDispose(json)
    CkStringBuilder::ckDispose(sbQuery)


    ProcedureReturn
EndProcedure