如何使用 C# 向谷歌聊天室发布消息? (**错误**:请求的身份验证范围不足)

Posted

技术标签:

【中文标题】如何使用 C# 向谷歌聊天室发布消息? (**错误**:请求的身份验证范围不足)【英文标题】:How to post a message to google chat room using C#? (**Error**: Request had insufficient authentication scopes) 【发布时间】:2020-02-27 07:16:31 【问题描述】:

目前我正在尝试从 C# 向 Google 聊天室发送消息,尝试使用以下代码进行身份验证


    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();

身份验证工作正常,但是当我尝试发送消息时出现以下错误。

"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

【问题讨论】:

【参考方案1】:

根据this 判断,您的范围可能应该设置为:

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

不要忘记删除之前生成的带有旧范围的 token.json。

【讨论】:

我也检查过,但它显示“错误 400:invalid_scope”

以上是关于如何使用 C# 向谷歌聊天室发布消息? (**错误**:请求的身份验证范围不足)的主要内容,如果未能解决你的问题,请参考以下文章

向谷歌大查询插入数据时出现 503 错误

如何让谷歌跟踪直观地指示有错误的跨度

在 Android Studio 中从 JSON 向谷歌地图添加标记

以角度向谷歌表单提交数据时出现CORS问题

向谷歌查询添加参数

向谷歌应用添加域时请求无效