有没有办法通过.NET谷歌云sdk列出谷歌项目名称?

有没有办法通过.NET谷歌云sdk列出谷歌项目名称?,.net,google-cloud-platform,.net,Google Cloud Platform,我想在我们所有的谷歌项目中做一些内务管理,要做到这一点,我需要能够列出所有可用的项目。我在API的任何地方都找不到,我是不是遗漏了什么?我想它可能在IAM sdk中,但没有在那里找到它。关于它可能在哪里,或者我是否需要自己在API之上实现一些东西,有什么想法吗?要做到这一点,您必须使用原始API客户端。要使用的API是这个Google.API.CloudResourceManager.v1.CloudResourceManagerService 如果希望使用应用程序默认凭据,那么代码应该类似于(

我想在我们所有的谷歌项目中做一些内务管理,要做到这一点,我需要能够列出所有可用的项目。我在API的任何地方都找不到,我是不是遗漏了什么?我想它可能在IAM sdk中,但没有在那里找到它。关于它可能在哪里,或者我是否需要自己在API之上实现一些东西,有什么想法吗?

要做到这一点,您必须使用原始API客户端。要使用的API是这个
Google.API.CloudResourceManager.v1.CloudResourceManagerService

如果希望使用应用程序默认凭据,那么代码应该类似于(F#)

let getCredentials() = GoogleCredential.GetApplicationDefaultAsync() |> Async.AwaitTask
async {
    let! credentials = getCredentials()
    initializer.HttpClientInitializer <- credentials
    let crmService = new Google.Apis.CloudResourceManager.v1.CloudResourceManagerService(initializer)
    let projectsResource = crmService.Projects
    let projects = projectsResource.List().Execute().Projects
    .
    .
    .
}
``´
let getCredentials()=GoogleCredential.GetApplicationDefaultAsync()|>Async.AwaitTask
异步的{
让!credentials=getCredentials()

initializer.HttpClientInitializer要做到这一点,您必须使用原始API客户端。要使用的API是这一个
Google.API.CloudResourceManager.v1.CloudResourceManagerService

如果希望使用应用程序默认凭据,那么代码应该类似于(F#)

let getCredentials() = GoogleCredential.GetApplicationDefaultAsync() |> Async.AwaitTask
async {
    let! credentials = getCredentials()
    initializer.HttpClientInitializer <- credentials
    let crmService = new Google.Apis.CloudResourceManager.v1.CloudResourceManagerService(initializer)
    let projectsResource = crmService.Projects
    let projects = projectsResource.List().Execute().Projects
    .
    .
    .
}
``´
let getCredentials()=GoogleCredential.GetApplicationDefaultAsync()|>Async.AwaitTask
异步的{
让!credentials=getCredentials()

initializer.HttpClientInitializer我喜欢Tomas Jansson关于使用Google云资源管理器列出项目的问题/答案

权限和您可以访问的内容:

您只能列出您有权访问的项目。这意味着您无法查看所有项目,除非您有权访问它。在我下面的示例中,我显示了需要哪些范围。这也意味着您可以跨帐户列出项目。这允许您查看使用凭据可以访问哪些项目我展示了如何使用应用程序默认凭据(ADC)和服务帐户凭据(Json文件格式)

这个答案包括两个Python示例,它们使用两个不同的Google Cloud Python库。这些示例生成的输出与Google CLI
gcloud projects list
命令相同

示例1使用Python客户端库(服务发现方法):

来自GoogleAppClient导入发现的

从oauth2client.client导入Google凭据
从google.oauth2导入服务_帐户
#使用Python客户端库的示例
#文件
# https://github.com/googleapis/google-api-python-client
# https://developers.google.com/resources/api-libraries/documentation/cloudresourcemanager/v2/python/latest/
#图书馆安装
#pip安装-U谷歌api python客户端
#pip安装-U oauth2client
#需要下列作用域之一
# https://www.googleapis.com/auth/cloud-platform
# https://www.googleapis.com/auth/cloud-platform.read-only
# https://www.googleapis.com/auth/cloudplatformprojects
# https://www.googleapis.com/auth/cloudplatformprojects.readonly

打印({:我喜欢Tomas Jansson关于使用Google云资源管理器列出项目的问题/答案

权限和您可以访问的内容:

您只能列出您有权访问的项目。这意味着您无法查看所有项目,除非您有权访问它。在我下面的示例中,我显示了需要哪些范围。这也意味着您可以跨帐户列出项目。这允许您查看使用凭据可以访问哪些项目我展示了如何使用应用程序默认凭据(ADC)和服务帐户凭据(Json文件格式)

这个答案包括两个Python示例,它们使用两个不同的Google Cloud Python库。这些示例生成的输出与Google CLI
gcloud projects list
命令相同

示例1使用Python客户端库(服务发现方法):

来自GoogleAppClient导入发现的

从oauth2client.client导入Google凭据
从google.oauth2导入服务_帐户
#使用Python客户端库的示例
#文件
# https://github.com/googleapis/google-api-python-client
# https://developers.google.com/resources/api-libraries/documentation/cloudresourcemanager/v2/python/latest/
#图书馆安装
#pip安装-U谷歌api python客户端
#pip安装-U oauth2client
#需要下列作用域之一
# https://www.googleapis.com/auth/cloud-platform
# https://www.googleapis.com/auth/cloud-platform.read-only
# https://www.googleapis.com/auth/cloudplatformprojects
# https://www.googleapis.com/auth/cloudplatformprojects.readonly
print(“{:C#代码如下所示。它假定您已将环境变量GOOGLE_APPLICATION_CREDENTIALS设置为下载的服务帐户密钥json文件

using Google.Apis.Auth.OAuth2;
using Google.Apis.CloudResourceManager.v1;
using Google.Apis.Services;
using System;

namespace ListProjects
{
    class Program
    {
        static void Main(string[] args)
        {

             GoogleCredential credential =
                GoogleCredential.GetApplicationDefault();
            if (credential.IsCreateScopedRequired)
            {
                credential = credential.CreateScoped(new[]
                {
                    CloudResourceManagerService.Scope.CloudPlatformReadOnly
                });
            }
            var crmService = new CloudResourceManagerService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
            });
            var request = new ProjectsResource.ListRequest(crmService);
            while (true)
            {
                var result = request.Execute();
                foreach (var project in result.Projects)
                {
                    Console.WriteLine(project.ProjectId);
                }
                if (string.IsNullOrEmpty(result.NextPageToken))
                {
                    break;
                }
                request.PageToken = result.NextPageToken;
            }
        }
    }
}
C#代码如下所示。它假设您已将环境变量GOOGLE#u APPLICATION#u CREDENTIALS设置为下载的服务帐户密钥json文件

using Google.Apis.Auth.OAuth2;
using Google.Apis.CloudResourceManager.v1;
using Google.Apis.Services;
using System;

namespace ListProjects
{
    class Program
    {
        static void Main(string[] args)
        {

             GoogleCredential credential =
                GoogleCredential.GetApplicationDefault();
            if (credential.IsCreateScopedRequired)
            {
                credential = credential.CreateScoped(new[]
                {
                    CloudResourceManagerService.Scope.CloudPlatformReadOnly
                });
            }
            var crmService = new CloudResourceManagerService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
            });
            var request = new ProjectsResource.ListRequest(crmService);
            while (true)
            {
                var result = request.Execute();
                foreach (var project in result.Projects)
                {
                    Console.WriteLine(project.ProjectId);
                }
                if (string.IsNullOrEmpty(result.NextPageToken))
                {
                    break;
                }
                request.PageToken = result.NextPageToken;
            }
        }
    }
}

托马斯,非常有趣。请为.net发布更多类似的问题/答案。我个人的兴趣是c#,但任何语言示例都很棒。托马斯,非常有趣。请为.net发布更多类似的问题/答案。我个人的兴趣是c#,但任何语言示例都很棒。