Unicode C++ Google Drive: Rename a File (Update a File's MetaData)

Back to Index

Updates a file's metadata and/or content with patch semantics. (This example renames the file having id = 1R_70heIyzIAu1_u0prXbYcaIiJRVkgBl)

Documentation: https://developers.google.com/drive/v3/reference/files/update


#include <CkRestW.h>
#include <CkOAuth2W.h>
#include <CkJsonObjectW.h>
#include <CkStringBuilderW.h>

void ChilkatSample(void)
    {
    CkRestW rest;
    bool success;

    //   Provide a previously obtained OAuth2 access token.
    CkOAuth2W oauth2;
    oauth2.put_AccessToken(L"OAUTH2_ACCESS_TOKEN");
    rest.SetAuthOAuth2(oauth2);

    success = rest.Connect(L"www.googleapis.com",443,true,true);
    if (success != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    rest.AddQueryParam(L"includeTeamDriveItems",L"true");
    rest.AddQueryParam(L"supportsTeamDrives",L"true");

    //  The following code creates the JSON request body.
    //  The JSON created by this code is shown below.
    CkJsonObjectW jsonReq;
    jsonReq.UpdateString(L"name",L"three_penguins.jpg");

    CkStringBuilderW sbReq;
    jsonReq.EmitSb(sbReq);

    rest.AddHeader(L"Content-Type",L"application/json");

    CkStringBuilderW sbJson;
    success = rest.FullRequestSb(L"PATCH",L"/drive/v3/files/1R_70heIyzIAu1_u0prXbYcaIiJRVkgBl",sbReq,sbJson);
    if (success != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    if (rest.get_ResponseStatusCode() != 200) {
        wprintf(L"Received error response code: %d\n",rest.get_ResponseStatusCode());
        wprintf(L"Response body:\n");
        wprintf(L"%s\n",sbJson.getAsString());
        return;
    }

    CkJsonObjectW json;
    json.LoadSb(sbJson);

    //  The following code parses the JSON response.
    //  A sample JSON response is shown below the sample code.
    const wchar_t *kind = 0;
    const wchar_t *id = 0;
    const wchar_t *name = 0;
    const wchar_t *mimeType = 0;
    const wchar_t *teamDriveId = 0;

    kind = json.stringOf(L"kind");
    id = json.stringOf(L"id");
    name = json.stringOf(L"name");
    mimeType = json.stringOf(L"mimeType");
    teamDriveId = json.stringOf(L"teamDriveId");

    wprintf(L"Example Completed.\n");
    }

Sample JSON Request Body

{
    "name": "three_penguins.jpg"
}

Sample JSON Response Body

{
  "kind": "drive#file",
  "id": "1R_70heIyzIAu1_u0prXbYcaIiJRVkgBl",
  "name": "three_penguins.jpg",
  "mimeType": "image/jpeg",
  "teamDriveId": "0AEd3EhGff2SaUk9PVA"
}