我在课堂上使用了以下内容:
public class AppFlowMetaData : FlowMetadata
{
private static readonly IAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = new ClientSecrets
{
ClientId = "nnnnnnnnnnnnn.apps.googleusercontent.com",
ClientSecret = "nnnnnnn-nnnnnnnnnnnnnnn"
},
Scopes = new[] { CalendarService.Scope.Calendar },
DataStore = new FileDataStore("Calendar.Api.Auth.Store")
});
public override string GetUserId(System.Web.Mvc.Controller controller)
{
var user = controller.Session["user"];
if (user == null)
{
user = Guid.NewGuid();
controller.Session["user"] = user;
}
return user.ToString();
}
public override IAuthorizationCodeFlow Flow
{
get { return flow; }
}
}
以及控制器上的以下内容:
public async Task<ActionResult> TestAsync(CancellationToken token)
{
var result = await new AuthorizationCodeMvcApp(this, new AppFlowMetaData()).AuthorizeAsync(token);
if(result.Credential != null)
{
var service = new CalendarService(new BaseClientService.Initializer
{
HttpClientInitializer = result.Credential,
ApplicationName = "Calendar Test"
});
IList<CalendarListEntry> results = service.CalendarList.List().Execute().Items;
List<Event> model = new List<Event>();
foreach (var calendar in results)
{
Google.Apis.Calendar.v3.EventsResource.ListRequest request = service.Events.List(calendar.Id);
var events = request.Execute().Items;
foreach (var e in events)
{
model.Add(e);
}
}
return View(model);
}
else
{
return new RedirectResult(result.RedirectUri);
}
}
这很好用,我希望能够使用 gmail 使用相同的流程发送邮件,但我在 Google Api 文档中找不到范围或任何详细信息(坦率地说,这很糟糕)基于相同的流程来执行此操作。我在其他地方看到过使用 Imap 的 3rd 方库,但这依赖于传递我不想做的用户名/密码。
任何人都可以帮我更改以上内容以用于 Gmail 吗?