PureBasic Google Drive: Create a New Comment on a File

Back to Index

Creates a new comment on a file. The file is specified by file ID. This example creates a comment on the file having id = "0B5drHSd5ZHwgc3RhcnRlcl9maWxlX2Rhc2hlclYw".

Documentation: https://developers.google.com/drive/v3/reference/comments/create


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

    CkRest::ckAddQueryParam(rest,"fields","author,content")

    ;  The following code creates the JSON request body.
    ;  The JSON created by this code is shown below.
    jsonReq.i = CkJsonObject::ckCreate()
    If jsonReq.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(jsonReq,"content","This is a test comment about this file...")

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

    CkJsonObject::ckEmitSb(jsonReq,sbReq)

    CkRest::ckAddHeader(rest,"Content-Type","application/json")

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

    success = CkRest::ckFullRequestSb(rest,"POST","/drive/v3/files/0B5drHSd5ZHwgc3RhcnRlcl9maWxlX2Rhc2hlclYw/comments",sbReq,sbJson)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkOAuth2::ckDispose(oauth2)
        CkJsonObject::ckDispose(jsonReq)
        CkStringBuilder::ckDispose(sbReq)
        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)
        CkJsonObject::ckDispose(jsonReq)
        CkStringBuilder::ckDispose(sbReq)
        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.
    authorKind.s
    authorDisplayName.s
    authorPhotoLink.s
    authorMe.i
    content.s

    authorKind = CkJsonObject::ckStringOf(json,"author.kind")
    authorDisplayName = CkJsonObject::ckStringOf(json,"author.displayName")
    authorPhotoLink = CkJsonObject::ckStringOf(json,"author.photoLink")
    authorMe = CkJsonObject::ckBoolOf(json,"author.me")
    content = CkJsonObject::ckStringOf(json,"content")

    Debug "Example Completed."


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


    ProcedureReturn
EndProcedure

Sample JSON Request Body

{
  "content": "This is a test comment about this file..."
}

Sample JSON Response Body

{
  "author": {
    "kind": "drive#user",
    "displayName": "Matt Fausey",
    "photoLink": "//ssl.gstatic.com/s2/profiles/images/silhouette96.png",
    "me": true
  },
  "content": "This is a test comment about this file..."
}