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

发布于 2025-01-31 18:56:36 字数 2529 浏览 2 评论 0原文

有人可以帮助我使用“请求中的无效空间资源名称”。错误?

  1. 我在Google Cloud中创建了服务帐户/应用程序,
  2. 我创建了Google Chat API
  3. ,我创建了Space“ Server”,并将应用程序添加到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);
    }
}

似乎可以应用程序和凭据是可以的。什么会导致此错误?如果我创建了“服务器”空间并在此处添加了此应用程序,是否正确?

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

谢谢您的任何想法或观察

can someone help me with "Invalid space resource name in request." error?

  1. I created service account/application in Google Cloud
  2. I created Google Chat API
  3. I created space "server" and added my application to 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);
    }
}

It seems that application and credentials are ok. What can cause this error? Is it correct if I created a "server" space and added this application there?

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>
gt;g__SendMessage|0_0(String space, String message, String thread) in E:\TestGoogleChat\Program.cs:line 37

Thank you for any ideas or observations

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流年已逝 2025-02-07 18:56:36

您将方法称为错误。所有方法都需要通过服务对象调用。

这将为您提供空间列表。

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

然后,要创建一条消息,您将其添加到空间

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

空间中,

spaces.create

开发人员预览:作为Google Workspace开发人员预览程序的一部分,该程序可以尽早访问某些功能。

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

Your calling the method wrong. All methods need to be called though the service object.

This will get you a list of spaces.

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

Then to create a message you add it to the space

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

spaces create

As stated in the docs for spaces.create

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

As this method is not fully available this means that the client library will not be generated with this method. If you do have access to that method you would need to send the request yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文