Set up and run the Office 365 iOS SDK Samples

One of the best things about the Office 365 REST APIs is that it comes with an iOS SDK. That means you don't have to get down to the level of constructing HTTP calls - it's all abstracted away for you in the SDK.

Let's see how the SDK works by downloading and running a sample.

1. Go to the Office 365 SDK for iOS GitHub repository and clone it. You can either do that using the command line, or via the Mac desktop client.

Clone by clicking "Clone in Desktop" on the lower-right of this screenshot.

Clone by clicking "Clone in Desktop" on the lower-right of this screenshot.

2. After you've cloned the repository, cd into it using the terminal. Then, cd into the Samples folder. You'll notice there are three Samples: 

This is because there are currently three services that are under the Office 365 umbrella - OneDrive for Business (also known as SharePoint), Exchange, and Discovery (which is more of a tool to help you find the right endpoint for the service, than a service itself). In this tutorial, let's focus on the simple-drive-app sample. 

3. cd into simple-drive-app sample. Then, run the following command:

pod install

Please make sure that you have cocoapods installed already. This is a must-have for any Mac developer, so head on over to their website and learn how to set it up if you aren't already familiar with using it.

This is what you'll see if after cocoapods has successfully downloaded the dependencies.

4. Open the workspace:

XCode should launch and you'll see this:

5. Run the app on a simulator or device. In this case, I'm running it on a simulator. You'll see that it asks you for these things: Authority Url, Redirect Url and Client Id.

6. In order to get these, we must first register an app in Azure AD.

This tutorial assumes that you already know what Azure AD is, and that you have an Azure account as well as an Office 365 Developer Tenant. Here's a step-by-step walkthrough on how to set up an Office 365 Developer Tenant.

Go to Azure.com, and click on Portal at the top. Enter your login for Azure, and select your Active Directory that you want this app to be registered to. Let's say we want to test it on my clippy.onmicrosoft.com Office 365 tenant, so in this case, I select that tenant. You'll be able to select your own tenant here based on what you created previously.

Once you've select your Active Directory, click on "Applications" as shown below:

At the bottom, press "Add"

Select "Add an application my organization is developing" and on the next page, enter any name for your application. In this case, I'm using "iOS-Drive-Sample". Select "Native Client Application" since it's going to run on a mobile device.

On the next page, type anything you want into the Redirect URI box. I chose "https://www.google.com" as my redirect URI.

Press OK. Go to "Configure" and you'll see the following:

First we want to press the green "Add application" button below and select "Office 365 SharePoint Online"

Then after we press the OK button, we'll want to just select all the permissions that we need for this sample app. Select "Read users' files" and "Edit or delete users' files" for now.

Press "Save" so the changes are saved.

7. Go back to the app running in the simulator, and under "Authority URL", paste the following: https://login.windows.net/YOURTENANT.onmicrosoft.com. Replace YOURTENANT with the tenant address of your developer tenant. E.g. if your developer tenant is clippy.onmicrosoft.com, your Authority URL is https://login.windows.net/clippy.onmicrosoft.com

For redirect URL and client ID, paste the ones that are shown on the Azure AD Application dashboard.

Press Save, and then press "Files" on the bottom tab bar after you're done filling up these, and you'll see our good 'ol login screen.

Sign in with your developer tenant account. E.g. "kloh@clippy.onmicrosoft.com"

Screen Shot 2015-01-05 at 2.44.23 PM.png

8. Something's gone wrong - you'll see a very annoying loading indicator, and your files don't show up. Uh-oh, let's back up a bit and try to fix what's going on.

If you go back to Xcode and look at the Output, you'll see this:

Strange, why is it making a GET request to "https://teeudev1-my.sharepoint.com/_api/v1.0/me/files/" when that's not your OneDrive for Business (i.e. SharePoint) endpoint?

Fortunately, it's just changing a single string. Do a search in your workspace for "teeudev1" and you'll quickly realize that BaseController.m has that URL hard-coded in it. 

Replace teeudev1 with your tenant address. So if your developer tenant is "clippy.onmicrosoft.com", you'll want to replace that highlighted text in the image above with "https://clippy-my.sharepoint.com"

9. Run the app again. Select "Files" in the bottom tab bar and you should see all your files displayed!

Note: If it didn't work, press "Clear Credentials" under Settings and try logging in again.

I hope this helps you get the sample project set up and configured. 

You can press the "+" on the top right to create and upload a new text file.

Screen Shot 2015-01-05 at 2.59.02 PM.png

