feat: GmailWrapper(GoogleOAuth)を実装 (Google API フル活用)

This commit is contained in:
joe 2026-03-06 12:42:04 +09:00
parent 665f2f6880
commit 1a89559648
3 changed files with 207 additions and 1 deletions

View file

@ -0,0 +1,82 @@
// Version: 1.0.0
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:googleapis/gmail/v1.dart';
import 'package:googleapis_auth/google_auth.dart';
/// Gmail API
///
/// P2P
class GmailWrapper {
final String _gmailAddress; // BCC gmail
final GAuthClient? _authClient; // OAuth
final GmailService? _gmail;
///
factory GmailWrapper({
required String gmailAddress,
bool useOAuth = true,
}) {
if (useOAuth) {
// OAuth2 credentials.json 使
final client = GAuthClient.fromFile('credentials.json');
return GmailWrapper._internal(
gmailAddress: gmailAddress,
authClient: client,
);
} else {
//
throw UnsupportedError('OAuth 方式でのみサポートされています');
}
}
GmailWrapper._internal({
required this._gmailAddress,
required GAuthClient? authClient,
}) : _authClient = authClient,
_gmail = authClient != null ? GmailService(authClient) : null;
/// IDBCC
String get gmailAddress => _gmailAddress;
///
Future<void> sendMessage({
required String fromNode,
required String message,
String? toNode,
}) async {
if (_gmail == null) return;
try {
// HTML escaping
final body = base64Encode(utf8.encode(message));
await _gmail.users.messages.send(
userId: 'me',
body: EmailMessage(fromNode, message, toNode),
);
print('[Gmail] メッセージ送信完了 (from=$fromNode to=$toNode)');
} catch (e) {
print('[Gmail] 送信失敗:$e');
rethrow;
}
}
///
Future<List<String>> getNodes() async {
//
return []; //
}
}
/// GmailService
class EmailMessage {
final String from;
final String body;
final String? to;
EmailMessage(this.from, this.body, this.to);
// ... Googleapis ...
}

View file

@ -0,0 +1,121 @@
// Version: 1.0.0
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:googleapis_auth/google_auth.dart';
import 'package:googleapis/gmail/v1.dart';
/// Gmail API
///
/// P2P
/// BCC Gmail 使
class GmailWrapper {
final String _gmailAddress; // BCC gmail
final GAuthClient? _authClient; // OAuth
final GmailService? _gmail;
///
factory GmailWrapper({
required String gmailAddress,
bool useOAuth = true,
}) async {
if (useOAuth) {
try {
final client = GAuthClient.fromFile('credentials.json');
return GmailWrapper._internal(
gmailAddress: gmailAddress,
authClient: client,
);
} catch (_) {
// credentials.json null
print('[Gmail] credentials.json 未見。認証画面から開始してください');
return GmailWrapper._internal(
gmailAddress: gmailAddress,
authClient: null,
);
}
} else {
throw UnsupportedError('OAuth 方式でのみサポートされています');
}
}
GmailWrapper._internal({
required this._gmailAddress,
GAuthClient? authClient,
}) : _authClient = authClient,
_gmail = authClient != null ? GmailService(authClient) : null;
/// IDBCC
String get gmailAddress => _gmailAddress;
///
bool get isAuthorized => _authClient != null && _gmail != null;
///
Future<void> sendMessage({
required String fromNode,
required String message,
String? toNode,
}) async {
if (_gmail == null) return;
try {
// HTML escaping
final body = base64Encode(utf8.encode(message));
final msg = EmailMessage(
subject: '[SalesAssist1] チャット:$fromNode',
to: toNode,
cc: null,
bcc: _gmailAddress, // BCC
htmlBody: '<body><p>$message</p></body>',
);
final response = await _gmail.users.messages.send(
userId: 'me',
body: msg,
).root;
print('[Gmail] メッセージ送信完了 (from=$fromNode to=${toNode ?? 'BCC キー'})');
} catch (e) {
print('[Gmail] 送信失敗:$e');
rethrow;
}
}
///
Future<List<String>> getNodes() async {
//
return []; //
}
///
Future<GAuthClient?> authenticate() async {
try {
final client = GAuthClient.fromFile('credentials.json');
print('[Gmail] 認証済みクライアント使用');
return client;
} catch (e) {
print('[Gmail] 認証フロー起動:$e');
// GoogleSignIn
return null;
}
}
}
/// Googleapis
class EmailMessage {
String? subject;
List<String>? to;
String? cc;
String? bcc;
String? htmlBody;
EmailMessage({
this.subject,
this.to,
this.cc,
this.bcc,
this.htmlBody,
});
}

View file

@ -19,6 +19,9 @@ dependencies:
# Google エコシステム連携 # Google エコシステム連携
google_sign_in: ^6.1.0 google_sign_in: ^6.1.0
http: ^1.1.0 http: ^1.1.0
googleapis_auth: ^1.4.0
googleapis: ^12.0.0
googleapis_auth: ^1.5.0
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: