【问题标题】:How to implement Google Drive API with .Net Core MVC Identity如何使用 .Net Core MVC Identity 实现 Google Drive API
【发布时间】:2020-01-17 03:26:20
【问题描述】:
我一直在尝试让 Google Drive API 与 .Net Core Identity(内置 google Oauth2)一起工作
我已尝试遵循 this 教程。但它不适用于 .Net Core。我将如何使用 .Net Core Identity 在他们登录后访问谷歌驱动器?
【问题讨论】:
标签:
asp.net-core
.net-core
google-drive-api
asp.net-core-mvc
asp.net-identity
【解决方案1】:
您需要您的 Google 控制台应用启用 Drive API,然后您需要在 startup.cs 中的身份配置中添加正确的范围。这将确保当您的用户登录时,他们获得分配给其登录令牌的正确范围。
例如,如果您想从云端硬盘读取文件和/或文件 META,您可能有:
https://www.googleapis.com/auth/drive.readonly
范围见这里:https://developers.google.com/drive/api/v2/about-auth
这是一个示例:
services.AddAuthentication().AddGoogle(googleOptions =>
{
googleOptions.ClientId = "YOUR_CLIENT_ID";
googleOptions.ClientSecret = "YOUR_CLIENT_SECRET";
googleOptions.Scope.Add("https://www.googleapis.com/auth/drive.readonly");
googleOptions.SaveTokens = true;
...
});
从这里,您的用户将在登录时获得一个 AccessToken 和 RefreshToken。您可以使用它(连同他们的电子邮件地址)访问他们的 Google Drive。
我有一项服务用于向 API 发出各种请求。