Skip to content
This repository has been archived by the owner on Oct 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #8 from Priyankarp24/drive-v2-snippets
Browse files Browse the repository at this point in the history
Drive v2 snippets
  • Loading branch information
RajeshGogo authored May 13, 2022
2 parents f9c8004 + 78bd1a2 commit fe7e2f1
Show file tree
Hide file tree
Showing 5 changed files with 12,309 additions and 0 deletions.
85 changes: 85 additions & 0 deletions drive/snippets/drive_v2/DriveV2Snippets/CreateFolder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// [START drive_create_Folder]

using System;
using Google;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v2beta;
using Google.Apis.Services;


namespace DriveV2Snippets
{
public class CreateFolder
{
/// <summary>
/// Creates a new folder
/// </summary>
/// <returns></returns>

public static string DriveCreateFolder()
{
try
{
/* Load pre-authorized user credentials from the environment.
TODO(developer) - See https://developers.google.com/identity for
guides on implementing OAuth2 for your application. */
GoogleCredential credential = GoogleCredential.GetApplicationDefault()
.CreateScoped(DriveService.Scope.Drive);

// Create Drive API service.
var service = new DriveService(new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName = "Drive API Snippets"
});
// File metadata
var fileMetadata = new Google.Apis.Drive.v2beta.Data.File()
{
Title = "Invoices",
MimeType = "application/vnd.google-apps.folder"
};
// Create a new folder on drive.
var request = service.Files.Insert(fileMetadata);
request.Fields = "id";
var file = request.Execute();
// Prints the created folder id.
Console.WriteLine("Folder ID: " + file.Id);
return file.Id;
}
catch (Exception e)
{
// TODO(developer) - handle error appropriately
if (e is AggregateException)
{
Console.WriteLine("Credential Not found");
}
else if (e is GoogleApiException)
{
Console.WriteLine("Failed to Create Folder with Error : {0}", e.Message);
}
else
{
throw;
}
}
return null;
}

}

}
// [END drive_create_Folder]
80 changes: 80 additions & 0 deletions drive/snippets/drive_v2/DriveV2Snippets/CreateShortcut.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// [START drive_create_shortcut]

using Google;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v2beta;
using Google.Apis.Services;

namespace DriveV2Snippets
{
// Class to demonstrate Drive's create shortcut use-case
public class CreateShortcut
{
/// <summary>
/// Create a third party shortcut.
/// </summary>
/// <returns>newly created shortcut file id, null otherwise.</returns>
public string DriveCreateShortcut()
{
try
{
/* Load pre-authorized user credentials from the environment.
TODO(developer) - See https://developers.google.com/identity for
guides on implementing OAuth2 for your application. */
GoogleCredential credential = GoogleCredential.GetApplicationDefault()
.CreateScoped(DriveService.Scope.Drive);

// Create Drive API service.
var service = new DriveService(new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName = "Drive API Snippets"
});
// Create Shortcut for file.
var fileMetadata = new Google.Apis.Drive.v2beta.Data.File()
{
Title = "Project plan",
MimeType = "application/vnd.google-apps.drive-sdk"
};
var request = service.Files.Insert(fileMetadata);
request.Fields = "id";
var file = request.Execute();
// Prints the shortcut file id.
Console.WriteLine("File ID: " + file.Id);
return file.Id;
}
catch (Exception e)
{
// TODO(developer) - handle error appropriately
if (e is AggregateException)
{
Console.WriteLine("Credential Not found");
}
else if (e is GoogleApiException)
{
Console.WriteLine("Failed with an error {0}",e.Message);
}
else
{
throw;
}
}
return null;
}
}
}
// [END drive_create_shortcut]
15 changes: 15 additions & 0 deletions drive/snippets/drive_v2/DriveV2Snippets/DriveV2Snippets.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>

</PropertyGroup>

<ItemGroup>
<PackageReference Include="Google.Apis.Auth" Version="1.57.0" />
<PackageReference Include="Google.Apis.Drive.v2" Version="1.57.0.2663" />
</ItemGroup>

</Project>
Loading

0 comments on commit fe7e2f1

Please sign in to comment.