Querying O365 Calendars With Chilkat - c#

We've been using the Chilkat component with C# for ages to read and send emails. Works great, no problems. Now though we need to be able to extract Calendar data for meetings rooms, all of which have their own mail account setup so that they can all show in Outlook and teams can, in effect, book the room.
Although I've been through the samples online (https://www.example-code.com/dotnet-core/outlook_calendar_list_events.asp) and got it working for our development mail account in terms of permissions etc. it can only see it's 'own' calendars (e.g. Calendar, UK Holidays, Birthdays), and not the calendars for the the meeting rooms (e.g. Room1, Room2, Room3), all of which I've added to the development mail account's list of calendars.
Is this something that's even possible? Have I connected the other calendars up wrong?
I've generated the token using a working set of endpoints (and the scope 'openid profile offline_access user.readwrite calendars.readwrite files.readwrite') and that's validated properly and returned a token. I've then used that with some of the Chilkat sample code but, when run, it outputs 3 calendars rather than the expected/hoped for 7:
http.AuthToken = jsonToken.StringOf("access_token");
Chilkat.HttpResponse resp = http.QuickRequest("GET","https://graph.microsoft.com/v1.0/me/calendars");
if (http.LastMethodSuccess != true)
{
Console.WriteLine(http.LastErrorText);
return;
}
Console.WriteLine("Response status code = " + Convert.ToString(resp.StatusCode));
// The HTTP request succeeded if the response status code = 200.
if (resp.StatusCode != 200)
{
Console.WriteLine(resp.BodyStr);
Console.WriteLine("Failed");
return;
}
Chilkat.JsonObject json = new Chilkat.JsonObject();
json.Load(resp.BodyStr);
json.EmitCompact = false;
Console.WriteLine(json.Emit());
string odata_context = json.StringOf("\"#odata.context\"");
Console.WriteLine(json.SizeOfArray("value"));

Related

Get calendars of office365 email without authentication

