gpt4 book ai didi

flutter - 如何将 Stackdriver 与 Flutter 结合使用

转载 作者:行者123 更新时间:2023-12-03 02:47:15 26 4
gpt4 key购买 nike

我正在开发一个 flutter 应用程序。有什么方法可以直接从 Flutter/Dart 写入数据到 StackDriver。

最佳答案

您可以使用:

https://pub.dev/packages/googleapis

例子:

import 'package:googleapis/logging/v2.dart';
import 'package:googleapis_auth/auth.dart';
import 'package:googleapis_auth/auth_io.dart';

final _credentials = new ServiceAccountCredentials.fromJson(r'''
{
... YOUR CREDENTIALS ....
}
''');

const _SCOPES = const ['https://www.googleapis.com/auth/logging.write'];

clientViaServiceAccount(_credentials, _SCOPES).then((httpClient) {
var errorReporting = new LoggingApi(httpClient);

// Resource
var resource = new MonitoredResource();
resource.type = 'global';
resource.labels = {
'project_id': 'PROJECT_ID',
};

// Prepare new log entry.
LogEntry logEntry = new LogEntry();
logEntry.logName = "projects/PROJECT_ID/logs/LOG_ID";
logEntry.jsonPayload = {'message': 'YOUR MESSAGE'};
logEntry.resource = resource;

var request = new WriteLogEntriesRequest();
request.entries = [logEntry];

errorReporting.entries.write(request).then((_) {
print('log entry sent');
});
});

但是,请注意,您需要将您的凭据放在您的 Flutter 应用程序上,“黑客”可以窃取它。因此,最好将日志发送到后端,然后再将其发送到 StackDriver。

关于flutter - 如何将 Stackdriver 与 Flutter 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56428955/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com