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

时间:2018-05-29 21:54:10

标签: c# google-api google-oauth gmail-api google-api-dotnet-client

我尝试在我的网络应用中发送电子邮件(目前在localhost中),我使用我使用的下一个代码(及其工作)作为控制台应用,它应该打开一个网页询问对于谷歌帐户:

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("=", "");
}

我收到错误:

  

Google.Apis.Requests.RequestError

     

许可不足[403]

     

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

1 个答案:

答案 0 :(得分:0)

谢谢DalmTo确实它对改变非常好:

将“用户”更改为“XXXX”