2

Currently i am trying to send message from C# to Google chat Room ,tried Authentication using following code


    UserCredential credential;
    var jsonPath = "";
    jsonPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "client_secrets.json");
    //using (var stream = new FileStream(jsonPath, FileMode.Open, FileAccess.Read))

    using (var stream = new FileStream(jsonPath, FileMode.Open, FileAccess.Read))
    {
        string[] scopes = new[] { "https://www.googleapis.com/auth/chat"};
        credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
            GoogleClientSecrets.Load(stream).Secrets,
             scopes,
            "admin@XXXX.XX", CancellationToken.None, new FileDataStore("C:\\AlexaHangout\\Google.Hangout.Auth.Store")).Result;
    }

    // Create the service.

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

    })


    Message msg = new Message();
    msg.Text = "Hi USER";
    msg.Sender = new User() { DisplayName = "Test", Name = "admin@XXXXX.XX", ETag = "12345", Type = "BOT" };
    msg.Space = new Space() { Name = "spaces/XXXXXXXX", DisplayName = "ViSev" }

    SpacesResource.MessagesResource.CreateRequest req = new SpacesResource.MessagesResource(service).Create(msg, "spaces/XXXXXXX");
    var result = req.Execute();

Authenication is working fine but when i trying to send message getting following error.

{"ClassName":"Google.GoogleApiException","Message":"Google.Apis.Requests.RequestError\r\nRequest had insufficient authentication scopes. [403]\r\nErrors [\r\n\tMessage[Request had insufficient authentication scopes.] Location[ - ] Reason[forbidden] Domain[global]\r\n]\r\n","Data":null,"InnerException":null,"HelpURL":null,"StackTraceString":" at Google.Apis.Requests.ClientServiceRequest`1.d__31.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at Google.Apis.Requests.ClientServiceRequest`1.Execute()\r\n at HangoutSample.WebForm1.CheckService() in E:\\mahesh Works\\HangoutSample\\HangoutSample\\WebForm1.aspx.cs:line 70","RemoteStackTraceString":null,"RemoteStackIndex":0,"ExceptionMethod":"8\nMoveNext\nGoogle.Apis, Version=1.43.0.0, Culture=neutral, PublicKeyToken=4b01fa6e34db77ab\nGoogle.Apis.Requests.ClientServiceRequest`1+d__31\nVoid MoveNext()","HResult":-2146233088,"Source":"Google.Apis","WatsonBuckets":null}
imsome1
  • 1,182
  • 4
  • 22
  • 37
Mahesh
  • 33
  • 5

1 Answers1

2

Judging by this maybe your scope should be set to:

string[] scopes = new[] { "https://www.googleapis.com/auth/chat.bot"};

Don't forget to delete the previously generated token.json with the old scope.

Dinac23
  • 214
  • 2
  • 12