使用 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
結尾的空行(希望)。