gpt4 book ai didi

c# - 如何使用 C# 向谷歌聊天室发布消息? (**错误** : Request had insufficient authentication scopes)

转载 作者:行者123 更新时间:2023-12-04 10:30:44 32 4
gpt4 key购买 nike

目前我正在尝试从 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}

最佳答案

this 判断也许您的范围应该设置为:

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

不要忘记使用旧范围删除先前生成的 token.json。

关于c# - 如何使用 C# 向谷歌聊天室发布消息? (**错误** : Request had insufficient authentication scopes),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60428039/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com