C# 无法使用\“启动浏览器”;https://accounts.google.com/o/oauth2/v2/auth?access_type=offline
我使用此代码通过我的web服务发送电子邮件C# 无法使用\“启动浏览器”;https://accounts.google.com/o/oauth2/v2/auth?access_type=offline,c#,email,.net-core,gmail,gmail-api,C#,Email,.net Core,Gmail,Gmail Api,我使用此代码通过我的web服务发送电子邮件 string[] Scopes = { GmailService.Scope.GmailSend }; string ApplicationName = "SendMail"; UserCredential credential; //read credentials file using (var stream = new FileStream("credentials.j
string[] Scopes = { GmailService.Scope.GmailSend };
string ApplicationName = "SendMail";
UserCredential credential;
//read credentials file
using (var stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
{
string credPath = "token.json";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None
).Result;
}
string plainText = $"To: user@domain.io\r\n" +
$"Subject: subject\r\n" +
"Content-Type: text/html; charset=utf-8\r\n\r\n" +
$"<h1>test email</h1>";
//call gmail service
var service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
var newMsg = new Google.Apis.Gmail.v1.Data.Message();
newMsg.Raw = Base64UrlEncode(plainText.ToString());
service.Users.Messages.Send(newMsg, "me").Execute();
string[]Scopes={GmailService.Scope.GmailSend};
字符串ApplicationName=“SendMail”;
用户凭证;
//读取凭据文件
使用(var stream=newfilestream(“credentials.json”、FileMode.Open、FileAccess.Read))
{
字符串credPath=“token.json”;
凭证=GoogleWebAuthorizationBroker.AuthorizationAsync(
GoogleClientSecrets.Load(stream.Secrets),
范围,
“用户”,
取消令牌。无
).结果;
}
字符串明文=$”到:user@domain.io\r\n“+
$“主题:主题\r\n”+
“内容类型:text/html;字符集=utf-8\r\n\r\n”+
$“测试电子邮件”;
//呼叫gmail服务
var service=new-GmailService(new-BaseClientService.Initializer()
{
HttpClientInitializer=凭证,
ApplicationName=ApplicationName,
});
var newMsg=newgoogle.api.Gmail.v1.Data.Message();
newMsg.Raw=Base64UrlEncode(明文.ToString());
service.Users.Messages.Send(newMsg,“me”).Execute();
部署后,它在本地完全正常工作,并抛出此错误
无法使用\启动浏览器https://accounts.google.com/o/oauth2/v2/auth?access_type=offline
即使我在重定向uri部分添加了服务器url和客户端的考虑事项
您正在Web服务器中使用已安装的应用程序授权流。这不适用。您必须在Web托管应用程序中以不同方式处理授权
在代码中,这意味着使用GoogleAuthorizationCodeFlow
而不是GoogleWebAuthorizationBroker
来构建客户端实例来验证和授权googleapi
已安装的应用程序与Web应用程序
部署Web应用程序时,必须首先获得正确类型的ClientID
关于如何验证Web服务器应用程序的详细指南
尽管这在每个客户机库中都非常相似,但这是一个关于如何在.NET Framework中实现服务器端身份验证流的示例
工具书类
对于web应用程序,您需要在开发控制台中指定重定向URL。如果应用程序缺少恒定的IP地址/URL,建议如何使用Google API?例如,我的应用程序可以作为localhost/appname或某些serveripaddress/appname启动。