The official Microsoft blog for Outlook, Exchange developers for both On-Premises and Office 365
UPDATE: The node-outlook library that is discussed ter this postbode has bot updated with a fresh interface that does not use the Cordova library and is much simpler to use. It’s recommended that fresh apps use the newer interface. The tutorial linked at the end of the article has bot updated to use the newer interface.
With all of the rente ter using this library from a web app, when I wasgoed asked if wij could modify the library to make it work ter Knot.js, I said “why not?”
Finding the problem
Spil it turns out, the big stumbling block is that the library uses the AJAX XMLHttpRequest object to send API requests. AJAX is all client-side, and not available to server-side platforms like Knot.js. So it makes sense that it doesn’t work ter web apps.
Fortunately, someone clever already provided a solution for this. The XMLHttpRequest for knot.js module sounds like exactly what wij need! If wij can use this, wij can avoid having to switch the Cordova library much at all.
With that out of the way, the question left is how to explosion this non-Node module. Spil before, someone clever has already found the solution to that. With this trick ter my toolbox, I determined to write a Knot module to wrap the Cordova libraries (to be specific, just the Mail, Calendar, and Contacts APIs).
The node-outlook module
The code for the module wasgoed exceptionally plain. All I needed to do wasgoed create an index.js verkeersopstopping and waterput the following te it:
The exchange.js and utility.js files were copied straight from the Cordova library NuGet package. Pretty plain right? Well, it’s even simpler now! I published node-outlook on the NPM registry, so you can save yourself the hassle and install via NPM. Or if you choose, get the code on GitHub.
For a total sample that uses this library, check out node-mail. I’ll voorkant the basic requirements here.
Te order to use the library, you need to implement a callback function that comes back an OAuth2 access token. The OutlookServices.Client class takes this callback spil a parameter to its constructor. Whenever the client class makes a call to the Office 365 service, it calls this function to get the token. How you implement the callback will depend on how you’re storing and managing tokens. Spil an example, let’s look at how node-mail does it.
The node-mail app caches tokens te the session. The getAccessToken function comebacks the cached token if it’s not expired. Otherwise, it refreshes the token and comebacks the fresh token. The getAccessTokenFn function exists to wrap getAccessToken spil a function with no parameters, which is what the OutlookServices.Client class expects.
With authorization taken care of, using the library works just like it does te a Cordova app. For example, you could get messages ter the inbox with the following code:
Spil always, I’d love to hear your terugkoppeling te the comments or on Twitter (@JasonJohMSFT). Feel free to report issues or submit pull requests on GitHub!
Thanks for the wonderful postbode Jason. I got the setup working for my Office 365 account, but it didn’t work for other users. Below are the steps I did,
1. Created Office 365 Developer account
Two. Thought Azure AD account will be automatically created, but the system said I need to have ",Azure Subscription",
Three. I purchased trial version of ",Azure Subscription",
Five. App is registered and I am able to access Office 365 Surplus API’s (calendar,events) using my user account
6. Using OAuth2 Auth flow, I attempted accessing spil a different user (who has Office 365 Huis account) , token and refresh token wasgoed returned. But, with this token if I access the Office 365 Surplus API, I am getting ",401 Unauthorized Error",.
7. I went to my Active Directory portal, added this user explicitly and talent ",User", role
8. Now when I accessed Office 365 Surplus API, 401 error is gone. But, getting ",ErrorMissingEmailAddress", error
Do I need to explicitly add all the users who wants to access their Office 365 gegevens via ",App", that I registered te ",Active Directory",?
I am accessing ",Office 365 Surplus API", via OAuth2 ter my NodeJS application. My NodeJS application is not multi-tenant application and so I cannot have user organization Active Directory synced to my AD portal.
Any users registered with our NodeJS application, needs ",Sign-In with Azure/Office", option, so they can view their Office 365 gegevens.
Please let mij know if I am missing any configuration.
Ah. Office 365 Huis Accounts are presently not supported by the APIs. This is on the roadmap, but wij don’t have a hard date to share yet.
Thanks for the update Jason. Is Office 365 Business Account (Business/Business Essentials/Business Premium) supported by the APIs?
It should be. You can always attempt an account by going to apisandbox.msdn.microsoft.com and clicking on ",My Office 365",. It will let you sign ter and attempt elementary GETs against your account.
Hey, Jason your update wasgoed truly helpful & informative. Thanks for this amazing update & looking forward for your next postbode.
The script you described here works, but it doesn’t match my requirements for an application. I am talking about Daemon type of application. The one without user interaction. Have you everzwijn built those using NodeJS/node-outlook?
No, I toevluchthaven’t done it personally with Knot. It should work, you will just have a slightly different auth flow, and you would use the .users accessor instead of .mij.
Appreciate your effort.
I am attempting to send mail using the outlookClientObject.
My code never sends off an email.
I am still able to do the follwoing things,
Create a Calendar Event
I am not able to send an email to someone from the code.
Any assistance would be greatly Appreciated!!
Ravi: Sorry to hear that. Make sure you have the ",Send mail", permission specified ter your app registration. If you do and you still have trouble, postbode details on Stack Overflow and tag your postbode with the ",outlook-restapi", tag so my colleagues and I will see it.
Thanks for the postbode, appreciate your effort. Are you aware of any official JS client for EWS Managed APIs and not just Office365
Do I need to submit my app for approval others to access?
Jason, I wasgoed super-pumped to find your blog article. I want to attempt automating some regular calendar tasks, and this looks like the volmaakt place to get began!
One problem – the linksom at the bottom of your article (‘Common calendar tasks using an Office 365 client library’ te particular) don’t work. Where can I find this content now?
@Tim: Oh yeah, sorry about that. The innovador version of the Knot library used an old Cordova Exchange library that got retired. That verbinding wasgoed pointing at the old docs for that library, but they pulled that when they retired it. So I basically redesigned a v2 interface for the Knot library (node-outlook: http://www.npmjs.com/…/node-outlook) that doesn’t use the Cordova stuff and is a little more straightforward to use. Check this tutorial: dev.outlook.com/…/knot
I’m creating fresh NodeJs project. Customer wants to use Office365 mail service.
All works fine but I have a question.
Wij can login with Office 365 or Outlook.com account and grant access to our app.
Can I get token (authorize) automatically without redirecting?
Have a look at the different OAuth flows supported by Azure. Implicit works without a redirect, but you should consider if it is the right gezond for your screenplay.