【问题标题】:Using google apis in firebase funcion在 firebase 函数中使用 google apis
【发布时间】:2019-04-11 22:10:59
【问题描述】:

我想从 firebase 函数中使用 google iot core api。 一切正常,但速度很慢。我认为是由于需要进行一次非常调用的身份验证过程。有没有办法加快速度?

现在我有这个:

function getClient(cb) {
    const API_VERSION = 'v1';
    const DISCOVERY_API = 'https://cloudiot.googleapis.com/$discovery/rest';
    const jwtAccess = new google.auth.JWT();
    jwtAccess.fromJSON(serviceAccount);
    // Note that if you require additional scopes, they should be specified as a
    // string, separated by spaces.
    jwtAccess.scopes = 'https://www.googleapis.com/auth/cloud-platform';
    // Set the default authentication to the above JWT access.
    google.options({ auth: jwtAccess });
    const discoveryUrl = `${DISCOVERY_API}?version=${API_VERSION}`;
    google.discoverAPI(discoveryUrl, {}).then( end_point => {
        cb(end_point);
    });
}

这让我可以这样做:

export function sendCommandToDevice(deviceId, subfolder, mqtt_data) {
    const cloudRegion = 'europe-west1';
    const projectId = 'my-project-id;
    const registryId = 'my-registry-id';

    getClient(client => {
        const parentName = `projects/${projectId}/locations/${cloudRegion}`;
        const registryName = `${parentName}/registries/${registryId}`;
        const binaryData = Buffer.from(mqtt_data).toString('base64');
        const request = {
            name: `${registryName}/devices/${deviceId}`,
            binaryData: binaryData,
            subfolder: subfolder
        };
        client.projects.locations.registries.devices.sendCommandToDevice(request,
            (err, data) => {
                if (err) {
                    console.log('Could not update config:', deviceId);
                }
            });
    });
}

我发现加快速度的方法是避免进行身份验证。我已经解决了这个问题:

const google = new GoogleApis();
const API_VERSION = 'v1';
const DISCOVERY_API = 'https://cloudiot.googleapis.com/$discovery/rest';
const jwtAccess = new google.auth.JWT();
jwtAccess.fromJSON(serviceAccount);
// Note that if you require additional scopes, they should be specified as a
// string, separated by spaces.
jwtAccess.scopes = 'https://www.googleapis.com/auth/cloud-platform';
// Set the default authentication to the above JWT access.
google.options({ auth: jwtAccess });
const discoveryUrl = `${DISCOVERY_API}?version=${API_VERSION}`;
var googleClient;
google.discoverAPI(discoveryUrl, {}).then( client => {
    //cb(end_point);
    googleClient = client;
});

// Returns an authorized API client by discovering the Cloud IoT Core API with
// the provided API key.
function getClient(cb) {
    cb(googleClient);
}

但是当客户端过期时会发生什么?从firebase函数中使用google api有什么好的解决方案吗?

【问题讨论】:

    标签: javascript firebase google-cloud-functions google-authentication


    【解决方案1】:

    问题可能是发现部分。有一个直接的 IoT Core 管理 REST API,所以你不必使用发现......我想。我没有使用过 Firebase 函数,但它们大致相当于谷歌云函数,最终也可能在这里工作。我们(在我们所做的现场演示中)运行的代码是here,如果您想修补一下,看看您是否可以在 Firebase 函数中运行它。

    【讨论】:

      猜你喜欢
      • 2020-04-24
      • 2020-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-13
      • 2020-11-25
      • 2018-07-08
      相关资源
      最近更新 更多