none
Authenticating Sharepoint from Office add in RRS feed

  • Question

  • Hello forums!

    I'm trying to create an add in for Office, and I've been trying to authenticate towards a SharePoint site, but I cannot seem to find anything that works. I've tried the following:

    app.initialize = function () {
    
    	var response = {"status": "none", "accessToken": ""};
    
    	var authContext = new AuthenticationContext(config);
    
    	var isCallback = authContext.isCallback(window.location.hash);
    	authContext.handleWindowCallback();
    
    	if (isCallback && !authContext.getLoginError()) {
    		window.location = authContext._getItem(authContext.CONSTANTS.STORAGE.LOGIN_REQUEST);
    	}
    	
    	app.user = authContext.getCachedUser();
    	if (app.user) {
    		authContext.acquireToken("https://{tenant}.sharepoint.com/sites/{site}", function (error, token) {
    			if (error || !token) {
    				response.status = "error";
    				response.accessToken = null;
    			} else {
    				response.status = "success";
    				response.accessToken = token;
    			}
    		});
    		$('#info').html("Status: " + response.status + " - Token: " + response.accessToken);
    	} else {
    		authContext.login();
    	}
    };

    If the user isn't logged in, I'm asking him to login. If app.user is logged in, the we need to acquire a token to the SharePoint site. This isn't working, and I don't know why, or what's missing? 

    Can I not authenticate SharePoint like this? How else would I do it?

    Friday, August 12, 2016 11:26 AM

All replies

  • Hi ChristianHaase,

    To check whether it is specific to Office Add-ins, I suggest you make a test with this function in a simple web application. Could this AuthenticationContext work in a simple web application?

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, August 15, 2016 6:06 AM
  • I've decided to use AngularJS instead with the adal-angular template that's has been Made available from the Yoeman office generator. I still cannot make it authenticate towards a SharePoint site.

    It's still an Office add in so Instead of making a new thread, I'll just keep posting in this one.

    Here's my adal configurations:

    function adalConfigurator($httpProvider, adalProvider, appId) {
      var adalConfig = {
        tenant: 'workpoint365.com',
        clientId: appId,
        endpoints: {     
          'https://graph.microsoft.com':'https://graph.microsoft.com'
      }
      // cacheLocation: 'localStorage', // enable this for IE, as sessionStorage does not work for localhost. 
    };
    
    adalProvider.init(adalConfig, $httpProvider);

    When the add in loads, the user is prompted to login, and when he does I'll try to get some lists from SharePoint.

    Here's how I try to contact the SharePoint REST API:

    function callApi(){
      var deferred = $q.defer();
      $http.get('https://some-tenant.sharepoint.com/sites/some-site')
        .then(function successCallback (response) {
          deferred.resolve(response.data);
        }, function errorCallback (response) {
          deferred.reject(response);
        });
      return deferred.promise();
    }

    This code stump is places within an Angular service, which gets called from my home controller:

    function homeController (dataService) {
      var vm = this;
      vm.title = 'test title';
      vm.listResults = {};
      
      getLists();
    
      function getLists () {
        dataService.callApi();
      }
    }

    If I look at the console, it almost looks like the application, or the actual call to the REST API, is trying to redirect me to some sort of login. I've been searching for the scarce documentation, and managed to find out that the token is automatically applied to the header, if it exists in the localStorage cache.

    Perhaps the token is not for SharePoint but for the graph API as that's in my adal configurations? I'm having a hard time figuring this out as I can't seem to find any documentation on this. It seems like this should be a simple task as both technologies are Microsoft. I simply cannot understand why this is so difficult? 

    Perhaps it's just me, but I can't seem to figure this out. Do I need to get a token to SharePoint? How then? Can I add SharePoint as a resource some where or some how? Can I run acquireToken towards SharePoint?


    Tuesday, August 16, 2016 6:27 AM
  • Hi ChristianHaase,

    Have you tried your requirements in a simple web application? There are multiple parts in your issues like Office Add-ins, SharePoint REST API and graph API. To narrow down your issue, we need to check which part causes this issue.

    So, I suggest you create a simple web page and make a test with SharePoint REST API to check whether it works.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, August 17, 2016 8:54 AM
  • Hello Edward.

    I still haven't tried to test this in a web app. I've been busy trying multiple other solutions. I think I've managed to get something partially working now.

    I stepped away from the adal-angular template provided by the Yeoman Office generator, and went back to make a simple Apps for Office project using the Visual Studio provided template.

    After hooking this up to be an angular app, I've got a new issue that I haven't seen before.

    I've gone ahead and installed a certificate as well, as this gave me some problems. Now when I load my app, I don't have to "Continue anyways" because of a certificate error.

    After hooking everything up correctly, using this example, I've got a whole other problem.

    I get the typical login screen in the task pane, just like the image below.

    When I press my username, I get to enter my credentials BUT I also get a popup of internet explorer, prompting me to sign in, just as the task pane prompt. When I sign in through internet explorer, my app runs in that new browser that my app made. If I sign in through the task pane, I get a new internet explorer popup, showing me this error message.

    This looks quite strange, and I'm not sure how to solve this situation. It's a bit strange that It creates the popup as well as the task pane logging screen. I've read that task pane add ins doesn't support logging in through the task pane, because of IFrames, hence the reason of the popup.

    This is some quite strange behavior, and I'd be more than willing to share my code if that could help. 

    But no, I haven't made a web app to test this, as I've been trying this new technique instead.

    Wednesday, August 17, 2016 9:32 AM
  • For testing purposes I tried to remove all Office related code, and ran the app directly in my browser, as it runs on https://localhost:{some-port}.

    I can now see that there's something wrong with the request as it's a CORS request. I get this message

    So how exactly do I configure this to allow CORS?

    Wednesday, August 17, 2016 11:10 AM
  • Hi ChristianHaase,

    For CORS in SharePoint, I think the link below might be useful to you.

    #SharePoint Hosted App For Reading All the List From the Host Web Using Cross-Domain Calls

    http://www.c-sharpcorner.com/UploadFile/Roji.Joy/sharepoint-hosted-app-for-reading-all-the-list-from-the-host/

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, August 18, 2016 11:15 AM
  • Hello Edward Z.

    This is not an app for SharePoint, it's and app for Office.

    Thursday, August 18, 2016 11:54 AM
  • Hi ChristianHaase,

    To check whether it is related with Office Add-ins, I suggest you create a simple web project like MVC or Web Form to run your code, will it work or not?

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, August 19, 2016 5:57 AM