如何在 Node.js 上使用 gRPC 调用 Home Graph API
How to call the Home Graph API with gRPC on Node.js
给定可用的协议缓冲区定义:https://github.com/googleapis/googleapis/blob/master/google/home/graph/v1/homegraph.proto
如何在 Node.js 上使用 gRPC 调用 Home Graph API RPC endpoint 以通过单个连接多路复用并发 API 方法调用?
您可以使用 @grpc-js in combination with Application Default Credentials 初始化凭据。
$ npm install @grpc/grpc-js
$ npm install google-auth-library
$ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account-key.json
const grpc = require('@grpc/grpc-js');
const { GoogleAuth } = require('google-auth-library');
async function getCredentials() {
const sslCredentials = grpc.credentials.createSsl();
const googleAuth = new GoogleAuth({
scopes: 'https://www.googleapis.com/auth/homegraph'
});
const authClient = await googleAuth.getClient();
const callCredentials = grpc.credentials.createFromGoogleCredential(
authClient
);
const credentials = grpc.credentials.combineChannelCredentials(
sslCredentials,
callCredentials
);
return credentials;
}
使用 google-proto-files with @grpc/proto-loader 加载 Home Graph 服务 protobuf 定义及其依赖项。
const protoLoader = require('@grpc/proto-loader');
const protos = require('google-proto-files');
async function getHomegraph() {
const homegraphProto = await protoLoader.load(
protos.getProtoPath('home/graph', 'v1', 'homegraph.proto'), {
includeDirs: [protos.getProtoPath('..')]
}
);
const homegraph = grpc.loadPackageDefinition(
homegraphProto
).google.home.graph.v1;
return homegraph;
}
最后初始化客户端存根以调用 HomeGraphApiService 方法。
(async function() {
const credentials = await getCredentials();
const homegraph = await getHomegraph();
const homegraphService = new homegraph.HomeGraphApiService(
'homegraph.googleapis.com', credentials
);
homegraphService.sync({
agentUserId: 'AGENT_USER_ID'
}, function(err, result) {
if (err) {
console.error(err);
} else {
console.log(result);
}
});
// homegraphService.query();
// homegraphService.requestSyncDevices();
// homegraphService.reportStateAndNotification();
// homegraphService.deleteAgentUser();
})();
请注意,默认情况下 Channel
implementation will reuse existing channels from a global pool if the parameters (address, credentials and options) are the same. You can alter this behavior with the grpc.use_local_subchannel_pool
option。
给定可用的协议缓冲区定义:https://github.com/googleapis/googleapis/blob/master/google/home/graph/v1/homegraph.proto
如何在 Node.js 上使用 gRPC 调用 Home Graph API RPC endpoint 以通过单个连接多路复用并发 API 方法调用?
您可以使用 @grpc-js in combination with Application Default Credentials 初始化凭据。
$ npm install @grpc/grpc-js
$ npm install google-auth-library
$ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account-key.json
const grpc = require('@grpc/grpc-js');
const { GoogleAuth } = require('google-auth-library');
async function getCredentials() {
const sslCredentials = grpc.credentials.createSsl();
const googleAuth = new GoogleAuth({
scopes: 'https://www.googleapis.com/auth/homegraph'
});
const authClient = await googleAuth.getClient();
const callCredentials = grpc.credentials.createFromGoogleCredential(
authClient
);
const credentials = grpc.credentials.combineChannelCredentials(
sslCredentials,
callCredentials
);
return credentials;
}
使用 google-proto-files with @grpc/proto-loader 加载 Home Graph 服务 protobuf 定义及其依赖项。
const protoLoader = require('@grpc/proto-loader');
const protos = require('google-proto-files');
async function getHomegraph() {
const homegraphProto = await protoLoader.load(
protos.getProtoPath('home/graph', 'v1', 'homegraph.proto'), {
includeDirs: [protos.getProtoPath('..')]
}
);
const homegraph = grpc.loadPackageDefinition(
homegraphProto
).google.home.graph.v1;
return homegraph;
}
最后初始化客户端存根以调用 HomeGraphApiService 方法。
(async function() {
const credentials = await getCredentials();
const homegraph = await getHomegraph();
const homegraphService = new homegraph.HomeGraphApiService(
'homegraph.googleapis.com', credentials
);
homegraphService.sync({
agentUserId: 'AGENT_USER_ID'
}, function(err, result) {
if (err) {
console.error(err);
} else {
console.log(result);
}
});
// homegraphService.query();
// homegraphService.requestSyncDevices();
// homegraphService.reportStateAndNotification();
// homegraphService.deleteAgentUser();
})();
请注意,默认情况下 Channel
implementation will reuse existing channels from a global pool if the parameters (address, credentials and options) are the same. You can alter this behavior with the grpc.use_local_subchannel_pool
option。