This repository has been archived by the owner on Oct 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from Priyankarp24/drive-v2-snippets
Drive v2 snippets
- Loading branch information
Showing
5 changed files
with
12,309 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
drive/snippets/drive_v2/DriveV2Snippets/DriveV2Snippets.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.