Xojo Plugin Google Drive: Make a Copy of a File

Back to Index

Makes a copy of a file. The name and description of the copy is provided in the JSON body of the request.

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


Dim rest As New Chilkat.Rest
Dim success As Boolean

//   Provide a previously obtained OAuth2 access token.
Dim oauth2 As New Chilkat.OAuth2
oauth2.AccessToken = "OAUTH2_ACCESS_TOKEN"
rest.SetAuthOAuth2(oauth2)

success = rest.Connect("www.googleapis.com",443,True,True)
If (success <> True) Then
    System.DebugLog(rest.LastErrorText)
    Return
End If

rest.AddQueryParam("supportsTeamDrives","true")

//  The following code creates the JSON request body.
//  The JSON created by this code is shown below.
Dim jsonReq As New Chilkat.JsonObject
jsonReq.UpdateString("name","penguins2.jpg")
jsonReq.UpdateString("description","resized and edited by Matt")

Dim sbReq As New Chilkat.StringBuilder
jsonReq.EmitSb(sbReq)

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

Dim sbJson As New Chilkat.StringBuilder
success = rest.FullRequestSb("POST","/drive/v3/files/1R_70heIyzIAu1_u0prXbYcaIiJRVkgBl/copy",sbReq,sbJson)
If (success <> True) Then
    System.DebugLog(rest.LastErrorText)
    Return
End If

If (rest.ResponseStatusCode <> 200) Then
    System.DebugLog("Received error response code: " + Str(rest.ResponseStatusCode))
    System.DebugLog("Response body:")
    System.DebugLog(sbJson.GetAsString())
    Return
End If

Dim json As New Chilkat.JsonObject
json.LoadSb(sbJson)

//  The following code parses the JSON response.
//  A sample JSON response is shown below the sample code.
Dim kind As String
Dim id As String
Dim name As String
Dim mimeType As String
Dim teamDriveId As String

kind = json.StringOf("kind")
id = json.StringOf("id")
name = json.StringOf("name")
mimeType = json.StringOf("mimeType")
teamDriveId = json.StringOf("teamDriveId")

System.DebugLog("Example Completed.")

Sample JSON Request Body

{
    "name": "penguins2.jpg",
    "description": "resized and edited by Matt"
}

Sample JSON Response Body

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