#c# #.net #google-cloud-platform #gmail-api
#c# #.net #google-облачная платформа #gmail-api
Вопрос:
Я новичок в Google cloud / gmail API. В c # я хотел бы использовать их gmail api для:
- вход в Google Cloud — работает
- чтение элементов списка — работает
- отправка электронной почты — не работает
API на шаге 3 возвращает недостаточные области запроса (403) Я уверен, что я вошел в свою учетную запись cloud: строка кода, которую я подозреваю больше всего, это:
static string[] Scopes = { GmailService.Scope.GmailAddonsCurrentActionCompose, GmailService.Scope.GmailAddonsCurrentMessageAction };
Я получаю эту ошибку
Request had insufficient authentication scopes. [403]
Errors [
Message[Insufficient Permission] Location[ - ] Reason[insufficientPermissions] Domain[global]
]
// Code
class Program
{
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/gmail-dotnet-quickstart.json
static string[] Scopes = { GmailService.Scope.GmailAddonsCurrentActionCompose, GmailService.Scope.GmailAddonsCurrentMessageAction };
static string ApplicationName = "Gmail API .NET Quickstart";
static void Main(string[] args)
{
UserCredential credential;
using (var stream =
new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
{
// The file token.json stores the user's access and refresh tokens, and is created
// automatically when the authorization flow completes for the first time.
string credPath = "token.json";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Resu<
Console.WriteLine("Credential file saved to: " credPath);
}
// Create Gmail API service.
var service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
// Define parameters of request.
UsersResource.LabelsResource.ListRequest request = service.Users.Labels.List("me");
// List labels.
IList<Label> labels = request.Execute().Labels;
Console.WriteLine("Labels:");
if (labels != null amp;amp; labels.Count > 0)
{
foreach (var labelItem in labels)
{
Console.WriteLine("{0}", labelItem.Name);
}
}
else
{
Console.WriteLine("No labels found.");
}
string plainText = "Body Test";
var newMsg = new Google.Apis.Gmail.v1.Data.Message();
newMsg.Raw = Program.Base64UrlEncode(plainText.ToString());
try
{
service.Users.Messages.Send(newMsg, "me").Execute();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
/*
{"Google.Apis.Requests.RequestErrorrnRequest had insufficient authentication scopes.
[403]rnErrors [rntMessage[Insufficient Permission] Location[ - ] Reason[insufficientPermissions] Domain[global]rn]rn"} Google.GoogleApiException
*/
Console.Read();
}
public static string Base64UrlEncode(string input)
{
var inputBytes = System.Text.Encoding.UTF8.GetBytes(input);
return Convert.ToBase64String(inputBytes).Replace(" ", "-").Replace("/", "_").Replace("=", "");
}
}
// Вывод сверху
Credential file saved to: token.json
Labels:
CHAT
SENT
INBOX
IMPORTANT
TRASH
DRAFT
SPAM
CATEGORY_FORUMS
CATEGORY_UPDATES
CATEGORY_PERSONAL
CATEGORY_PROMOTIONS
CATEGORY_SOCIAL
STARRED
UNREAD
Sent Messages
Pa0
P
Insurance
Junk E-mail
Licenses
Notes
Personal
Receipts
Travel
Work
Tickets
**Google.Apis.Requests.RequestError
Request had insufficient authentication scopes. [403]
Errors [
Message[Insufficient Permission] Location[ - ] Reason[insufficientPermissions] Domain[global]
]**
Комментарии:
1. я добиваюсь некоторого прогресса. Этот код основан на быстром запуске. Мне нужно было изменить сам идентификатор облака с помощью облачной консоли, чтобы разрешить отправку почты. Теперь я получаю следующую ошибку… Я обновлю эту проблему с результатами, когда в конечном итоге выиграю
Ответ №1:
Хорошо: я решил использовать файл учетных данных, предоставленный GMAIL.API и поместите в однострочную переменную среды и выполните преобразование JSON в GoogleClientSecrets:
private static GoogleClientSecrets GetSecretsFromEnvironment()
{
var environmentConfiguration = new ConfigurationBuilder()
.AddEnvironmentVariables()
.Build();
var secretsEnv = environmentConfiguration["GoogleSecrets"];
var secrets = JsonConvert.DeserializeObject<GoogleClientSecrets>(secretsEnv);
return secrets;
}
appsettings.json
{
"MailSettings": {
"account": "mark.d.wardell@gmail.com",
"subject": "Please Confirm Account",
"from": "mark.d.wardell@gmail.com",
"HTML": "<b>Hello {0}</b>"
}
}
credentials.json, предоставленный облачной консолью Google. Я превратил строку в одну строку и добавил в EnvironmentVariable
using Google.Apis.Auth.OAuth2;
using Google.Apis.Gmail.v1;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Threading;
using System.Threading.Tasks;
namespace SendMail
{
class Program
{
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/gmail-dotnet-quickstart.json
static string[] Scopes = { GmailService.Scope.GmailAddonsCurrentActionCompose, GmailService.Scope.GmailAddonsCurrentMessageAction, GmailService.Scope.GmailSend };
static string ApplicationName = "Restful Resting Place";
static async Task Main(params string[] args)
{
try
{
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
Dictionary<string, string> MailSettings;
MailSettings = configuration.GetSection("MailSettings").GetChildren().ToDictionary(x => x.Key, x => x.Value);
MailSettings.Add("to", args[0]);
MailSettings.Add("link", args[1]);
GoogleClientSecrets gSecrets = GetSecretsFromEnvironment();
string credPath = "token.json";
UserCredential gcredential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
gSecrets.Secrets,
Scopes,
MailSettings["account"],
CancellationToken.None,
new FileDataStore(credPath, true));
var service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = gcredential,
ApplicationName = ApplicationName,
});
SendItTwo(service, MailSettings);
Console.WriteLine()
}catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static GoogleClientSecrets GetSecretsFromEnvironment()
{
var environmentConfiguration = new ConfigurationBuilder()
.AddEnvironmentVariables()
.Build();
var secretsEnv = environmentConfiguration["GoogleSecrets"];
var secrets = JsonConvert.DeserializeObject<GoogleClientSecrets>(secretsEnv);
return secrets;
}
public static void SendItTwo(GmailService gmail, Dictionary<string,string> dict)
{
MailMessage mailmsg = new MailMessage();
{
mailmsg.Subject = dict["subject"];
mailmsg.Body = string.Format(dict["HTML"],dict["link"]);
mailmsg.From = new MailAddress(dict["from"]);
mailmsg.To.Add(new MailAddress(dict["to"]));
mailmsg.IsBodyHtml = true;
}
////add attachment if specified
if (dict.ContainsKey("attachement"))
{
if (File.Exists(dict["attachment"]))
{
Attachment data = new Attachment(dict["attachment"]);
mailmsg.Attachments.Add(data);
}else
{
Console.WriteLine("Error: Invalid Attachemnt");
}
}
//Make mail message a Mime message
MimeKit.MimeMessage mimemessage = MimeKit.MimeMessage.CreateFromMailMessage(mailmsg);
Google.Apis.Gmail.v1.Data.Message finalmessage = new Google.Apis.Gmail.v1.Data.Message();
finalmessage.Raw = Base64UrlEncode(mimemessage.ToString());
var result = gmail.Users.Messages.Send(finalmessage, "me").Execute();
}
public static string Base64UrlEncode(string input)
{
var inputBytes = System.Text.Encoding.UTF8.GetBytes(input);
return Convert.ToBase64String(inputBytes).Replace(" ", "-").Replace("/", "_").Replace("=", "");
}
}
}