是否可以通过.NET谷歌云sdk列出谷歌项目名称?

时间:2019-01-12 12:33:45

标签: .net google-cloud-platform

我想在我们所有的Google项目中做一些整理工作,为此,我需要列出所有可用的项目。我在API的任何地方都找不到,我缺少什么吗?我以为它可能在IAM SDK中,但在那里找不到。有什么想法,或者我需要在API之上自己实现一些东西?

3 个答案:

答案 0 :(得分:1)

要执行此操作,您必须使用原始API客户端。要使用的API是这个Google.Apis.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
    .
    .
    .
}
``´

答案 1 :(得分:1)

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;
            }
        }
    }
}

答案 2 :(得分:0)

我喜欢Tomas Jansson关于使用Google Cloud Resource Manager列出项目的问题/答案。

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

您只能列出您有权访问的项目。这意味着除非您有权访问它,否则您将看不到所有项目。在下面的示例中,我显示了哪些范围是必需的。这也意味着您可以列出各个帐户的项目。这样,您可以使用示例中指定的凭据查看有权访问哪些项目。我展示了如何使用应用程序默认凭据(ADC)和服务帐户凭据(Json文件格式)。

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

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

from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
from google.oauth2 import service_account

# Example using the Python Client Library

# Documentation
# https://github.com/googleapis/google-api-python-client
# https://developers.google.com/resources/api-libraries/documentation/cloudresourcemanager/v2/python/latest/

# Library Installation
# pip install -U google-api-python-client
# pip install -U oauth2client

# Requires one of the following scopes
# 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('{:<20} {:<22} {:<21}'.format('PROJECT_ID', 'NAME', 'PROJECT_NUMBER'))

# Uncomment to use Application Default Credentials (ADC)
credentials = GoogleCredentials.get_application_default()

# Uncomment to use Service Account Credentials in Json format
# credentials = service_account.Credentials.from_service_account_file('service-account.json')

service = discovery.build('cloudresourcemanager', 'v1', credentials=credentials)

request = service.projects().list()

while request is not None:
        response = request.execute()

        for project in response.get('projects', []):
                print('{:<20} {:<22} {:<21}'.format(project['projectId'], project['name'], project['projectNumber']))

        request = service.projects().list_next(previous_request=request, previous_response=response)

使用Python Google Cloud Resource Manager API客户端库的示例2:

from google.cloud import resource_manager

# Example using the Python Google Cloud Resource Manager API Client Library

# Documentation
# https://pypi.org/project/google-cloud-resource-manager/
# https://github.com/googleapis/google-cloud-python
# https://googleapis.github.io/google-cloud-python/latest/resource-manager/index.html
# https://googleapis.github.io/google-cloud-python/latest/resource-manager/client.html
# https://googleapis.github.io/google-cloud-python/latest/resource-manager/project.html

# Library Installation
# pip install -U google-cloud-resource-manager

# Requires one of the following scopes
# 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('{:<20} {:<22} {:<21}'.format('PROJECT_ID', 'NAME', 'PROJECT_NUMBER'))

# Uncomment to use Application Default Credentials (ADC)
client = resource_manager.Client()

# Uncomment to use Service Account Credentials in Json format
# client = resource_manager.Client.from_service_account_json('service-account.json')

for project in client.list_projects():
        print('{:<20} {:<22} {:<21}'.format(project.project_id, project.name, project.number))