Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Functions - Can it be used with Office Add-in's?

I'm sure there are clever people who can make anything run on Azure functions but does it make sense at all to use it for running Office Add-in's? I read that it's ideal for running small pieces of code and that's what my add-in currently running on Azure as an web app is.

like image 981
DutchDan Avatar asked Oct 17 '22 18:10

DutchDan


1 Answers

You wouldn't use Azure Functions to build an add-in -- but you absolutely could use it in conjunction with a regular website, for some small server-side processing.

Concrete example: for an Add-in that a colleague and I were building, we needed to obtain a user's GitHub permissions to post Gists on the user's behalf. GitHub uses an "authorization code grant type" flow (see https://developer.github.com/v3/oauth/), so the flow would be as follows:

  1. We pop a dialog (using the recently-introduce Dialog API in Add-ins) to direct the user to https://github.com/login/oauth/authorize, which shows a pretty login UI.
  2. If the user signs in and consents, GitHub sends an authorization code back to us. The code does little good for us in client-side JavaScript, but if we pass it to an Azure Function, we can exchange it for an access token. This must be done in some server-side code (i.e., a web server, or Azure Functions, as a super-lightweight form of a web server) so that we can pass in a Client Secret for the exchange -- which quite naturally wouldn't be secret in sniffable-out client-side JavaScript. Hence putting that code on the server.

If you're curious to see what that code was like, here it is:

var request = require('request');

module.exports = function (context, data) {
    context.log('code: ' + data.code);
    if ('code' in data) {
        request.post({
            url: 'https://github.com/login/oauth/access_token',
            json: {
                client_id: '################',
                client_secret: '################',
                redirect_uri: '################',
                code: data.code
            }
        }, function (err, httpResponse, body) {
            if (err) {
                context.log('error: ' + err);
                context.res = {
                    body: {
                        status: 500,
                        error: err
                    }
                }
            }
            else {
                context.res = { body: body };
            }

            context.done();
        });
    }
    else {
        context.res = {
            status: 400,
            body: { error: 'Please pass the GitHub code in the input object' }
        };

        context.done();
    }
}
like image 200
Michael Zlatkovsky - Microsoft Avatar answered Oct 21 '22 02:10

Michael Zlatkovsky - Microsoft