Windows服务C#在安装后无法使用Google Drive API上载文件

Windows服务C#在安装后无法使用Google Drive API上载文件,windows,service,visual-studio-2013,google-drive-api,Windows,Service,Visual Studio 2013,Google Drive Api,以下是上载到驱动器文件夹的代码。这将由windows服务中的线程调用。在调试模式下运行时,一切正常。但一旦我使用VisualStudio安装程序安装它,它就无法真正工作。当我尝试使用事件日志文件调试它时,函数一被调用,它就会中断 using System; using System.Threading; using System.Threading.Tasks; using Google; using Google.Apis.Auth.OAuth2; using Google.Apis.Driv

以下是上载到驱动器文件夹的代码。这将由windows服务中的线程调用。在调试模式下运行时,一切正常。但一旦我使用VisualStudio安装程序安装它,它就无法真正工作。当我尝试使用事件日志文件调试它时,函数一被调用,它就会中断

using System;
using System.Threading;
using System.Threading.Tasks;
using Google;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v2;
using Google.Apis.Drive.v2.Data;
using Google.Apis.Services;


namespace RedHillsServiceV1._0
{
    class AjCloudConnect
    {
        public void cloudStart()
        {

            try
            { 
            UserCredential credential =                     GoogleWebAuthorizationBroker.AuthorizeAsync(
                new ClientSecrets
                {
                    ClientId = "181014074404-                  
                    ClientSecret = "3vgzlLoEF_",
                },
                new[] { DriveService.Scope.Drive },
                "user",
            CancellationToken.None).Result;

        // Create the service.
        var service = new DriveService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "PseudoGPService",
        });

        File body = new File();
        body.Title = "log" + DateTime.Now.ToString("h:mm:ss tt");
        body.Description = "log file";
        body.MimeType = "text/plain";

        byte[] byteArray = System.IO.File.ReadAllBytes(AppDomain.CurrentDomain.BaseDirectory + "log.txt");
        System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);

        FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "text/plain");
        request.Upload();

        File file = request.ResponseBody;
        Console.WriteLine("----File id: " + file.Id+" has been successfully uploaded at"+DateTime.Now.ToString("h:mm:ss tt")+"-----");
        //Console.WriteLine("Press Enter to end this process.");
        //Console.ReadLine();
       }
        catch(Exception clEx)
        {
            Console.WriteLine(clEx.ToString());
        }
    }


}
}

这也是服务中线程的代码:

    public void callUpThreadStart()
    {
        try
        { 
        //Thread.Sleep(4500000);
        while(true)
        {
            Thread.Sleep(60000);
            //Thread.Sleep(900000);
            Console.WriteLine("----Starting Upload Thread----");
            //Service1 UpSer = new Service1(); 
            //Console.WriteLine(isLogging);
            int d;
            if (InternetGetConnectedState(out d, 0))
            {
                if (new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log.txt").Length == 0)
                {
                    Console.WriteLine("----The File is Empty----");
                }
                else
                {
                    Console.WriteLine("----Internet Connection Detected----");

                    if (isLogging)
                    {
                        isLogging = false;
                    }
                    Thread.Sleep(3000);

                    AjCloudConnect ajcc = new AjCloudConnect();
                    ajcc.cloudStart();
                    flag = 0;
                    clearText();
                    isLogging = true;

                }

            }
            else
            {
                Console.WriteLine("----No Internet Connection Detected----");

                flag = 1;
            }

        }

        }
        catch (Exception e)
        {
            Console.WriteLine("Generic Exception", e);
            upThreadRestart();

        }


    }

最后,我创建了作为本地系统的服务,在调试模式下运行时,程序将第一次使用浏览器请求身份验证,但在安装后运行时不会这样做。请帮忙,如果我没有提供任何信息,我很抱歉。

您没有保存身份验证。那么,是什么让您认为它不会再次要求身份验证呢?我真的不认为windows服务可以在每次启动时要求身份验证它有点违背了拥有windows服务的目的,不是吗?可能希望首先在初始请求中添加FileDataStore或iDataStore的其他实现,以保存身份验证

使用FileDataStore的示例:

UserCredential credential =                     GoogleWebAuthorizationBroker.AuthorizeAsync(
                new ClientSecrets
                {
                    ClientId = "181014074404-                  
                    ClientSecret = "3vgzlLoEF_",
                },
                new[] { DriveService.Scope.Drive },
                "user",
            CancellationToken.None
            , new FileDataStore("Daimto.Drive.Auth.Store")).Result;
有用的链接

  • Google Drive.net示例项目:
  • 谷歌硬盘.net上传教程:

  • 还是不能解决我的问题。另外,不添加新的filedatastore只意味着将使用默认的filedatastore。此外,来自google devs()的教程没有反映任何filedatastore实现。请给我提个建议。谢谢。实际上,不添加文件数据存储将意味着您的身份验证不会保存在任何位置,并且每次都会再次询问。FileDataStore是默认的例外方法,它再次保存到%appdata%,您的服务是否有权在其中写入?同样,windows服务器可能无法弹出请求身份验证的窗口,因此如果您不保存某个用户的初始身份验证,您的代码将无法工作。包含文件数据存储的官方文档此处有一些小更正:如果您不使用数据存储(例如使用),库将为您创建文件数据存储,正如您在实现中可以发现的那样:确切地说,peeleyal,问题不在于FileDataStore,这是我的观点,我得到了-1的答案。由于这个问题,我不得不改变我的架构,并使用sftp上传。