PureBasic Google Drive: List Team Drives

Back to Index

Lists the user's Team Drives.

Documentation: https://developers.google.com/drive/v3/reference/teamdrives/list


IncludeFile "CkJsonObject.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

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

    success = CkRest::ckFullRequestNoBodySb(rest,"GET","/drive/v3/teamdrives",sbJson)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkOAuth2::ckDispose(oauth2)
        CkStringBuilder::ckDispose(sbJson)
        ProcedureReturn
    EndIf

    If CkRest::ckResponseStatusCode(rest) <> 200
        Debug "Received error response code: " + Str(CkRest::ckResponseStatusCode(rest))
        Debug "Response body:"
        Debug CkStringBuilder::ckGetAsString(sbJson)
        CkRest::ckDispose(rest)
        CkOAuth2::ckDispose(oauth2)
        CkStringBuilder::ckDispose(sbJson)
        ProcedureReturn
    EndIf

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

    CkJsonObject::ckLoadSb(json,sbJson)

    ;  The following code parses the JSON response.
    ;  A sample JSON response is shown below the sample code.
    kind.s
    i.i
    count_i.i
    id.s
    name.s

    kind = CkJsonObject::ckStringOf(json,"kind")
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(json,"teamDrives")
    While i < count_i
        CkJsonObject::setCkI(json, i)
        kind = CkJsonObject::ckStringOf(json,"teamDrives[i].kind")
        id = CkJsonObject::ckStringOf(json,"teamDrives[i].id")
        name = CkJsonObject::ckStringOf(json,"teamDrives[i].name")
        i = i + 1
    Wend

    Debug "Example Completed."


    CkRest::ckDispose(rest)
    CkOAuth2::ckDispose(oauth2)
    CkStringBuilder::ckDispose(sbJson)
    CkJsonObject::ckDispose(json)


    ProcedureReturn
EndProcedure

Sample JSON Response Body

{
  "kind": "drive#teamDriveList",
  "teamDrives": [
    {
      "kind": "drive#teamDrive",
      "id": "0AEd3EhGff2SaUk9PVA",
      "name": "Project Pegasus"
    },
    {
      "kind": "drive#teamDrive",
      "id": "0AGO-j6qTGkqgUk9PVA",
      "name": "Project Unicorn"
    }
  ]
}