We will use Google API Client Libraries for .NET for this sample.
There are other libraries. Please see Google Client Libraries Explained for details.
We will use the Cloud Resource Manager API for Creating and Managing Projects.
Let's get started.
Putting it all together...
You should have two files. The first file is either called packages.config
or project.json
.
Let's name the second file Program.cs
. All the code that was included in the sections above may be pasted into a single main method:
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Google.Apis.Auth.OAuth2;
using Google.Apis.CloudResourceManager.v1;
using Google.Apis.Services;
using Data = Google.Apis.CloudResourceManager.v1.Data;
namespace OurFirstProject
{
public class Program
{
private const string projectId = [YOUR-PROJECT-ID];
private const string applicationName = "Test";
public static void Main(string[] args)
{
var scopes = new String[] {
CloudResourceManagerService.Scope.CloudPlatform
};
GoogleCredential credential = Task.Run(
() => GoogleCredential.GetApplicationDefaultAsync()
).Result;
if (credential.IsCreateScopedRequired)
{
credential = credential.CreateScoped(scopes);
}
CloudResourceManagerService service = new CloudResourceManagerService(
new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = applicationName
}
);
Console.WriteLine("1. Create Project");
Data.Operation operation1 = service.Projects.Create(
new Data.Project()
{
ProjectId = projectId,
}
).Execute();
Console.Write("2. Awaiting Operation Completion");
Data.Operation operation2;
do
{
operation2 = service.Operations.Get(operation1.Name).Execute();
Console.WriteLine(operation2.Done.ToString());
System.Threading.Thread.Sleep(1000);
} while (operation2.Done != true);
Console.WriteLine();
Console.WriteLine("Enter to continue");
Console.ReadLine();
Console.WriteLine("3. Deleting Project");
var operation3 = service.Projects.Delete(projectId).Execute();
}
}
}
If you're using Windows and Visual Studio, "Start"
If you're using Linux, you should first restore the packages, then run the app
dotnet restore
dotnet run
The output should be similar to:
Compiling Projects for .NETCoreApp,Version=v1.1
Compilation succeeded.
0 Warning(s)
0 Error(s)
Time elapsed 00:00:01.4161926
1. Create Project
2. Awaiting Operation Completion
True
Enter to continue
3. Deleting Project
The "Awaiting" step will contain blank lines (hopefully) ending in "True".