0

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

Thank you for any ideas or observations

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Jozef
  • 13
  • 6

1 Answers1

0

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.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • Hello, Thanks for your reply. I apologize, however services.Spaces does not contain "Create" method. Are you sure she should be there? I use "Google.Apis.HangoutsChat.v1" in version "1.57.0.2687" – Jozef May 24 '22 at 14:01
  • You are correct there is no create method try using list. I put up an issue request to see if we cant figure out why we are missing the create method that appears in the documentation. [#2134](https://github.com/googleapis/google-api-dotnet-client/issues/2134) – Linda Lawton - DaImTo May 24 '22 at 14:46