使用google drive API

时间:2018-04-25 17:46:21

标签: c#

我尝试使用client_id.json登录google驱动器来启动应用程序,但是当我启动程序时,它会说:"文件&#C; \ Windows \ system32 \ client_id.json'无法找到。",此文件位于程序文件夹中。我不知道为什么它试图获取system32。

public Google.Apis.Auth.OAuth2.UserCredential Autenticar()
{
    string caminhoarquivo = @"c:\ProComercio\";

    //Se o diretório não existir, ele irá criar esse novo diretório
    if (!Directory.Exists(caminhoarquivo))
    {
        //Criamos um com o nome folder
        Directory.CreateDirectory(caminhoarquivo);
    }
    string fileName = "client_id.json";
    string sourcePath = System.IO.Directory.GetCurrentDirectory();
    string targetPath = @"c:\ProComercio\";
    string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
    string destFile = System.IO.Path.Combine(targetPath, fileName);
    System.IO.File.Copy(sourceFile, destFile, true);
    using (var ctx = new Entities())
    {
        //Busca o email na tabela parametros
        var resultado = ctx.Clientes.SqlQuery("Select * from tblParametros").FirstOrDefault();

        Google.Apis.Auth.OAuth2.UserCredential credenciais;

        using (var stream = new System.IO.FileStream(caminhoarquivo + "client_id.json", System.IO.FileMode.Open, System.IO.FileAccess.Read))
        {
            var diretorioAtual = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            var diretorioCredenciais = System.IO.Path.Combine(diretorioAtual, "credential");

            credenciais = Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.AuthorizeAsync(
                    Google.Apis.Auth.OAuth2.GoogleClientSecrets.Load(stream).Secrets,
                    new[] { Google.Apis.Drive.v3.DriveService.Scope.Drive },
                    resultado.mailuser,
                    System.Threading.CancellationToken.None,
                    new Google.Apis.Util.Store.FileDataStore(diretorioCredenciais, true)).Result;
        }
        return credenciais;
    }
}

0 个答案:

没有答案