使用 Cloud Resource Manager API Client for .NET 的新项目
把它们放在一起……
你应该有两个文件。第一个文件叫做 packages.config
或 project.json
。
我们将第二个文件命名为 Program.cs
。上面部分中包含的所有代码都可以粘贴到一个 main 方法中:
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();
}
}
}
如果你使用的是 Windows 和 Visual Studio,请单击开始
如果你使用的是 Linux,则应先恢复软件包,然后再运行该应用程序
dotnet restore
dotnet run
输出应类似于:
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
等待步骤将包含以 True
结尾的空行(希望)。