Windows Service C# to Upload Files To Google Drive API Not Working After Installing But it works fine on CONSOLE APPLICATION

  asp.net, c++, drive, service, windows

I’m using this code to upload some files to GOOGLE DRIVE .

It works fine when i test it with a console application .
Google drive asks for authentication the first time and work correctly after that .

public class RepositoryGoogleDrive : IDisposable
{
    public void Dispose() { }
    static string[] Scopes = { DriveService.Scope.Drive,
                   DriveService.Scope.DriveAppdata,
                   DriveService.Scope.DriveFile,
                   DriveService.Scope.DriveMetadataReadonly,
                   DriveService.Scope.DriveReadonly,
                   DriveService.Scope.DriveScripts };
  
    public static DriveService Execution( string ApplicationName , string credentials)
    {
        UserCredential credential;
       // credentials = "C:/workspace/datawarehous/TMP/credentials.json";
        using (var stream =
            new FileStream(credentials, FileMode.Open, FileAccess.Read))
        {

            string credPath = "token.json";
            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                Scopes,
                "XXXX",
                CancellationToken.None,
                new FileDataStore(credPath, true)).Result;
        }
        // Create Drive API service.
        var service = new DriveService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = ApplicationName,
        });

        // Define parameters of request.
        FilesResource.ListRequest listRequest = service.Files.List();
        listRequest.PageSize = 10;
        listRequest.Fields = "nextPageToken, files(id, name)";

        // List files.
        IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute()
            .Files;
        return service;
    }

    public void CreateFolderOnDrive(string Folder_Name,string ApplicationName, string credentials)
    {
        Google.Apis.Drive.v3.DriveService service = Execution(ApplicationName, credentials);

        Google.Apis.Drive.v3.Data.File FileMetaData = new
        Google.Apis.Drive.v3.Data.File();
        FileMetaData.Name = Folder_Name;
        FileMetaData.MimeType = "application/vnd.google-apps.folder";

        Google.Apis.Drive.v3.FilesResource.CreateRequest request;

        request = service.Files.Create(FileMetaData);
        request.Fields = "id";
        var file = request.Execute();
    }

    public  bool Upload(string fileName , string ApplicationName, string credentials)
    {
            DriveService service = Execution(ApplicationName, credentials);
            FilesResource.CreateMediaUpload request; 
            using (var stream = new FileStream(fileName,
                                    System.IO.FileMode.Open))
            {
                var name = Path.GetFileName(stream.Name);

                var fileMetadata1 = new File()
                {

                    Name = name
                };
                request = service.Files.Create(
                    fileMetadata1, stream, "");
                request.Fields = "id";
                request.Upload();
            }
            var file = request.ResponseBody;
        return true;
    }

 }

MY PROBLEM is now i want to execute this code throws Windows Service that is installed on my laptop but !!! nothing happen even i tried to log the exception in a .txt file even it never asked for authentication.

Source: Windows Questions

LEAVE A COMMENT