/XamarinGraphSample

Ths code sample shows how to use Microsoft Graph SDK with your Xamarin.Forms application.

Primary LanguageC#MIT LicenseMIT

Xamarin Graph Sample

Working with Microsoft Graph SDK in Xamarin.Forms

This code sample shows how to use Microsoft Graph SDK with your Xamarin.Forms application.

Original Docs Link: https://docs.microsoft.com/en-us/graph/tutorials/xamarin

Dependencies

  • Microsoft.Graph
  • Microsoft.Graph.Core
  • Microsoft.Identity.Client

Prerequisites

A personal Microsoft Account or an Office (Microsoft) 365 Subscription.

  • Sign up for a personal Microsoft Account here.
  • Sign up for the Office 365 Developer Program here.

TODO: Create OAuthSettings.cs file

To save my keys and ID, I had to add that OAuthSettings.cs to my .gitignore file. No worries, it's simple. In the Models Folder, create a C Sharp file called OAuthSettings.cs This is what it will look like. Be sure to replace <yourAppID> and <yourAppBundleID> with the Application (Client ID) of your Azure AD Application and the ID of your app bundle respectively.

public static class OAuthSettings
{
    public const string ApplicationId = "<yourAppID>";
    public const string Scopes = "User.Read Calendars.Read";
    public const string RedirectUri = "msauth://<yourAppBundleID>";
}

Also, be sure to go into your AndroidManifest.xml file to change the package value to what you would like it to be (it should be the value you used when setting up the Azure AD Application). Do the same for your iOS Project-that would be the Info.plist file (Bundle Identifier).

From here on, all things should be good.

Changes I made to my code

I followed the official documentation up till here then I realized the user information was not showing after I finished the sign-in process (I might have missed some steps) so I modified my InitializeGraphClientAsync() method to initialize sign in if no account is found.

var interactiveRequest = PCA.AcquireTokenInteractive(Scopes);

if (AuthUIParent != null)
{
    interactiveRequest = interactiveRequest
        .WithParentActivityOrWindow(AuthUIParent);
}

var interactiveAuthResult = await interactiveRequest.ExecuteAsync();
//recursive call to the same function
await InitializeGraphClientAsync();

Scopes

So that I am able to get my graph API calls to work fine, I created an ApplicationScopes class that contains fields of strings. It looks something like the image below. This I will be using for my various scenarios.

Application Scopes

For more information on scopes and permission, visit this link.

Issues

Running on an iOS emulator gave me error telling me to enable KeyChain access. It works fine deploying to a phone. Trying to fix with this doc and this. This part of the former link solved it for me. Xamarin iOS Entitlement