I used code for upload for console application.
its working but for web client api.. its not working for web application ...
I got null for Responsebody=null.
here is my code:
ClientSecrets secrets = new ClientSecrets()
{
ClientId = parameters.ClientId,
ClientSecret = parameters.ClientSecret
};
var token = new TokenResponse { RefreshToken = parameters.RefreshToken };
var credentials = new UserCredential(new GoogleAuthorizationCodeFlow(
new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = secrets
}),
Environment.UserName,
token);
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credentials,
ApplicationName = "Google Drive",
});
if (FileUpload1.HasFile)
{
var filename = FileUpload1.FileName;
var filepath = FileUpload1.PostedFile.FileName;
Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File();
body.Title = filename;
body.Description = "Documents";
body.MimeType =GetMimeType(filename);
body.Parents = new List<ParentReference>() { new ParentReference() { Id = "0BySorYm3rcaNeXV2Y0M5bXh1WjA" } };
//byte[] array = System.IO.File.ReadAllBytes(filepath);
MemoryStream stream = new MemoryStream(FileUpload1.FileBytes);
FilesResource.InsertMediaUpload fileuploadrequest = new FilesResource.InsertMediaUpload(service, body, stream, GetMimeType(filename));
//var name=MediaFileSource.GetContentTypeForFileName(filename);
//fileuploadrequest.Convert = true;
//fileuploadrequest.OauthToken=parameters.AccessToken;
fileuploadrequest.Upload();
//geting null using this code and also not getting error for upload
Google.Apis.Drive.v2.Data.File response = fileuploadrequest.ResponseBody;
Related
Using a service account, how to generate a google meet link by creating an event using google calendar API. I have my authorization all working fine. But I don't have any idea what. Here is my code, creating events successfully but not generating a google meet link. The response shows event creation details but nothing about google-meet. I'd really appreciate some help.
string calendarId = #"calendar-id";
string[] Scopes = { CalendarService.Scope.Calendar };
ServiceAccountCredential credential;
string path = Server.MapPath("~/file.json");
using (var stream =
new FileStream(path, FileMode.Open, FileAccess.Read))
{
var confg = Google.Apis.Json.NewtonsoftJsonSerializer.Instance.Deserialize<JsonCredentialParameters>(stream);
credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(confg.ClientEmail)
{
Scopes = Scopes
}.FromPrivateKey(confg.PrivateKey));
}
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Calendar API Sample",
});
var calendar = service.Calendars.Get(calendarId).Execute();
string token = credential.Token.AccessToken;
// Define parameters of request.
EventsResource.ListRequest listRequest = service.Events.List(calendarId);
listRequest.TimeMin = DateTime.Now;
listRequest.ShowDeleted = false;
listRequest.SingleEvents = true;
listRequest.MaxResults = 10;
listRequest.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;
Event newEvent = new Event();
{
DateTime start = Convert.ToDateTime("2021-06-18T05:00:02.000Z");
DateTime end = Convert.ToDateTime("2021-06-18T06:00:02.000Z");
newEvent.Summary = "summary".ToString();
newEvent.Description = "description".ToString();
newEvent.Status = "confirmed";
newEvent.Creator = new Event.CreatorData
{
Email = " email-id",
Self = true
};
newEvent.Organizer = new Event.OrganizerData
{
Email = "email-id",
Self = true
};
newEvent.Start = new EventDateTime
{
DateTime = start,
TimeZone = "Asia/Kolkata"
};
newEvent.End = new EventDateTime
{
DateTime = end,
TimeZone = "Asia/Kolkata"
};
newEvent.HangoutLink = "";
newEvent.ConferenceData = new ConferenceData()
{
ConferenceSolution = new ConferenceSolution
{
Key = new ConferenceSolutionKey
{
Type = "hangoutsMeet"
}
},
CreateRequest = new CreateConferenceRequest()
{
ConferenceSolutionKey = new ConferenceSolutionKey()
{
Type = "hangoutsMeet"
},
RequestId = "some-random-string"
},
};
//newEvent.Attendees = new List<EventAttendee>()
//{
// new EventAttendee() { Email = "" }
//};
};
RestClient restClient = new RestClient();
RestRequest request = new RestRequest();
var serilaizeJson = JsonConvert.SerializeObject(newEvent, Formatting.None,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
request.AddQueryParameter("key", "api-key");
request.AddHeader("Authorization", "Bearer " + token);
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", serilaizeJson, ParameterType.RequestBody);
restClient.BaseUrl = new System.Uri("https://www.googleapis.com/calendar/v3/calendars/calendar-id/events?conferenceDataVersion=1");
var response = restClient.Post(request);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
dynamic returnFile = JObject.Parse(response.Content);
string link = returnFile["hangoutLink"];
}
Solution
Add the following object to the request body
conferenceData: {
createRequest: {
requestId: "sample123",
conferenceSolutionKey: { type: "hangoutsMeet" },
},
}
I am trying to upload a file into my drive but the response is always null
i created a developer account then i got my api key and my client secret and id
but i still think that the problem is in the authentication step
static string ApplicationName = "WebApi";
private static readonly IAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = new ClientSecrets
{
ClientId = "my_client_ID",
ClientSecret = "my_client_secret",
},
Scopes = new[] { DriveService.Scope.Drive,
DriveService.Scope.DriveAppdata,
DriveService.Scope.DriveFile,
DriveService.Scope.DriveMetadataReadonly,
DriveService.Scope.DriveReadonly,
DriveService.Scope.DriveScripts},
DataStore = new FileDataStore("Drive.Api.Auth.Store")
});
public MainWindow()
{
InitializeComponent();
var service = new DriveService(new Google.Apis.Services.BaseClientService.Initializer()
{
ApiKey = "my_api_key",
HttpClientInitializer = flow as UserCredential,
ApplicationName = ApplicationName
});
insertFile(service, "file_name.mdf", "", "", "application/octet-stream", "file_name.mdf");
}
private static Google.Apis.Drive.v2.Data.File insertFile(DriveService service, String title, String description, String parentId, String mimeType, String filename)
{
// File's metadata
Google.Apis.Drive.v2.Data.File body = new
Google.Apis.Drive.v2.Data.File();
body.Title = title;
body.Description = description;
body.MimeType = mimeType;
body.Parents = body.Parents = new List<ParentReference>();
// Set the parent folder.
if (!String.IsNullOrEmpty(parentId))
{
body.Parents = new List<ParentReference>()
{new ParentReference() {Id = parentId}};
}
// File's content.
byte[] byteArray = System.IO.File.ReadAllBytes(filename);
MemoryStream stream = new MemoryStream(byteArray);
try
{
FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream,mimeType );
request.Upload();
Google.Apis.Drive.v2.Data.File file = request.ResponseBody;
// Uncomment the following line to print the File ID.
Console.WriteLine("File ID: " + file.Id);
return file;
}
catch (Exception e)
{
throw e;
}
}
i am creating the service in the main window the i am trying to insert
the file in the insert file function
i am getting a null response please help..
I have successfully uploaded image file in google drive using C#. But i am facing problem in uploading mp3 file on drive.
How I upload mp3 file in Google drive using C# in MVC?
this my Code for save file:
public ServieResponse SaveFileOnGoogleDrive(string url)
{
//string url = string.Empty; ; ;
//string[] scopes = new string[] { DriveService.Scope.Drive,
// DriveService.Scope.DriveFile};
string[] scopes = new string[] { DriveService.Scope.Drive,
DriveService.Scope.DriveAppdata,
//DriveService.Scope.DriveAppsReadonly,
DriveService.Scope.DriveFile,
DriveService.Scope.DriveMetadataReadonly,
DriveService.Scope.DriveReadonly,
DriveService.Scope.DriveScripts };
var clientId = "xxxxxx"; // From https://console.developers.google.com
var clientSecret = "xxxxxxx";
// From https://console.developers.google.com
// here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets
{
ClientId = clientId,
ClientSecret = clientSecret
},
scopes,
Environment.UserName,
CancellationToken.None,
new FileDataStore("MyAppsToken")).Result;
//Once consent is recieved, your token will be stored locally on the AppData directory, so that next time you wont be prompted for consent.
DriveService service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "CTM",
});
string filePath = WebConfigurationManager.AppSettings["filPath"];
filePath = HttpContext.Current.Server.MapPath(filePath) + url;
uploadFile(service, filePath, "", "");
// service.HttpClient.Timeout = TimeSpan.FromMinutes(100);
//Long Operations like file uploads might timeout. 100 is just precautionary value, can be set to any reasonable value depending on what you use your service for.
//return Json(new { result = "abc" }, JsonRequestBehavior.AllowGet);
ServieResponse ob = new ServieResponse();
ob.ResponseMsg = "Success";
return ob;
}
Now my code is working fine. Updated code is:
public ServieResponse SaveFileOnGoogleDrive(string url)
{
//string url = string.Empty; ; ;
//string[] scopes = new string[] { DriveService.Scope.Drive,
// DriveService.Scope.DriveFile};
string[] scopes = new string[] { DriveService.Scope.Drive,
DriveService.Scope.DriveAppdata,
//DriveService.Scope.DriveAppsReadonly,
DriveService.Scope.DriveFile,
DriveService.Scope.DriveMetadataReadonly,
DriveService.Scope.DriveReadonly,
DriveService.Scope.DriveScripts };
var clientId = "65675933715-poet7f7dhjhrmccmalhb41pltho7tusr.apps.googleusercontent.com"; // From https://console.developers.google.com
var clientSecret = "D8USyz3Pf82wOMi6l2pJehjx";
// From https://console.developers.google.com
// here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets
{
ClientId = clientId,
ClientSecret = clientSecret
},
scopes,
Environment.UserName,
CancellationToken.None,
new FileDataStore("MyAppsToken")).Result;
//Once consent is recieved, your token will be stored locally on the AppData directory, so that next time you wont be prompted for consent.
DriveService service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "CTM",
});
string filePath = WebConfigurationManager.AppSettings["filPath"];
filePath = HttpContext.Current.Server.MapPath(filePath) + url;
uploadFile(service, filePath);
ServieResponse ob = new ServieResponse();
ob.ResponseMsg = "Success";
return ob;
}
public static void uploadFile(DriveService _service, string _uploadFile)
{
if (System.IO.File.Exists(_uploadFile))
{
var body = new Google.Apis.Drive.v3.Data.File();
//File body = new File();
body.Name = System.IO.Path.GetFileName(_uploadFile);
//body.Description = _descrp;
body.MimeType = GetMimeType(_uploadFile);
// body.Parents = new List<ParentReference>() { new ParentReference() { Id = _parent } };
FilesResource.CreateMediaUpload request;
try
{
using (var stream = new System.IO.FileStream(_uploadFile, System.IO.FileMode.Open))
{
request = _service.Files.Create(body, stream, body.MimeType);
request.Fields = "id";
request.Upload();
}
var file = request.ResponseBody;
var fili = file.Id;
}
catch (Exception e)
{
}
}
else
{
}
}
private static string GetMimeType(string fileName)
{
string mimeType = "application/unknown";
string ext = System.IO.Path.GetExtension(fileName).ToLower();
Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
if (regKey != null && regKey.GetValue("Content Type") != null)
mimeType = regKey.GetValue("Content Type").ToString();
return mimeType;
}
I want to know how to create an instance of Google BaseClientService.Initializer. I need to use the BaseClientService.Initializer to create the GmailService instance, and then use this GmailService instance to create an UsersResource instance.
Google.Apis.Services.BaseClientService.Initializer initializer = new BaseClientService.Initializer();
initializer.ApiKey = "MyApiKey";
initializer.ApplicationName = "MyProject";
initializer.DefaultExponentialBackOffPolicy = Google.Apis.Http.ExponentialBackOffPolicy.None;
Google.Apis.Gmail.v1.GmailService gmailService = new GmailService(initializer);
Google.Apis.Gmail.v1.UsersResource usersResource = new UsersResource(gmailService);
UsersResource.MessagesResource messagesResource = usersResource.Messages;
First go to the link bellow and open an application in your google account.
once you run the code you'll get exception for the first time, given you an address to go and enable the API in your google account.
https://console.developers.google.com/apis/dashboard?project=super-pharm-log-notifications
static string[] Scopes = { GmailService.Scope.GmailCompose, GmailService.Scope.GmailSend, GmailService.Scope.GmailInsert };
static private BaseClientService.Initializer initializeGmailAccountServices()
{
string appRoot = null;
if (System.Web.HttpContext.Current == null)
{
appRoot = Environment.CurrentDirectory;
}
else
{
appRoot = System.Web.HttpContext.Current.Server.MapPath(#"~\");
}
string path = Path.Combine(appRoot, DBConstants.GMAIL_CREDENTIALS_PATH);
UserCredential credential;
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
ClientSecrets secrets = GoogleClientSecrets.Load(stream).Secrets;
var t = GoogleWebAuthorizationBroker.AuthorizeAsync(secrets, Scopes, "user", CancellationToken.None);
t.Wait();
credential = t.Result;
}
return= new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "your application name",
};
}
private static string Base64UrlEncode(string input)
{
var inputBytes = System.Text.Encoding.UTF8.GetBytes(input);
// Special "url-safe" base64 encode.
return Convert.ToBase64String(inputBytes)
.Replace('+', '-')
.Replace('/', '_')
.Replace("=", "");
}
HelloI'm am currently trying to create a file under a specific user account.The user account is located within my Google Domain.For Oauth im using a service account.
The DriveService()
private static DriveService Service()
{
var certificate = new X509Certificate2(Constants.P12, "notasecret", X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(Constants.ServiceAccountEmail)
{
Scopes = new[] {
DriveService.Scope.Drive,
}
}.FromCertificate(certificate));
// Create the service.
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "MyApplicationName",
});
return service;
}
Creating a file
private static void CreateDriveDocument(string title)
{
Console.WriteLine("Google - Create New Document:" + Environment.NewLine);
File body = new File();
body.Title = title;
body.Description = "A test document";
body.MimeType = "text/plain";
byte[] byteArray = System.IO.File.ReadAllBytes(#"C:\myDoc.txt");
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
//Callin the service at "Service()"
FilesResource.InsertMediaUpload request = Service().Files.Insert(body, stream, "text/plain");
request.Upload();
File file = request.ResponseBody;
}
So here I create a File, since I authenticated with the ServiceAccount, thats where the File is uploaded.
The Question
I need to be able to Create/Copy this file to a specific user withing
my domain. And make that user owner of the file.
All help is appreciated.
Old post Reference
TransferOwnership Using Google SDK
I'll go with Marcel Balk anwser.
This way we will use impersonation!
we can use the service account to authenticate, then use any email account in our domain and upload files.
The uploaded files will be visible in the users Drive under My Files (not Shared with me!)
Files also are owned by the impersonated account.
Tested in a little console app:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v2;
using Google.Apis.Drive.v2.Data;
using Google.Apis.Services;
namespace UseWebServiceMethod
{
class Program
{
// Goto https://admin.google.com/AdminHome?chromeless=1#OGX:ManageOauthClients
// Login met admin account => ****#***.***
// Add/edit Client Name (Use Client ID of service account here) => 2***8.apps.googleusercontent.com
// One or More API Scopes => https://www.googleapis.com/auth/drive
public const string ImpersonatedAccountEmail = "****#***.***";
public const string ServiceAccountEmail = "2******8#developer.gserviceaccount.com";
public const string Key = #"C:\Users\Wouter\Desktop\GoogleTester\GoogleTester\A****c.p12";
public const string FileToUpload = #"C:\Users\Wouter\Desktop\GoogleTester\GoogleTester\whakingly.txt";
public const string ApplicationName = "A***l";
static void Main()
{
var certificate = new X509Certificate2(Key, "notasecret", X509KeyStorageFlags.Exportable);
var initializer = new ServiceAccountCredential.Initializer(ServiceAccountEmail)
{
Scopes = new[] { DriveService.Scope.Drive },
User = ImpersonatedAccountEmail
};
var credential = new ServiceAccountCredential(initializer.FromCertificate(certificate));
var driveService = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName
});
// Show all files
var list = driveService.Files.List().Execute();
if (list.Items != null)
foreach (var fileItem in list.Items)
{
Console.WriteLine(fileItem.Title + " - " + fileItem.Description);
}
Console.WriteLine("Press Enter to continue.");
Console.ReadLine();
// Upload a new file
File body = new File();
body.Title = "whakingly.txt";
body.Description = "A whakingly (that a new word I created just there) file thats uploaded with impersonation";
body.MimeType = "text/plain";
byte[] byteArray = System.IO.File.ReadAllBytes(FileToUpload);
var stream = new System.IO.MemoryStream(byteArray);
FilesResource.InsertMediaUpload request = driveService.Files.Insert(body, stream, "text/plain");
request.Upload();
File file = request.ResponseBody;
Console.WriteLine("Press Enter to continue.");
Console.ReadLine();
// Show all files
var list2 = driveService.Files.List().Execute();
if (list2.Items != null)
foreach (var fileItem in list2.Items)
{
Console.WriteLine(fileItem.Title + " - " + fileItem.Description);
}
Console.WriteLine("Press Enter to continue.");
Console.ReadLine();
}
}
}
You can easily do this by adding the user to the code.
See the user i added.
private static DriveService Service()
{
var certificate = new X509Certificate2(Constants.P12, "notasecret",X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(Constants.ServiceAccountEmail)
{
Scopes = new[] {DriveService.Scope.Drive},
User="someuser#somedomain.someextension" //Add this to your code!
}.FromCertificate(certificate));
// Create the service.
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "MyApplicationName",
});
return service;
}
Then go to admin.google.com --> Security --> Advance settings --> Manage oauth client access
Add the client id to the client name and add your scope (http://www.googleapis.com/auth/drive) to the "One or More API Scopes field"