I want to get list of events of particular users lets say user#company1.com , user#company2.com which uses office365 accounts.
I need to retrive user#company2.com calendar with out login. My application will be like listing my available timings for my clients , so that they can select my free time and will schedule meeting with me. I need to filter the already scheduled events from my list... Is there any example code for getting calendar events without login??
I tried office365 multi-tenant application which will gives sample code for getting calendar events only after login. I need it with out authentication. Please help me on this.
Trying to access the O365 information without authentication is impossible , either user authentication or app authentication will be required . In your scenario ,you may need app authentication . You could try to build Daemon or Service Apps using client credential grant flow as described in this blog, the service app that requires admin consent, but is authorized to access any mailbox/calendar information in your Office 365 tenant.
Another choice is to use EWS Managed API, you could get free/busy information of a user and suggested meeting times by using the EWS Managed API :
https://msdn.microsoft.com/en-us/library/office/dn643673(v=exchg.150).aspx
And an existing Office add-in support on Outlook:
https://findtime.microsoft.com/
Finally I tried to use the free/busy code. My code is as follows... I am following this procedure but I don't know if it is correct or not. I have a Microsoft Office 365 account, and by passing credentials silently, I am creating Exchange Server service. After that i am passing different domain attendee information as ORGANIZER and REQUIRED as follows. But it is returning all values not skipping any scheduled meetings for those users.
Lets assume user1#domain.com is ORGANIZER and user2#anotherdomain.com is REQUIRED for meeting . User1 have meeting scheduled at 7:00-7:30pm on daily basis but when I executed the following script it shows me 7:00-7:30pm as available for meeting. It supposed to block that time.
Can you suggest some changes to code and am I proceeding in the correct way?
private static void GetSuggestedMeetingTimes(ExchangeService service)
{
List<AttendeeInfo> attendees = new List<AttendeeInfo>();
attendees.Add(new AttendeeInfo()
{
SmtpAddress = "user1#mydomain.com",
AttendeeType = MeetingAttendeeType.Organizer
});
attendees.Add(new AttendeeInfo()
{
SmtpAddress = "user2#anotherdomain.com",
AttendeeType = MeetingAttendeeType.Required
});
// Specify options to request free/busy information and suggested meeting times.
AvailabilityOptions availabilityOptions = new AvailabilityOptions();
availabilityOptions.GoodSuggestionThreshold = 49;
availabilityOptions.MaximumNonWorkHoursSuggestionsPerDay = 0;
availabilityOptions.MaximumSuggestionsPerDay = 40;
// Note that 60 minutes is the default value for MeetingDuration, but setting it explicitly for demonstration purposes.
availabilityOptions.MeetingDuration = 30;
availabilityOptions.MinimumSuggestionQuality = SuggestionQuality.Good;
availabilityOptions.DetailedSuggestionsWindow = new TimeWindow(DateTime.Now.AddDays(1), DateTime.Now.AddDays(2));
availabilityOptions.RequestedFreeBusyView = FreeBusyViewType.FreeBusy;
// Return free/busy information and a set of suggested meeting times.
// This method results in a GetUserAvailabilityRequest call to EWS.
GetUserAvailabilityResults results = service.GetUserAvailability(attendees,
availabilityOptions.DetailedSuggestionsWindow,
AvailabilityData.FreeBusyAndSuggestions,
availabilityOptions);
// Display suggested meeting times.
Console.WriteLine("Availability for {0} and {1}", attendees[0].SmtpAddress, attendees[1].SmtpAddress);
Console.WriteLine();
foreach (Suggestion suggestion in results.Suggestions)
{
Console.WriteLine("Suggested date: {0}\n", suggestion.Date.ToShortDateString());
Console.WriteLine("Suggested meeting times:\n");
foreach (TimeSuggestion timeSuggestion in suggestion.TimeSuggestions)
{
Console.WriteLine("\t{0} - {1}\n",
timeSuggestion.MeetingTime.ToShortTimeString(),
timeSuggestion.MeetingTime.Add(TimeSpan.FromMinutes(availabilityOptions.MeetingDuration)).ToShortTimeString());
}
}
int i = 0;
// Display free/busy times.
foreach (AttendeeAvailability availability in results.AttendeesAvailability)
{
Console.WriteLine("Availability information for {0}:\n", attendees[i].SmtpAddress);
foreach (CalendarEvent calEvent in availability.CalendarEvents)
{
Console.WriteLine("\tBusy from {0} to {1} \n", calEvent.StartTime.ToString(), calEvent.EndTime.ToString());
}
i++;
}

Sending emails as company domain from SendGrid

I own a domain, example.com. I'd like to send out emails from my webapp where it displays it's from info#example.com, or support#example.com.
I've used SendGrid a lot in the past from within my websites, but I've always simply been able to fill out where an email is from by editing the From property of the SendGridMessage class, and it just showed up that way on clients.
Is there an official way/API from sendgrid to utilize a domain that I own? What's to stop me or someone else from typing any domain they want using the sendgrid API?
The process of setting up DNS records for your domain that allow your emails to be authenticated, as well as verifying your ownership of the domains, is known at SendGrid as whitelabeling. After this process, SPF and DKIM records will then be available for receiving servers to check.
SPF and DKIM ensure the originating IP is allowed to send email on behalf of the domain in question, and to essentially verify that the contents of the email have not been tampered with respectively.
The thing that will stop others from sending from your domain is called DMARC. Domains owned by yahoo, aol, and very soon google all implement strict policies; emails claiming to be from these domains but that are not will never be delivered. Many other domains will soon be following this trend and implementing DMARC.
The code sample from https://azure.microsoft.com/en-us/documentation/articles/sendgrid-dotnet-how-to-send-email/ shows how to do this:
// Create the email object first, then add the properties.
var myMessage = new SendGridMessage();
// Add the message properties.
myMessage.From = new MailAddress("john#example.com");
// Add multiple addresses to the To field.
List<String> recipients = new List<String>
{
#"Jeff Smith <jeff#example.com>",
#"Anna Lidman <anna#example.com>",
#"Peter Saddow <peter#example.com>"
};
myMessage.AddTo(recipients);
myMessage.Subject = "Testing the SendGrid Library";
//Add the HTML and Text bodies
myMessage.Html = "<p>Hello World!</p>";
myMessage.Text = "Hello World plain text!";

Selenium Webdriver automation with emails

I'm currently trying to use Selenium Webdriver (C#) to automate a Forgot Password -> Reset Password workflow, where the user navigates to a page and supplies their username, and the backend code validates the username, then sends an email with a reset password link to the email address associated with their account.
I'm able to automate the process up to the point where the code sends the email, but I don't know any ways of checking for the email and/or clicking a link in the email, so I was hoping someone more experienced with Selenium/automation may be able to give me a few pointers.
Ideally the test should not care about the email address that the email is being sent to. Is there a way for Selenium WebDriver or some 3rd party package to catch the email being sent?
Thanks for any input or suggestions.
No. You are talking about setting up an email server, which is not an easy task.
You should send it to a test work email (if this is for a company), or a public email (hotmail/gmail), or if security is not an issue at all, the easiest place to send it would be a disposable email (mailinator)
You could try PutsBox. You can send an email to whatever-you-want#putsbox.com, wait for a few seconds (SMTP stuff ins't instantaneous) then check your email via http://preview.putsbox.com/p/whatever-you-want/last.
Have a look at this post tutorial, it can give you some ideas.
There is no integration of selenium with email clients like Thunderbird/Outlook. But if you have a web interface of the same email client, then you can access the email from browser and using selenium you can read and respond to the emails. I have tried this recently and it works fine where I have used web Outlook for testing.
Hope this helps.
Hi I was in a similar situation and was able to successfully implement a way to get an activation or forgotten password links.
Using Java Mail API I was able to trigger a method when such action is performed which goes into a Folder and read a specific message line then get the link and open it up in a browser using WebDriver.
However the main drawback with this is the inconsistency of reading a specific folder, sometimes emails goes to spam or other folder (in case of Gmail the new Social Folder) making it invisible or difficult to be retrieved.
Overall i think its a process that shouldn't really be automated, In terms of testing it should be done more code base level by mocking responses.
Snippet below should give you an idea on how to go about implementing
public class RefactoredMail {
public static void main(String[] args) {
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imaps");
try {
Session session = Session.getInstance(props, null);
Store store = session.getStore();
store.connect("imap.gmail.com", "username", "password");
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
Message msg = inbox.getMessage(inbox.getMessageCount());
Address[] in = msg.getFrom();
for (Address address : in) {
System.out.println("FROM:" + address.toString());
}
Multipart mp = (Multipart) msg.getContent();
BodyPart bp = mp.getBodyPart(0);
System.out.println("SENT DATE:" + msg.getSentDate());
System.out.println("SUBJECT:" + msg.getSubject());
System.out.println("CONTENT:" + bp.getContent());
System.out.println("Activation Link:" + ((String)
bp.getContent()).startsWith("http"));
String [] line = new String[1];
line [0] = mp.getContentType().toString();
System.out.println("Activation Link:" + (mp.getBodyPart(0).getLineCount()));
System.out.println("Activation Link:" +line[0]);
} catch (Exception e) {
e.printStackTrace();
}
//WebDriver Stuffs
public String activationUrl() {
//getting the url link and making it a global variable .... etc
//Accessing the link
}
}
You can use https://github.com/cmendible/netDumbster or http://ndumbster.sourceforge.net/default.html. I've used one i forget which. This will host an smtp listener and allow you to make assertions against any email it receives. Its kind of awesome! The caveat is you need to be able to control where the server delivers mail in the environment you are testing.

Paypal Rest API Working with Demo Account but not with Live Account

I use below code for Paypal Rest API to Store Credit Card Information
String AccessToken = "";
PaypalAccessToken pat = new PaypalAccessToken();
AccessToken = pat.GetPaypalAccessToken();
PayPal.Api.Payments.Address add = new PayPal.Api.Payments.Address();
add.city = TextBoxCity.Text;
add.line1 = TextBoxAddress.Text;
add.phone = TextBoxPhoneNumber.Text;
add.postal_code = TextBoxZipcode.Text;
add.state = DropDownState.SelectedValue;
PayPal.Api.Payments.CreditCard cc = new PayPal.Api.Payments.CreditCard();
cc.number = TextBoxCreditCardNumber.Text;
cc.expire_month = Convert.ToInt16(TextBoxExpiryMonth.Text);
cc.expire_year = Convert.ToInt16(TextBoxExpiryYear.Text);
cc.cvv2 = TextBoxCVVNumber.Text;
cc.type = DropDownCreditCardType.SelectedValue;
//cc.billing_address = add;
PayPal.Api.Payments.CreditCard ResultCC = cc.Create(AccessToken);
This Code working good with Demo Credentials but When I try to use same with Live Credentials, It is giving me error : 401 Unauthorized. I can still get token in live credentials but I could not store credit card.
PayPal's REST API's are available globally as of the 31st of July 2013.
However, this applies only to PayPal payments. That is, setting the payment_method to paypal in the JSON payload and redirecting the buyer to PayPal.
If you want to process credit card payments, this is currently only supported in the US, UK or Canada.
You will need to enable this capability for your app via https://developer.paypal.com/ > Applications.
Note: Enabling card payments for live transactions requires a review of your account.
If you try to process card payments in live, with an account that's not enabled for it, you will get a HTTP 401 in the response from the /payment resource.
Have you updated your endpoints to use the live endpoints?
It was something wrong with Paypal Rest ApI because its still in Beta version . They fixed it for me.

DotNetOpenAuth with Google Calendar Feed

I have been racking my brain for a few days trying to get a list of calendars from Google using DotNetOpenAuth.
I can successfully get a list of contacts using the DotNetOpenAuth Samples. I have integrated it with my domain using the OpenId+OAuth. Everything works great to get a list of contacts.
So from there I modified the code to try to retrieve a list of Calendars and I keep getting a 401 Unauthorized error.
I know it is authorizing because I can get the contact list. Does anyone have a code example how they are retrieving calendars or calendar events using the DotNetOpenAuth with Google???
Thanks
Update:
Thanks for the response. I have read everything I can get my hands on. Here is what I have done so far
Step 1: I created a new GetCalendarEndPoint in the GoogleConsumer.cs
private static readonly MessageReceivingEndpoint GetCalendarEndpoint = new MessageReceivingEndpoint("https://www.google.com/calendar/feeds/default", HttpDeliveryMethods.GetRequest);
Step 2: Next I created a new method GetCalendars patterned after the GetContacts Method in GoogleConsumer.cs - (Rebuilt the dll etc.)
public static XDocument GetCalendars(ConsumerBase consumer, string accessToken, int maxResults/* = 25*/, int startIndex/* = 1*/) {
if (consumer == null)
{
throw new ArgumentNullException("consumer");
}
var request = consumer.PrepareAuthorizedRequest(GetCalendarEndpoint, accessToken);
var response = consumer.Channel.WebRequestHandler.GetResponse(request);
string body = response.GetResponseReader().ReadToEnd();
XDocument result = XDocument.Parse(body);
return result;
Step 3: In my Application I modified the ScopeURI to the the Calendar URI from GoogleConsumer as follows
private IAuthenticationRequest GetGoogleRequest()
{
Realm realm = Request.Url.Scheme + Uri.SchemeDelimiter + Global.GoogleTokenManager.ConsumerKey + "/";
IAuthenticationRequest authReq = relyingParty.CreateRequest(GoogleOPIdentifier, realm);
// Prepare the OAuth extension
string scope = GoogleConsumer.GetScopeUri(GoogleConsumer.Applications.Calendar);
Global.GoogleWebConsumer.AttachAuthorizationRequest(authReq, scope);
// We also want the user's email address
var fetch = new FetchRequest();
fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
authReq.AddExtension(fetch);
return authReq;
}
However, when I run the app I get 401 Unauthorized when I make the following call
var calendars = GoogleConsumer.GetCalendars(Global.GoogleWebConsumer, State.GoogleAccessToken, 25, 1);
I have also checked that the State.GoogleAccess token exists by simply displaying it on my screen before I trigger the method that makes this call.
Again, if I exectute
var calendars = GoogleConsumer.GetContacs(Global.GoogleWebConsumer, State.GoogleAccessToken, 25, 1);
then it works??????? Thanks for you help.
I've been suffering through exactly the same thing for most of the weekend.
I think that after much fiddling with Fiddler I've found the cause and have a solution which, although not pretty, seems to work. I found that I was able to access the calendar feed by copying and pasting the DNOA-generated Uri into a browser, but always got a 401 when attempting programmatic access. This is apparently because the default auto-redirect behavior of HttpWebRequest discards any cookies that the redirect is attempting to set. The Contacts feed doesn't set any cookies during the redirect, so it is immune.
The first time you request a calendar feed (even with a properly constructed and signed OAuth request), Google replies with a redirect containing a cookie. If you don't present that "calendar cookie" at the same time as your feed request you will get a 401 Unauthorized when you attempt to follow the redirect to the feed.
here's the cookie-setting header from Google:
HTTP/1.1 302 Moved Temporarily
Set-Cookie: S=calendar=y7AlfgbmcqYl0ugrF-Zt9A;Expires=Tue, 10-Jan-2012 03:54:20 GMT;Secure
Here's what I'm doing to make it work:
// wc: WebConsumer
var calRequest = wc.PrepareAuthorizedRequest(erp2, authTokenRsp.AccessToken);
// need to stop redirect to capture calendar cookie token:
calRequest.AllowAutoRedirect = false;
var calResponse = calRequest.GetResponse();
var redirectCookie = calResponse.Headers[System.Net.HttpResponseHeader.SetCookie];
var cookiedCalRequest = wc.PrepareAuthorizedRequest(erp2, authTokenRsp.AccessToken);
cookiedCalRequest.Headers[System.Net.HttpRequestHeader.Cookie] = redirectCookie;
var calFeedResponse = cookiedCalRequest.GetResponse();
Have you read the Google Calendar data API documentation to make sure you have the right endpoints programmed in? Have you also modified the code that acquires the access token to request access to Google Calendar in addition to Google Contacts? The access token in the sample only gets Contacts permissions unless you change it.

Categories