The files that are showing are the same files that are in your root folder in OneDrive for Business. You can access OneDrive for Business by going to the Office portal page and clicking on "OneDrive" at the top.

I hope this was helpful - if you have any questions, leave them in the Comments below and I'll try to answer them.

Send email using Mail API in C#

Note: This post may or may not be outdated, as it uses the SDK from when the Office 365 APIs were in Preview.

Because I couldn't find anything online, here's my contribution to the community. This code snippet shows you how to send a message using the C# SDK for the Exchange Mail REST API.

IMessage message = new Message();
Recipient recipient = new Recipient();
recipient.Name = "abcde";
recipient.Address = "abcde@12345.onmicrosoft.com";
ItemBody body = new ItemBody();
body.Content = "Hey!";
message.Body = body;
message.ToRecipients.Add(recipient);
await mailClient.Me.Messages.AddMessageAsync(message);

Making a REST call to the Office 365 Files API

In this post, I'll show how to make a REST call to the Office 365 Files API.

Note: There is a good article written by Matthias Leibmann, Program Manager on the Exchange team at Microsoft, on using OAuth to access Calendar, Contact and Mail API in Office 365.

It's not as easy as doing a "GET" request to a single endpoint. You first have to go through the Oauth flow, of course, because you want to get meaningful user data.

I'm using Advanced REST client, an app for Chrome that you can obtain in the Chrome Web Store.

It seems that a lot of people who develop on Windows use Fiddler, but I find it overly complicated and too heavy for this purpose. Advanced REST client is really lightweight and focuses on making REST requests and seeing responses. If you want more, check out Fiddler.

1. Assuming you have already registered your application with Azure (a post on how to do this will be coming up soon), you can use simply paste the first request URL into your browser's address bar -- no need to use Advanced REST Client yet. The following is a GET request, so pasting it in the address bar works fine.

https://login.windows.net/common/oauth2/authorize?
response_type=code
&client_id=YOUR_CLIENT_ID_HERE
&resource=RESOURCE_HERE
&state=SOME_UNIQUE_STRING_HERE
&redirect_uri=YOUR_REDIRECT_URL_HERE

Resource looks like "https://dreamfactories-my.sharepoint.com" since you're making a call to the Files API endpoint, which is part of SharePoint/OneDrive for Business. If you're interested in Mail, you should call "https://outlook.office365.com" -- but for the purposes of this guide, we'll look at Files only.

Client id looks like "08632ea7-3847-48b8-b43a-b99568e3a2c4", state is some unique string used to prevent CSRF (you can put something like "12345" for demo purposes), and redirect URI is the URL that you registered in AAD for where your users will be sent after authorization. Note that for the purposes of this demo, you don't actually have to point to something valid that is own your domain. You can just put "https://www.google.com", as long as it matches with what you registered in Azure AD.

Press Enter and your browser redirects you to a login screen. Sign in with your tenant credentials. You'll want to use a user in the same tenant you registered this app to in Azure AD unless it's a multi-tenant app, in which case any other tenant would work too.

If your credentials are correct, you'll get redirected to the redirect URL you wanted, with the authorization code inserted as a query parameter at the end of the URL in the browser's address bar:

See that ?code= query parameter after https://www.google.com? That contains the authorization code. Copy that.

2. Make another request to obtain an access token so you can actually make calls to the resource. The POST request looks like this:

POST https://login.windows.net/common/oauth2/token

In the body of the request, 

client_id=YOUR_CLIENT_ID&
client_secret=YOUR_CLIENT_SECRET&
code=AUTHORIZATION_CODE_FROM_STEP_1&
grant_type=authorization_code&
redirect_uri=YOUR_REDIRECT_URL

Client ID is from Azure AD, Client Secret is the same as "Key" and is also from Azure AD. Code is the authorization code you obtained in Step 1. Grant type is authorization code because that is the information you are sending over. Redirect URI is what you registered on Azure AD.

The screenshot above shows what you should send and what you'd receive. Notice you get an access token. Copy that.

3. Make a request to get files from the user's OneDrive for Business. The GET request looks like this:

GET https://clippy-my.sharepoint.com/_api/v1.0/me/files

In the header of the request, put:

Authorization: Bearer {YOUR_ACCESS_TOKEN_FROM_STEP_2_HERE}

You'll be able to figure out what to put before /_api/files from the discovery service. A blog post on that will come soon. You'll get what looks like this:

Voila! The response contains your files!

As promised, posts on Azure AD app registration and Discovery Service will come soon. Check this blog regularly for updates!