I want to access client_secret file of google calendar and to use it to add an event in my calendar.
I'm creating a chatbot in c# and in the moment that I use the path of my computer ( when I try to access client_secret file of my project in my pc ) works goods.
When I publish the project on the web and try to access the file I can't.
My code is :
string path = #"C:\Project\Test\Bot\V3\client_secret.json";
string pathi = #"{
'installed': {
'client_id': '1111111-
21432423.apps.googleusercontent.com',
'project_id': '111',
'auth_uri': 'https://accounts.google.com/o/oauth2/auth',
'token_uri': 'https://www.googleapis.com/oauth2/v3/token',
'auth_provider_x509_cert_url': 'https://www.googleapis.com/oauth2/v1/certs',
'client_secret': '22222222222222',
'redirect_uris': [ 'urn:ietf:wg:oauth:2.0:oob', 'http:dev.ai.com/BotTest/' ]
}
}";
UserCredential credential;
string[] Scopes = { CalendarService.Scope.Calendar };
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
using (var stream =
new FileStream(pathi, FileMode.Open, FileAccess.Read))
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
string ApplicationName = "Google Calendar API .NET Quickstart";
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
Event newEvent = new Event()
{
Summary = "Event",
Location = "",
Description = "Event is scheduled for: " +
eventi.Date.ToString("dd-MM-yyyy") + " " +
eventi.Time.ToShortTimeString(),
Start = new EventDateTime()
{
// DateTime = eventi.Date,
DateTime = new DateTime(eventi.Date.Year,
eventi.Date.Month, eventi.Date.Day, eventi.Time.Hour,
eventi.Time.Minute, 0),
TimeZone = "Europe/Brussels",
},
End = new EventDateTime()
{
DateTime = eventi.Date.AddMinutes(60),
// DateTime = new DateTime(eventi.Date.Year,
eventi.Date.Month, eventi.Date.Day, eventi.Time.Hour,
eventi.Time.Hour +1 ,0),
TimeZone = "Europe/Brussels",
},
Recurrence = new String[] { "RRULE:FREQ=DAILY;COUNT=1" },
Attendees = new EventAttendee[] {
new EventAttendee() { Email = "eeee#gmail.com" },
},
///////////////////////////////////////////
///////////////////////////////////////////
Reminders = new Event.RemindersData()
{
UseDefault = false,
Overrides = new EventReminder[] {
new EventReminder() { Method = "email", Minutes = 1 * 60 },
new EventReminder() { Method = "sms", Minutes = 10 },
}
}
};
String calendarId = "primary";
EventsResource.InsertRequest request1 =
service.Events.Insert(newEvent, calendarId);
Event createdEvent = request1.Execute();
Console.WriteLine("Event created: {0}", createdEvent.HtmlLink);
// Define parameters of request.
EventsResource.ListRequest request = service.Events.List("primary");
request.TimeMin = eventi.Time;
request.ShowDeleted = false;
request.SingleEvents = true;
request.MaxResults = 10;
request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;
// List events.
Events events = request.Execute();
Console.WriteLine("Upcoming events:");
if (events.Items != null && events.Items.Count > 0)
{
foreach (var eventItem in events.Items)
{
string when = eventItem.Start.Date;
if (String.IsNullOrEmpty(when))
{
when = eventItem.Start.Date;
}
Console.WriteLine("{0} ({1})", eventItem.Summary, when);
}
}
else
{
Console.WriteLine("No upcoming events found.");
}
//Console.Read();
// context.Wait(this.MessageReceived);
}
context.Wait(this.MessageReceived);
}
When i use path and in this part of code i put path work :
using (var stream =
new FileStream(path, FileMode.Open, FileAccess.Read))
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
When I put the root of the file in the web doesn't work, the same when i put all the script of the file in a string path :
string path = #"http://dev.aaaa.com/bot/client_secret.json"
I try to put just the name file but still don't work:
using (var stream =
new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Can you help me please how to access this file?
Copy credentials.json to bin\debug and bin\release folders and follow this way if your client is console application ,
UserCredential credential;
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
using (var stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[] { CalendarService.Scope.CalendarReadonly },
"user", CancellationToken.None, new FileDataStore(credPath));
}
For web App the client_secret.json would look like this and contains redirect uri's etc.
{"web":{"client_id":" ","project_id":" ","auth_uri":" ","token_uri":" ","auth_provider_x509_cert_url":" ","client_secret":" ","redirect_uris":[]}}
Also don't disclose any of your id's and secrets.
Related
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 am trying to download a file from Google Drive using the following code.
when it reach SaveStream() function it throwing an UnauthorizedAccessException.
As per some suggesstions I set the access permissions of that folder to Full control.But its not working for me now also I am getting the same Exception.
namespace BackEnd.Controllers
{
public class GoogleDriveController : Controller
{
// GET: GoogleDrive
static string[] Scopes = { DriveService.Scope.DriveReadonly };
static string ApplicationName = "Drive API .NET Quickstart";
public ActionResult Index()
{
UserCredential credential;
using (var stream = new FileStream(#"D:\client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/drive-dotnet-quickstart.json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
// Console.WriteLine("Credential file saved to: " + credPath);
}
// 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.MaxResults = 10;
IList<Google.Apis.Drive.v2.Data.File> files = listRequest.Execute().Items;
if (files != null && files.Count > 0)
{
foreach (var file in files)
{
DownloadFile(service, file, string.Format(#"D:\Photos"));
}
}
return View();
}
private static void DownloadFile(Google.Apis.Drive.v2.DriveService service, Google.Apis.Drive.v2.Data.File file, string saveTo)
{
var request = service.Files.Get(file.Id);
var stream = new System.IO.MemoryStream();
request.MediaDownloader.ProgressChanged += (Google.Apis.Download.IDownloadProgress progress) =>
{
switch (progress.Status)
{
case Google.Apis.Download.DownloadStatus.Downloading:
{
// (progress.BytesDownloaded);
break;
}
case Google.Apis.Download.DownloadStatus.Completed:
{
//"Download complete.";
SaveStream(stream, saveTo);
break;
}
case Google.Apis.Download.DownloadStatus.Failed:
{
//"Download failed.";
break;
}
}
};
request.Download(stream);
}
private static void SaveStream(System.IO.MemoryStream stream, string saveTo)
{
using (System.IO.FileStream file = new System.IO.FileStream(saveTo, System.IO.FileMode.Create, System.IO.FileAccess.Write))
{
stream.WriteTo(file);
}
}
}
}
I created a console application which sends an email to an address, it shows like:
private static string[] Scopes = { GmailService.Scope.GmailCompose, GmailService.Scope.GmailSend };
private static string ApplicationName = "Gmail API Quickstart";
private const string SUBJECT = "Some simple subject";
private static string[] emails = new string[] { "teodorescumihail2008#live.com" };
static void Main(string[] args)
{
UserCredential credential;
using (var stream =
new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = Environment.GetFolderPath(
Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/gmail-dotnet-quickstart.json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
// Create Gmail API service.
var service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
var msg = new AE.Net.Mail.MailMessage
{
Subject = SUBJECT,
Body = "Test body",
From = new MailAddress("teodorescumihail2008#gmail.com")
};
msg.To.Add(new MailAddress("teodorescumihail2008#live.com"));
StringWriter sw = new StringWriter();
msg.Save(sw);
var result = service.Users.Messages.Send(new Message
{
Raw = Base64UrlEncode(sw.ToString())
}, "me").Execute();
Console.Read();
}
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("=", "");
}
When execute sending email, I received:
Where is my mistake ?
I assumed you changed your scope in the code without reauthorize/getting a new credential file from Gmail. The problem is GmailAPI does not know that your scope is updated. You have to reauthorize your app in GmailAPI by deleting your app permission and regenerate your credentials.json so that your scope is updated on GmailAPI.
To delete your app permission: click here
I am using Google.Apis.Admin.Directory.directory_v1.Data to manage google users and groups. I can create user and group, but how to insert my new user in new group?
Here is my code
static void Main(string[] args)
{
UserCredential credential;
using (var stream =
new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/admin-directory_v1-dotnet-quickstart.json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new string[] { DirectoryService.Scope.AdminDirectoryGroup, DirectoryService.Scope.AdminDirectoryUser },
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
var service = new DirectoryService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Directory API .NET Quickstart",
});
User user = new User();
UserName username = new UserName();
user.PrimaryEmail = "mail#gmail.com";
username.GivenName = "GivenName";
username.FamilyName = "FamilyName";
user.Name = username;
user.Password = "Password";
service.Users.Insert(user);
var group = new Group();
group.Email = "group#gmail.com";
group.Name = "GroupName";
service.Groups.Insert(group);
}
Use https://developers.google.com/admin-sdk/directory/v1/reference/members/insert
Like role, add email as a parameter. Check the Members resource for more details.
Add Execute() and that worked for me
service.Users.Insert(user).Execute()
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("=", "");
}