Google 聊天 - 请求中的 space 资源名称无效

Google chat - Invalid space resource name in request

谁能帮我解决“请求中的 space 资源名称无效。”错误?

  1. 我在 Google Cloud
  2. 中创建了服务 account/application
  3. 我创建了 Google 聊天 API
  4. 我创建了 space“服务器”并将我的应用程序添加到 space
using Google.Apis.Auth.OAuth2;
using Google.Apis.HangoutsChat.v1;
using Google.Apis.Services;

Console.WriteLine("START");
SendMessage("spaces/server", "Hello Jozef");
Console.WriteLine("END");

void SendMessage(string space, string message, string thread = null)
{
    try
    {
        var jsonPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "E://serverapplication-92ed800d27af.json");
        using (var stream = new FileStream(jsonPath, FileMode.Open, FileAccess.Read))
        {
            string[] scopes = new[] { "https://www.googleapis.com/auth/chat.bot" };

            var credential = GoogleCredential.FromStream(stream)
                .CreateScoped(scopes);

            var service = new HangoutsChatService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "ServerApplication"
            });

            var pubSubMessage = new Google.Apis.HangoutsChat.v1.Data.Message
            {
                Text = message,
                Thread = new Google.Apis.HangoutsChat.v1.Data.Thread() { Name = thread },
                Sender = new Google.Apis.HangoutsChat.v1.Data.User() { Name = "ServerApplication", DisplayName = "ServerApplication" },
            };

            SpacesResource.MessagesResource.CreateRequest req = new SpacesResource.MessagesResource(service).Create(pubSubMessage, space);
            var result = req.Execute();
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
    }
}

看来申请和凭据没问题。什么会导致此错误?如果我创建一个“服务器”space 并在那里添加这个应用程序是否正确?

The service chat has thrown an exception.
HttpStatusCode is BadRequest.
Google.Apis.Requests.RequestError
Invalid space resource name in request. [400]
Errors [
        Message[Invalid space resource name in request.] Location[ - ] Reason[badRequest] Domain[global]
]

Google.GoogleApiException: The service chat has thrown an exception. HttpStatusCode is BadRequest. Invalid space resource name in request.
   at Google.Apis.Requests.ClientServiceRequest`1.ParseResponse(HttpResponseMessage response)
   at Google.Apis.Requests.ClientServiceRequest`1.Execute()
   at Program.<<Main>$>g__SendMessage|0_0(String space, String message, String thread) in E:\TestGoogleChat\Program.cs:line 37

感谢您的任何想法或意见

你调用的方法有误。所有方法都需要通过服务对象调用。

这将为您提供 space 的列表。

var listOfSpaces = service.Spaces.List().Execute();

然后要创建消息,请将其添加到 space

var request = service.Spaces.Messages.Create(new Message(), "spaces/{SpaceId}");
var response = request.Execute();

spaces 创建

spaces.create

的文档中所述

Developer Preview: Available as part of the Google Workspace Developer Preview Program, which grants early access to certain features.

由于此方法不完全可用,这意味着不会使用此方法生成客户端库。如果您确实有权访问该方法,则需要自己发送请求。