Search code examples
office365skype-for-businessskypedeveloper

Skype for business web sdk 401 Unauthorised when trying to login


I'm trying to create an app using Skype for business web SDK.

Here is the part of login code:

var config = {
    apiKey: 'a42fcebd-5b43-4b89-a065-74450fb91255', // SDK
    apiKeyCC: '9c967f6b-a846-4df2-b43d-5167e47d81e1' // SDK+UI
};

var Skype;
var app;
console.log(Skype);

Skype.initialize({ apiKey: config.apiKey }, function(api) {
    window.skypeWebAppCtor = api.application;
    window.skypeWebApp = new api.application();
    Application = api.application;
    app = new Application();
    console.log("app", app);
    skypeSignIn();
    window.skypeWebApp.signInManager.state.changed(function(state) {
        console.log("in",state);
    });
}, function(err) {
    console.log(err);
    console.log("cannot load the SDK");
});

var skypeSignIn = function() {
    app.signInManager.signIn({
        "client_id": "my-GUID", // GUID obtained from Azure app registration.
        "origins": ["https://webdir.online.lync.com/autodiscover/autodiscoverservice.svc/root"],
        "cors": true,
        "redirect_uri": 'https://freshservice.com',
        "version": 'freshservice skype for business integration'
    }).then(function() {
        alert('Logged in!');
    });

When I try the above code it gives an error 401 on origin URL -

GET https://webdirin1.online.lync.com/Autodiscover/AutodiscoverService.svc/root/oauth/user 401 (Unauthorized)

How can this be fixed ?


Solution

  • First of all, you need to get your app authorised using Azure Active Directory. This will return an access token which is then sent with every request. Without this access token, all requests would be unauthorised. To obtain the access token you can refer this resource on how to Set up Skype Web SDK Online.

    Then after your app is authorised, then you can use the Skype.Initialize and SignIn. This would actually sign you in to Skype For Business.

    Apart from this, if you have already authorised, getting an unauthorised response from the server is normal. As the app first tries to log you in. Than if the server returns unauthorised, it then initiates the authorisation process.