简体   繁体   中英

Google chat - Invalid space resource name in request

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM