C# 将显式流上载文件到google drive
我有一个Angular 6应用程序,它使用.NETCore2.x作为后端 我让外部提供者为显式流工作(很多示例) 我使用显式流,因为我不想与web应用共享机密和客户端id 我有一个上传文件服务(domain.com/api/upload) 女巫很管用 现在,我想将文件保存到登录用户的google驱动器。 google项目拥有google drive api权限和所有 但我无法让驱动器api客户端使用驱动器api进行身份验证 控制器动作C# 将显式流上载文件到google drive,c#,.net,angular,C#,.net,Angular,我有一个Angular 6应用程序,它使用.NETCore2.x作为后端 我让外部提供者为显式流工作(很多示例) 我使用显式流,因为我不想与web应用共享机密和客户端id 我有一个上传文件服务(domain.com/api/upload) 女巫很管用 现在,我想将文件保存到登录用户的google驱动器。 google项目拥有google drive api权限和所有 但我无法让驱动器api客户端使用驱动器api进行身份验证 控制器动作 private async Task<IFileSto
private async Task<IFileStoreItem> SaveFile(IFormFile file, IFilePath path)
{
if (file == null) throw new Exception("File is null");
if (file.Length == 0) throw new Exception("File is empty");
if(path == null) throw new Exception("path is null");
IFileStoreItem item = file.Adapt<DocumentInfo>();
item.Container = path.Container;
item.Partition = path.Partition;
IFileStoreService fileStoreService = GetFileStoreService();
if (file.Length > 0)
{
using (Stream stream = new MemoryStream())
{
await file.CopyToAsync(stream);
item = await fileStoreService.Save(stream, item);
}
item.Link = Url.AbsoluteUri("DownloadFile", "File", new { container = item.Container, partition = item.Partition, id = item.Id });
}
return item;
}
private IFileStoreService GetFileStoreService()
{
IFileStoreService ret = null;
string uploads = Path.Combine(_environment.WebRootPath, "uploads");
DocumentStorageConfiguration config = new StorageConfiguration().GetStoragetSettings();
ret = new GoogleDriveService(User.Claims.Where(c => c.Type == JwtRegisteredClaimNames.UniqueName).SingleOrDefault()?.Value);
return ret;
}
驾驶服务
public class GoogleDriveService : IFileStoreService
{
DriveService _service;
UserCredential credential;
public GoogleDriveService(string user)
{
string[] scopes = new string[] {
DriveService.Scope.Drive,
DriveService.Scope.DriveFile};
GoogleConfiguration config = new AuthentificationConfiguration().GetGoogleSettings();
var secrets = new ClientSecrets
{
ClientId = config.ClientId,
ClientSecret = config.ClientSecret
};
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(secrets, scopes, user, CancellationToken.None).Result;
_service = new DriveService(new Google.Apis.Services.BaseClientService.Initializer() {
HttpClientInitializer = credential
});
}
public async Task<IFileStoreItem> Save(System.IO.Stream stream, IFileStoreItem item)
{
File body = new File
{
Name = item.Name,
MimeType = item.ContentType,
Parents = new List<string>() { "Documents" }
};
FilesResource.CreateMediaUpload request = new FilesResource.CreateMediaUpload(_service,body,stream,item.ContentType);
IUploadProgress progress = await request.UploadAsync();
item.Id = body.Id;
return item;
}
}
公共类GoogleDriveService:IFileStoreService
{
驾驶服务(DriveService);;
用户凭证;
公共GoogleDriveService(字符串用户)
{
字符串[]范围=新字符串[]{
DriveService.Scope.Drive,
DriveService.Scope.DriveFile};
GoogleConfiguration配置=新建AuthentificationConfiguration().GetGoogleSettings();
var secrets=newclientsecrets
{
ClientId=config.ClientId,
ClientSecret=config.ClientSecret
};
credential=GoogleWebAuthorizationBroker.AuthorizationAsync(机密、作用域、用户、CancellationToken.None)。结果;
_service=newdriveservice(新的Google.api.Services.BaseClientService.Initializer(){
HttpClientInitializer=凭证
});
}
公共异步任务保存(System.IO.Stream,IFileStoreItem)
{
文件体=新文件
{
名称=项。名称,
MimeType=item.ContentType,
Parents=新列表(){“文档”}
};
FileResource.CreateMediaUpload请求=新建FileResource.CreateMediaUpload(_服务、正文、流、项.ContentType);
IUploadProgress progress=wait request.UploadAsync();
item.Id=body.Id;
退货项目;
}
}
我得到的错误是坏网关502
问题是我是否可以根据当前登录的用户(witch已经是google用户)使用google drive进行身份验证