简体   繁体   中英

Not getting folder id in response after creating folder to google drive by google api

I am using rest end point and don't get folder id on creation. It should be in response but not there. Folder is getting created successfully.

url: https://www.googleapis.com/drive/v3/files

public static async void CreateFolder(string accessToken, string brandFolderName)
{
    var gDriveItems = await GetFoldersByBrand(accessToken,brandFolderName);
    if (gDriveItems.Any(x=>x.Name.ToLower() == brandFolderName.ToLower()))
    {
        return;
    }

    var request = new HttpRequestMessage(HttpMethod.Post, "drive/v3/files");
        request.Headers.Add("Authorization", "Bearer "+ accessToken);
        request.Headers.Add("Accept","application/json");

    JsonObject jsonFolderObject = new JsonObject();
    jsonFolderObject.Add("name", brandFolderName);
    jsonFolderObject.Add("mimeType", "application/vnd.google-apps.folder");
    var data = JsonConvert.SerializeObject(jsonFolderObject);

    request.Content = new StringContent(data, Encoding.UTF8, "application/json");
    var responce = await _httpClient.SendAsync(request);
    var mm = responce.Content.ReadAsStringAsync();
    responce.EnsureSuccessStatusCode();
}

For reference, here's my code to add a new folder.

  • I use Google.Apis.Drive.v3
  • service is an instance of DriveService with the relevant scope to create a folder/file.

This will give me the Id of the new folder in the result

    private static async Task<File> CreateRankingsFolder(DriveService service, 
        string driveId, 
        string parentId, 
        string folderName = "YOURFOLDERNAME")
    {
        File result = null;

        try
        {

            File body = new File();
            body.Name = folderName;
            body.MimeType = "application/vnd.google-apps.folder";
            body.DriveId = driveId;
            if (!string.IsNullOrEmpty(parentId))
            {
                var _parents = new List<string>()
                {
                    parentId
                };
                body.Parents = _parents;
            }

            // service is an authorized Drive API service instance
            var req = service.Files.Create(body);

            result = await req.ExecuteAsync();



        }
        catch(Exception e)
        {

        }

        return result;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM