specify an email address for Google Calendar notifications via api - c#

How do I specify an email address for GoogleCalendar API notifications?
I need to programatically manipulate my Google Calendar via the Google API using C# dot-net. I've been able to create, delete and modify events successfully, but I'd like some control over notifications. Currently only email and UI popup options are available, and email defaults to the Google email account.
What I'm thinking is that if I can manipulate the email address used, I can send email to my Verizon email-to-SMS address, and send a text message instead of email (SMS is an option only for Government accounts). Or possibly to a dummy email that interfaces to an SMS service like Twilio. Bottom line is I would like to send email notifications to one or more email addresses of my choosing.
The only option for email is in a constructor for an EventReminder override:
new EventReminder{Method="email",Minutes=60};
Here's my working code:
namespace stuff
{
class Program
{
static string[] Scopes = { CalendarService.Scope.Calendar };
static string ApplicationName = "Google Calendar API .NET Quickstart";
static string _Chelle = "fake_id#gmail.com";
static string _EventCalendar = "jwejq36jjgwijg54iw7yjs7j7e#group.calendar.google.com";
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/calendar-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 Google Calendar API service.
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
EventsResource.ListRequest request = service.Events.List(_EventCalendar);
request.TimeMin = DateTime.Now;
request.TimeMax = DateTime.Now.AddDays(30);
request.ShowDeleted = false;
Events events = request.Execute();
if (events.Items != null && events.Items.Count > 0)
{
foreach (var eventItem in events.Items)
{
// add new reminders
eventItem.Reminders = new Event.RemindersData
{
UseDefault = false,
Overrides = new[]
{
new EventReminder {Method="popup",Minutes=((60*24*7) - (60*9)) }, // one week before # 0900
new EventReminder {Method="email",Minutes=((60*24*7) - (60*9)) }
}
};
try
{
service.Events.Update(eventItem, _EventCalendar, eventItem.Id).Execute();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Thread.Sleep(1000);
}
}
else
{
Console.WriteLine("No events found.");
}
Console.Read();
}
}
}

Related

Explicit flow upload file to google drive

I have an Angular 6 app that uses .net Core 2.x as backend
I have the External provider working for the explicit flow (lot of samples)
I use explicit flow since I do not whant to share the secret and client id with the web app.
I have a upload file service (domain.com/api/upload)
Witch works fine
Now I would like to save the file to a logged in user's google drive.
The google project has the google drive api permissions and all
But I can't get the drive api client to authenticate with the drive api
Controller action
private async Task<IFileStoreItem> SaveFile(IFormFile file, IFilePath path)
{
if (file == null) throw new Exception("File is null");
if (file.Length == 0) throw new Exception("File is empty");
if(path == null) throw new Exception("path is null");
IFileStoreItem item = file.Adapt<DocumentInfo>();
item.Container = path.Container;
item.Partition = path.Partition;
IFileStoreService fileStoreService = GetFileStoreService();
if (file.Length > 0)
{
using (Stream stream = new MemoryStream())
{
await file.CopyToAsync(stream);
item = await fileStoreService.Save(stream, item);
}
item.Link = Url.AbsoluteUri("DownloadFile", "File", new { container = item.Container, partition = item.Partition, id = item.Id });
}
return item;
}
Helper function
private IFileStoreService GetFileStoreService()
{
IFileStoreService ret = null;
string uploads = Path.Combine(_environment.WebRootPath, "uploads");
DocumentStorageConfiguration config = new StorageConfiguration().GetStoragetSettings();
ret = new GoogleDriveService(User.Claims.Where(c => c.Type == JwtRegisteredClaimNames.UniqueName).SingleOrDefault()?.Value);
return ret;
}
Drive service
public class GoogleDriveService : IFileStoreService
{
DriveService _service;
UserCredential credential;
public GoogleDriveService(string user)
{
string[] scopes = new string[] {
DriveService.Scope.Drive,
DriveService.Scope.DriveFile};
GoogleConfiguration config = new AuthentificationConfiguration().GetGoogleSettings();
var secrets = new ClientSecrets
{
ClientId = config.ClientId,
ClientSecret = config.ClientSecret
};
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(secrets, scopes, user, CancellationToken.None).Result;
_service = new DriveService(new Google.Apis.Services.BaseClientService.Initializer() {
HttpClientInitializer = credential
});
}
public async Task<IFileStoreItem> Save(System.IO.Stream stream, IFileStoreItem item)
{
File body = new File
{
Name = item.Name,
MimeType = item.ContentType,
Parents = new List<string>() { "Documents" }
};
FilesResource.CreateMediaUpload request = new FilesResource.CreateMediaUpload(_service,body,stream,item.ContentType);
IUploadProgress progress = await request.UploadAsync();
item.Id = body.Id;
return item;
}
}
The error I am getting is bad gateway 502
Question is if I can authenticate with google drive based on the currently logged in user (witch already is a google user)

Google Analytics(Custom Dimension update) Insufficient Permission 403

I am trying to update custom dimension fields(https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtReference/management/customDimensions/update) in google analytics by calling the analytics api from C#.
I created a project in https://console.developers.google.com, added a service account(downloaded the .p12, private key file),enabled the analytics api and linked the service account email in https://analytics.google.com
I am able to read the "analytics data"(like account summaries etc) but not insert or update. When I try to do that, I get the Insufficient permission 403 error. The service account added to google analytics has all the privileges.
class Program
{
static void Main(string[] args)
{
test();
}
public static void test() //takes clientid as input
{
string[] scopes = new string[] { AnalyticsService.Scope.Analytics }; // view and manage your Google Analytics data
var keyFilePath = #"C:\Users\xyz\Desktop\CustomDimUpdate\xxxxxxxx.p12"; // Downloaded from https://console.developers.google.com
var serviceAccountEmail = "xxxx.iam.gserviceaccount.com"; // found https://console.developers.google.com
//loading the Key file
var certificate = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = scopes
}.FromCertificate(certificate));
var service = new AnalyticsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential
//ApplicationName = "Analytics API Sample",
});
CustomDimension body = new CustomDimension();
body.Name = "Configurable"; //Found in https://analytics.google.com
body.Scope = "Product"; //Found in https://analytics.google.com
body.Active = true;
try
{
//analytics.management().customDimensions()
// .update("123456", "UA-123456-1", "ga:dimension2", body).execute();
ManagementResource.CustomDimensionsResource.UpdateRequest update = service.Management.CustomDimensions.Update(body, "123456", "UA-123456-1", "ga:dimension1");
update.Execute(); //Errors out here
ManagementResource.AccountsResource.ListRequest list = service.Management.Accounts.List();
list.MaxResults = 1000; // Maximum number of Accounts to return, per request.
Accounts feed1 = list.Execute(); //Works perfectly fine
foreach (Account account in feed1.Items)
{
// Account
Console.WriteLine(string.Format("Account: {0}({1})", account.Name, account.Id));
}
ManagementResource.ProfilesResource.ListRequest list1 = service.Management.Profiles.List("123456", "UA-123456-1");
Profiles feed = list1.Execute();
foreach (Profile profile in feed.Items)
{
Console.WriteLine(string.Format("\t\tProfile: {0}({1})", profile.Name, profile.Id));
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
I was able to resolve this by changing the below line of code
string[] scopes = new string[] { AnalyticsService.Scope.Analytics }; // view and manage your Google Analytics data
to
string[] scopes = new string[] { AnalyticsService.Scope.AnalyticsEdit }; // view and manage your Google Analytics data

Access Google Calendar API using Service Account Authentication

I was able to access the Google Calendar API using the .NET Quickstart tutorial and it worked great!
The problem with that tutorial is that it uses Open Authentication or OAuth2. I will like to do the same using Service Account Authentication.
(https://support.google.com/googleapi/answer/6158857?hl=en)
Can someone give me an example of how I can access my calendar using a Service account key file?
I have tried also tried using Google Calendar API Authentication with C# tutorial and was not able to accomplish it.
I am curious as to why your first attempt with the service account tutorial didn't work. What was wrong? Was there an error?
Remember service accounts are not you. The service account has its own Google calendar account, so if you are trying to read one of your "personal calendars", it will not work. You are going to have to share your personal calendar with the service account.
Here is another example using the Json service account key file.
string[] scopes = new string[] { CalendarService.Scope.Calendar };
GoogleCredential credential;
using (var stream = new FileStream(serviceAccountCredentialFilePath, FileMode.Open, FileAccess.Read))
{
credential = GoogleCredential.FromStream(stream)
.CreateScoped(scopes);
}
// Create the Calendar service.
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Calendar Authentication Sample",
});
Try this. First Create a service account which can be found in your Google Dev Console.
For reference on implementation, check DalmTo's blogpost on how to use Service Accounts here.
Here's a snippet:
var certificate = new X509Certificate2(keyFile, "notasecret", X509KeyStorageFlags.Exportable);
try{
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = scopes
}.FromCertificate(certificate));
//Create the service.
DriveService service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Drive API Sample"
});
return service;
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
return null;
}
For anybody finding their way to this question but needing a NodeJS solution, this is how you can log in with a service account that has domain-wide delegation permissions as a specific user:
const auth = new google.auth.JWT({ // use JWT instead of GoogleAuth
subject: "me#mycompany.com", // specify subject (user whose context you want to operate in)
keyFile: "service-account-key.json",
scopes: [
"https://www.googleapis.com/auth/calendar.events",
"https://www.googleapis.com/auth/calendar.readonly"
],
})
visit this link have complete working project google service account authentication with google calendar event insert method you have to change your json private key and your Credentials only
https://github.com/CodeForget/Google-Service-Account-Authentication
here json key file othentication as well p12 authentication both
ServiceAccountAuthentication.cs
using Google.Apis.Calendar.v3;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using System;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using Google.Apis.Calendar.v3.Data;
namespace GoogleSamplecSharpSample.Calendarv3.Auth
{
public static class ServiceAccountExample
{
/// <summary>
/// Authenticating to Google calender using a Service account
/// Documentation: https://developers.google.com/accounts/docs/OAuth2#serviceaccount
/// </summary>
/// Both param pass from webform1.aspx page on page load
/// <param name="serviceAccountEmail">From Google Developer console https://console.developers.google.com/projectselector/iam-admin/serviceaccounts </param>
/// <param name="serviceAccountCredentialFilePath">Location of the .p12 or Json Service account key file downloaded from Google Developer console https://console.developers.google.com/projectselector/iam-admin/serviceaccounts </param>
/// <returns>AnalyticsService used to make requests against the Analytics API</returns>
public static CalendarService AuthenticateServiceAccount(string serviceAccountEmail, string serviceAccountCredentialFilePath, string[] scopes)
{
try
{
if (string.IsNullOrEmpty(serviceAccountCredentialFilePath))
throw new Exception("Path to the service account credentials file is required.");
if (!File.Exists(serviceAccountCredentialFilePath))
throw new Exception("The service account credentials file does not exist at: " + serviceAccountCredentialFilePath);
if (string.IsNullOrEmpty(serviceAccountEmail))
throw new Exception("ServiceAccountEmail is required.");
// For Json file
if (Path.GetExtension(serviceAccountCredentialFilePath).ToLower() == ".json")
{
GoogleCredential credential;
//using(FileStream stream = File.Open(serviceAccountCredentialFilePath, FileMode.Open, FileAccess.Read, FileShare.None))
using (var stream = new FileStream(serviceAccountCredentialFilePath, FileMode.Open, FileAccess.Read))
{
credential = GoogleCredential.FromStream(stream)
.CreateScoped(scopes).CreateWithUser("xyz#gmail.com");//put a email address from which you want to send calendar its like (calendar by xyz user )
}
// Create the Calendar service.
return new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Calendar_Appointment event Using Service Account Authentication",
});
}
else if (Path.GetExtension(serviceAccountCredentialFilePath).ToLower() == ".p12")
{ // If its a P12 file
var certificate = new X509Certificate2(serviceAccountCredentialFilePath, "notasecret", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = scopes
}.FromCertificate(certificate));
// Create the Calendar service.
return new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Calendar_Appointment event Using Service Account Authentication",
});
}
else
{
throw new Exception("Something Wrong With Service accounts credentials.");
}
}
catch (Exception ex)
{
throw new Exception("Create_Service_Account_Calendar_Failed", ex);
}
}
}
}
add webform.aspx and put this code on webform.aspx.cs
using System;
using Google.Apis.Calendar.v3;
using GoogleSamplecSharpSample.Calendarv3.Auth;
using Google.Apis.Calendar.v3.Data;
namespace CalendarServerToServerApi
{
public partial class WebForm1 : System.Web.UI.Page
{
// create event which you want to set using service account authentication
Event myEvent = new Event
{
Summary = "Visa Counselling",
Location = "Gurgaon sector 57",
Start = new EventDateTime()
{
DateTime = new DateTime(2017, 10, 4, 2, 0, 0),
TimeZone = "(GMT+05:30) India Standard Time"
},
End = new EventDateTime()
{
DateTime = new DateTime(2017, 10, 4, 2, 30, 0),
TimeZone = "(GMT+05:30) India Standard Time"
}
//,
// Recurrence = new String[] {
//"RRULE:FREQ=WEEKLY;BYDAY=MO"
//}
//,
// Attendees = new List<EventAttendee>()
// {
// new EventAttendee() { Email = "Srivastava998#gmail.com" }
//}
};
protected void Page_Load(object sender, EventArgs e)
{
}
public void Authenticate(object o, EventArgs e)
{
string[] scopes = new string[] {
CalendarService.Scope.Calendar //, // Manage your calendars
//CalendarService.Scope.CalendarReadonly // View your Calendars
};
string cal_user = "calenderID#gamil.com"; //your CalendarID On which you want to put events
//you get your calender id "https://calendar.google.com/calendar"
//go to setting >>calenders tab >> select calendar >>Under calender Detailes at Calendar Address:
string filePath = Server.MapPath("~/Key/key.json");
var service = ServiceAccountExample.AuthenticateServiceAccount("xyz#projectName.iam.gserviceaccount.com", filePath, scopes);
//"xyz#projectName.iam.gserviceaccount.com" this is your service account email id replace with your service account emailID you got it .
//when you create service account https://console.developers.google.com/projectselector/iam-admin/serviceaccounts
insert(service, cal_user, myEvent);
}
public static Event insert(CalendarService service, string id, Event myEvent)
{
try
{
return service.Events.Insert(myEvent, id).Execute();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
}
}

The redirect URI in the request: http://localhost:12349/authorize/ did not match a registered redirect URI

I'm getting this error while trying to run my c# console application... I am trying to call google calender api v3 to fetch calender and add event to calender. According to the sample code from google-api-dotnet-client I am doing this.( https://code.google.com/p/google-api-dotnet-client/source/browse/Calendar.VB.ConsoleApp/Program.vb?repo=samples ) Here is the vb.net code. I am using this sample after converting it to c# code.
Here is my code:
class Program
{
static void Main(string[] args)
{
try
{
new Program().Run().Wait();
}
catch (AggregateException ex)
{
foreach (var e in ex.InnerExceptions)
{
Console.WriteLine("ERROR: " + e.Message);
}
}
}
private async Task Run()
{
UserCredential credential;
IList<string> scopes = new List<string>();
CalendarService service;
scopes.Add(CalendarService.Scope.Calendar);
using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
{
// problem occuring during executing this statement.
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
scopes,
"user", CancellationToken.None, new FileDataStore("Calender.SampleApp") );
}
BaseClientService.Initializer initializer = new BaseClientService.Initializer();
initializer.HttpClientInitializer = credential;
initializer.ApplicationName = "C# Calendar Sample";
service = new CalendarService(initializer);
Event newEvent = new Event();
newEvent.Summary = "Appointment";
newEvent.Description = "Need to meet my Uncle";
IList<EventReminder> reminders = new List<EventReminder>();
reminders.Add(new EventReminder { Method = "sms", Minutes = 10 });
newEvent.Reminders = new Event.RemindersData { UseDefault = false, Overrides = reminders };
newEvent.Recurrence = new String[] { "DTSTART;TZID=Bangladesh Standard Time:20140124T163000;RRULE:FREQ=DAILY" };
IList<EventAttendee> attendees = new List<EventAttendee>();
attendees.Add(new EventAttendee { Email = "hannan.cse.m#gmail.com", Organizer = true, DisplayName = "Hannan" });
newEvent.Attendees = attendees;
newEvent.GuestsCanInviteOthers = false;
newEvent.GuestsCanModify = false;
newEvent.GuestsCanSeeOtherGuests = false;
newEvent.Location = "Dhaka, Bangladesh";
newEvent.Start = new EventDateTime { DateTime = DateTime.Now, TimeZone = "Bangladesh Standard Time" };
Event recurringEvent = service.Events.Insert(newEvent, "primary").Execute();
var list = await service.CalendarList.List().ExecuteAsync();
}
}
This is my redirect URIs in my GoogleDevelopers Console project.
Redirect URIs: http://localhost:7744/authorize/
And this is the error message shown in browser.
I couldn't find any way to resolve this problem. Some help will be appreciable. I also search all the realted post in stackoverflow. But I couldn't find it's solution.
I think you are doing something wrong while "create client id" in GoogleDevelopers Console. Make sure that you have chosed "Installed application" in application type to access your project from console application.
Have a look in the attached image. According to request type you must create clientid
and credintials in your registered application in Google Developers Console.
You don't need to define redirect uri in console application while authenticating.
I was having this error on a simple test program (https://ctrlq.org/google.apps.script/docs/guides/rest/quickstart/dotnet.html), and it happens to be the client_secret.json was th wrong one, download it again and it worked.

Can't list users with Google Directory API Admin SDK

I'm trying to use the AdminService to manage my domain's users and groups, but I'm stuck with a simple request to get all the users of my domain. There is the code in C#:
public Users GetAllUsers()
{
var provider = new AssertionFlowClient(
GoogleAuthenticationServer.Description,
new X509Certificate2(privateKeyPath, keyPassword, X509KeyStorageFlags.Exportable))
{
ServiceAccountId = serviceAccountEmail,
Scope = AdminService.Scopes.AdminDirectoryUser.GetStringValue()
};
var auth = new OAuth2Authenticator<AssertionFlowClient>(provider, AssertionFlowClient.GetState);
m_serviceGroup = new AdminService(new BaseClientService.Initializer()
{
Authenticator = auth,
});
var request = m_serviceUser.Users.List();
request.Domain = m_domainName;
return request.Fetch();
}
I'm getting an exception when Fetch() that says:
Code: 403
Message: Not Authorized to access this resource/api
Error: {Message[Not Authorized to access this resource/api] Location[ - ] Reason[forbidden] Domain[global]}
I've followed the instructions here to have enabled API access, and also authorized my service account in domain control panel:
[Security]->[Advanced Setting]->[Authentication]->[Manage third party OAuth Client access]
with scopes:
https://www.googleapis.com/auth/admin.directory.group
https://www.googleapis.com/auth/admin.directory.user
Admin SDK Service is also enabled in API control panel.
I tried the code to use the DriveService and successfully listed/created/deleted files without any problem, so the authentication part of the code should be alright. I couldn't figure out what else needs to be configured or if there is any other problems with my code.
Thanks for any help.
As described on the page:
Manage API client access
Developers can register their web applications and other API clients with Google to enable access to
data in Google services like Calendar. You can authorize these
registered clients to access your user data without your users having to individually give consent or their passwords. Learn more
The service account needs to act on behave of a user, so when initializing the client the ServiceAccountUser needs to be assigned.
var provider = new AssertionFlowClient(
GoogleAuthenticationServer.Description,
new X509Certificate2(privateKeyPath, keyPassword, X509KeyStorageFlags.Exportable))
{
ServiceAccountId = serviceAccountEmail,
Scope = AdminService.Scopes.AdminDirectoryUser.GetStringValue(),
ServiceAccountUser = domainManangerEmail
};
Edit: AssertionFlowClient is deprecated, the following should work:
var cert = new X509Certificate2(privateKeyPath, keyPassword, X509KeyStorageFlags.Exportable);
var serverCredential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new []{DirectoryService.Scope.AdminDirectoryUser},
User = domainManagerAccountEmail
}.FromCertificate(cert));
var dirService = new DirectoryService(new BaseClientService.Initializer()
{
HttpClientInitializer = serverCredential
});
This code works for me
static void GettingUsers()
{
String serviceAccountEmail = "xxxxxxx#developer.gserviceaccount.com";
var certificate = new X509Certificate2(#"xxxxx.p12", "notasecret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { DirectoryService.Scope.AdminDirectoryUser},
User = "your USER",
}.FromCertificate(certificate));
var service = new DirectoryService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "name of your app",
});
var listReq = service.Users.List();
listReq.Domain = "your domain";
Users allUsers = listReq.Execute();
int counter = 0;
foreach(User myUser in allUsers.UsersValue){
Console.WriteLine("*" + myUser.PrimaryEmail);
counter++;
}
Console.WriteLine(counter);
Console.ReadKey();
For more information, Please take a look in Directory API: Users list.
There are Limits and Quotas.
We will need to give the service ID that we are using the super admin or the right privileges to get pass this error.
Hope this helps.
-Venu Murthy
Work for me.
using Google.Apis.Auth.OAuth2;
using Google.Apis.Admin.Directory.directory_v1;
using Google.Apis.Admin.Directory.directory_v1.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static string[] Scopes = { DirectoryService.Scope.AdminDirectoryUserReadonly};
static string ApplicationName = "API G Suite implementation guid by amit";
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, ".credentials1/admin-directory_v1-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 Directory API service.
var service = new DirectoryService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
////// Define parameters of request.
UsersResource.ListRequest request = service.Users.List();
request.Customer = "my_customer";
request.MaxResults = 10;
request.OrderBy = UsersResource.ListRequest.OrderByEnum.Email;
////// List users.
IList<User> users = request.Execute().UsersValue;
Console.WriteLine("Users:");
if (users != null && users.Count > 0)
{
foreach (var userItem in users)
{
Console.WriteLine("{0} ({1})", userItem.PrimaryEmail,
userItem.Name.FullName);
}
}
else
{
Console.WriteLine("No users found.");
}
Console.Read();
}
}
}

Categories