from __future__ import print_function
import google.auth
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
def create_shortcut():
"""Create a third party shortcut
Load pre-authorized user credentials from the environment.
TODO(developer) - See https://developers.google.com/identity
for guides on implementing OAuth2 for the application.
"""
creds, _ = google.auth.default()
try:
# create drive api client
service = build('drive', 'v3', credentials=creds)
file_metadata = {
'name': 'Project plan',
'mimeType': 'application/vnd.google-apps.drive-sdk'
}
# pylint: disable=maybe-no-member
file = service.files().create(body=file_metadata,
fields='id').execute()
print(F'File ID: {file.get("id")}')
except HttpError as error:
print(F'An error occurred: {error}')
return file.get('id')
if __name__ == '__main__':
create_shortcut()
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Services;
namespace DriveV3Snippets
{
// 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 static 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.v3.Data.File()
{
Name = "Project plan",
MimeType = "application/vnd.google-apps.drive-sdk"
};
var request = service.Files.Create(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
{
throw;
}
}
return null;
}
}
}