Olá pessoal, estou tentando criar uma aplicação na qual o eu consiga upar um arquivo txt para o google drive. Peguei um exemplo no google developer que funciona, porem funciona postando no drive de quem está logado.
Alterei o projeto para ao invés do drive que está ligado, mandar para o drive do exemplo.
Exemplo: Estou logado no Drive "menezes@gmail.com" e o meu projeto pela as certificações do Drive "teste@gmail.com".
O exemplo que eu peguei no site do google faz o upload para o Drive de "menezes@gmail.com", porem o que eu quero é que não interessa que estiver logado o sistema mande para o drive de "teste@gmail.com".
Minha alteração fez o projeto consumir o processo(consigo visualizar no próprio site do google) porem ele não manda para lugar nenhum e também não da erro.
Meu Código (tirei as informações de certificação).
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using Google;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v2;
using Google.Apis.Drive.v2.Data;
using Google.Apis.Services;
using Google.Apis.Util;
namespace GoogleDriveSamples
{
class DriveCommandLineSample
{
static void Main(string[] args)
{
Console.WriteLine("Informe o nome do arquivo!");
string Nome = Console.ReadLine();
File body = new File();
body.Title = Nome + ".txt";
body.Description = "A test document";
body.MimeType = "text/plain";
byte[] byteArray = System.IO.File.ReadAllBytes("document.txt");
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
DriveService service = new Autentificacao().BuildService();
FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "text/plain");
request.Upload();
File file = request.ResponseBody;
Console.WriteLine("File id: " + file.Id);
Console.WriteLine("Press Enter to end this process.");
Console.ReadLine();
}
}
public class Autentificacao
{
private const string SERVICE_ACCOUNT_EMAIL = --SERVICE_ACCOUNT_EMAIL--;
private const string SERVICE_ACCOUNT_PKCS12_FILE_PATH = --SERVICE_ACCOUNT_PKCS12_FILE_PATH--;
public DriveService BuildService()
{
string serviceAccountEmail = SERVICE_ACCOUNT_EMAIL;
string Caminho = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
var certificate = new X509Certificate2(Caminho + SERVICE_ACCOUNT_PKCS12_FILE_PATH, "notasecret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { DriveService.Scope.Drive }
}.FromCertificate(certificate));
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Drive API Sample",
});
return service;
}
}
}
Desde já agradeço a ajuda.