Is there any WORKING example how to connect to Office 365 REST API and OAuth?

JrgAckermannJrgAckermann DEMember, University

Hi,

i'm searching this for hours now, and nothing is working. Only broken Nuget Packages, Android examples that should work on iOS, ...

But nothing that shows the OAuth authentication flow and a simple query to Office 365... Is it really that difficult?

Best Regards
Jörg

Answers

  • JorgeSanzJorgeSanz USMember

    Hi! any luck with your search?
    Any good article to read please?

  • Rob.0184Rob.0184 USUniversity ✭✭

    Months later, I'm in the exact same position. Extremely disappointing.

  • mbalsam_nycmbalsam_nyc USMember ✭✭

    Did anyone get anywhere with this?

  • DaveHuntDaveHunt USMember ✭✭✭✭✭

    This is covered party completely in the Microsoft documentation: https://msdn.microsoft.com/en-us/office/office365/howto/common-app-authentication-tasks

  • mbalsam_nycmbalsam_nyc USMember ✭✭
    edited March 2016

    We found several articles that dance around the topic.

    Add Storage to Your Apps with Office 365
    https://blog.xamarin.com/add-storage-to-your-apps-with-office-365/

    Put Some Office 365 in Your Apps
    http://forums.xamarin.com/discussion/21367/put-some-office-365-in-your-apps

    ** Office 365 Client App using Xamarin.Android and Office 365 Client Libraries**
    https://github.com/mayur-tendulkar/O365Client

    But what were looking for is an example of how to get an OAuth2 token for an IOS application.

    Also see that the Xamarin.Auth component works with OAuth2, but how to connect the dots is the question.

    We got this sample working

    Office 365 Connect app for iOS
    https://github.com/OfficeDev/O365-iOS-Connect

    But its in Objective-C

    At the bottom of that github page they have a great collection of info

  • DaveHuntDaveHunt USMember ✭✭✭✭✭

    Have you seen this sample found in this blog post? The information is for authenticating with Azure Active Directory and the sample calls the Azure AD Graph API, but Office 365 uses the same authentication flow.

  • mbalsam_nycmbalsam_nyc USMember ✭✭

    Thank you @DaveHunt. I think you found it.

  • DaveHuntDaveHunt USMember ✭✭✭✭✭

    Your welcome!

  • JuanMosqueda.1547JuanMosqueda.1547 USMember ✭✭
    edited March 2016

    Based on this blog post cloudidentity.com/blog/2015/07/22/using-adal-3-x-with-xamarin-forms/

    I was able to build an app that connects from a xamarin ios application (not using Forms). The way i did it was to modify the sample like this

    `public partial class ViewController : UIViewController
    {
    public ViewController(IntPtr handle)
    : base(handle)
    {
    }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
    
            AuthenticateWithEWS();
        }
    
        private async void AuthenticateWithEWS()
        {
            try
            {
                var ac = new AuthenticationContext("https://login.microsoftonline.com/common");
    
                var platformParameters = new PlatformParameters(this);
    
                var ar = await ac.AcquireTokenAsync(
                    "https://outlook.office365.com/",
                    "the_client_id_we_get_from_azure",
                    new Uri("http://www.the_app_url.com/"),
                    platformParameters);
    
                var result = ar.AccessToken;
    
                if (result == null)
                {
    
                }
            }
            catch (Exception ex)
            {
                return;
            }
        }
    }`
    

    I also had to modify the Info.plist file to include the following entries

    <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict> <key>LSApplicationQueriesSchemes</key> <array> <string>msauth</string> </array>

    However, after all this is done and the app runs, i eventually get an error on the console log and the authentication page is not displayed.

    2016-03-10 13:41:11.340 EwsOAuthTest[54209:9397460] -canOpenURL: failed for URL: "msauth://" - error: "(null)"

    I've been unable to fix this, it looks like the webview doesn't support loading urls with the msauth:// scheme?

  • JuanMosqueda.1547JuanMosqueda.1547 USMember ✭✭
    edited March 2016

    Is worth noting that when i run the same code from a windows forms desktop application everything works as expected and i can get the authentication dialog and then get the token back. So this is an ios only issue.

    Any help will be greatly appreciated.

    Below a sample Windows Forms desktop app which is able to authenticate correctly

    `class EwsAuthManager
    {
    public async void GetToken()
    {
    var token = "";
    try
    {
    var authority = "https://login.microsoftonline.com/common";
    var authContext = new AuthenticationContext(authority);

                IPlatformParameters pms = new PlatformParameters(PromptBehavior.Always);
    
                var ret =
                    await
                        authContext.AcquireTokenAsync("https://outlook.office365.com/", "the_client_id_we_get_from_azure",
                            new Uri("http://www.the_app_url.com/"), pms);
    
                token = ret.AccessToken;
            }
            catch (AdalException ex)
            {
                MessageBox.Show(ex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (string.IsNullOrEmpty(token) == false)
                {
                    MessageBox.Show(string.Format("Token returned: [{0}]", token));
                }
            }
        }
    }`
    

    `public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

        private void Form1_Load(object sender, EventArgs e)
        {
            var em = new EwsAuthManager();
            em.GetToken();
        }
    }`
    
  • JuanMosqueda.1547JuanMosqueda.1547 USMember ✭✭

    I finally got it working. I have attached a sample app that does a basic login and gets back a token.

    Once you get the login dialog the first time and enter your credentials the dialog won't be displayed again if you go through the same code, instead it just silently returns the same token that was generated on the first call.

    Attached is my sample code in case anyone is interested

Sign In or Register to comment.