C# 发送电子邮件的权限不足[403]

C# 发送电子邮件的权限不足[403],c#,google-api,google-oauth,gmail-api,google-api-dotnet-client,C#,Google Api,Google Oauth,Gmail Api,Google Api Dotnet Client,我正在尝试在我的web应用程序(当前在localhost中)中发送电子邮件,我正在使用我在控制台应用程序中使用的下一个代码(及其工作代码),它应该打开一个网页,询问google帐户: public async Task<String> SendEmail(String from, String text) { try { UserCredential credential = await GoogleWebAuthorizationBroker.Aut

我正在尝试在我的web应用程序(当前在localhost中)中发送电子邮件,我正在使用我在控制台应用程序中使用的下一个代码(及其工作代码),它应该打开一个网页,询问google帐户:

public async Task<String> SendEmail(String from, String text)
{
    try
    {
        UserCredential credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
            new ClientSecrets
            {
                ClientId = "-------------",
                ClientSecret = "----------"
            },
            new[] { GmailService.Scope.GmailSend },
            "user",
            CancellationToken.None,
            new FileDataStore(this.GetType().ToString()));

        // Create Gmail API service.
        var service = new GmailService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = this.GetType().ToString()
        });

        string plainText = "To: -------@gmail.com," + from + "\r\n" +
                           "Subject: T2S\r\n" +
                           "Content-Type: text/html; charset=us-ascii\r\n\r\n" +
                           text;

        var newMsg = new Google.Apis.Gmail.v1.Data.Message();
        newMsg.Raw = Base64UrlEncode(plainText.ToString());
        service.Users.Messages.Send(newMsg, "me").Execute();
        return "Done";
    }
    catch (Exception ex)
    {
        System.Console.WriteLine(ex.Message);
        return "False";
    }
}

private string Base64UrlEncode(string input)
{
    var inputBytes = System.Text.Encoding.UTF8.GetBytes(input);
    return System.Convert.ToBase64String(inputBytes).Replace("+", "-").Replace("/", "_").Replace("=", "");
}
public async Task sendmail(字符串从,字符串文本)
{
尝试
{
UserCredential credential=等待GoogleWebAuthorizationBroker.AuthorizationAsync(
新客户的秘密
{
ClientId=“--------------”,
ClientSecret=“------------”
},
新建[]{GmailService.Scope.GmailSend},
“用户”,
取消令牌。无,
新文件数据存储(this.GetType().ToString());
//创建Gmail API服务。
var service=new-GmailService(new-BaseClientService.Initializer()
{
HttpClientInitializer=凭证,
ApplicationName=this.GetType().ToString()
});
string plainText=“To:--------@gmail.com,“+from+”\r\n+
“主题:T2S\r\n”+
“内容类型:text/html;字符集=us ascii\r\n\r\n”+
文本;
var newMsg=newgoogle.api.Gmail.v1.Data.Message();
newMsg.Raw=Base64UrlEncode(明文.ToString());
service.Users.Messages.Send(newMsg,“me”).Execute();
返回“完成”;
}
捕获(例外情况除外)
{
系统控制台写入线(例如消息);
返回“False”;
}
}
专用字符串Base64UrlEncode(字符串输入)
{
var inputBytes=System.Text.Encoding.UTF8.GetBytes(输入);
返回System.Convert.ToBase64String(inputBytes)。替换(“+”,“-”)。替换(“/”,“”)。替换(“=”,”);
}
我得到了一个错误:

Google.api.Requests.RequestError

权限不足[403]

错误[消息[权限不足]位置[-] 原因[权限不足]域[全局]

真的非常感谢您,它与更改配合得非常好:


将“用户”更改为“XXXX”

web应用程序应该如何打开GMail网页?因为控制台应用程序自动将“用户”更改为“XXXX”试着给我重新授权,如果可以的话请告诉我it@CamiloTerevinto.net客户端库自动处理打开google身份验证窗口的操作。至于需要打开gmail网页的发送,则不需要。这是gmail api。@DaImTo在控制台(即用户应用程序)上有意义,而不是在托管应用程序上e不应该有任何用户登录后看到新的Chrome窗口弹出。。。