I have an Angular 6 app that uses .net Core 2.x as backend

I have the External provider working for the explicit flow (lot of samples) I use explicit flow since I do not whant to share the secret and client id with the web app.

I have a upload file service (domain.com/api/upload) Witch works fine

Now I would like to save the file to a logged in user's google drive. The google project has the google drive api permissions and all

But I can't get the drive api client to authenticate with the drive api

Controller action

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;
}

Helper function

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;
}

Drive service

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;
    }
}

The error I am getting is bad gateway 502

Question is if I can authenticate with google drive based on the currently logged in user (witch already is a google user)

0 Answers