Azure Functions - 它可以与 Office 加载项一起使用吗?

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

我敢肯定有些聪明人可以在 Azure 函数上做任何事情 运行 但是将它用于 运行ning Office 加载项是否有意义?我读到它非常适合 运行 小段代码,这就是我目前 运行 在 Azure 上作为 Web 应用程序使用的加载项。

您不会使用 Azure Functions 构建一个 add-in -- 但您绝对可以将它与常规网站结合使用,对于一些小型 server-side处理中。

具体示例:对于我和同事正在构建的 Add-in,我们需要代表用户获得用户对 post Gists 的 GitHub 权限。 GitHub 使用 "authorization code grant type" 流程(参见 https://developer.github.com/v3/oauth/),因此流程如下:

  1. 我们弹出一个对话框(使用 Add-ins 中的 recently-introduce 对话框 API)将用户定向到 https://github.com/login/oauth/authorize,它显示了一个漂亮的登录 UI.
  2. 如果用户登录并同意,GitHub 会向我们发回授权码。 client-side JavaScript 中的代码对我们没有什么好处,但如果我们将它传递给 Azure 函数,我们可以用它交换访问令牌。这必须在某些 server-side 代码(即网络服务器或 Azure Functions,作为网络服务器的 super-lightweight 形式)中完成,以便我们可以传入客户端密钥进行交换——这在 sniffable-out client-side JavaScript 中自然不会是秘密。因此将该代码放在服务器上。

如果您想知道该代码是什么样的,请看这里:

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();
    }
}