upload working

This commit is contained in:
stopflock
2025-07-19 19:49:17 -05:00
parent 956b9321a9
commit 2e1b504419
4 changed files with 194 additions and 20 deletions
+60 -9
View File
@@ -1,5 +1,6 @@
import 'dart:convert';
import 'dart:developer';
import 'dart:math' as math;
import 'package:oauth2_client/oauth2_client.dart';
import 'package:oauth2_client/oauth2_helper.dart';
@@ -7,7 +8,7 @@ import 'package:http/http.dart' as http;
/// Handles PKCE OAuth login with OpenStreetMap.
class AuthService {
static const String _clientId = 'HNbRD_Twxf0_lpkm-BmMB7-zb-v63VLdf_bVlNyU9qs';
static const String _clientId = 'Js6Fn3NR3HEGaD0ZIiHBQlV9LrVcHmsOsDmApHtSyuY';
static const _redirect = 'flockmap://auth';
late final OAuth2Helper _helper;
@@ -24,9 +25,13 @@ class AuthService {
_helper = OAuth2Helper(
client,
clientId: _clientId,
scopes: ['write_api'],
scopes: ['read_prefs', 'write_api'],
enablePKCE: true,
);
print('AuthService: Initialized with scopes: [read_prefs, write_api]');
print('AuthService: Client ID: $_clientId');
print('AuthService: Redirect URI: $_redirect');
}
Future<bool> isLoggedIn() async =>
@@ -36,14 +41,23 @@ class AuthService {
Future<String?> login() async {
try {
print('AuthService: Starting OAuth login...');
final token = await _helper.getToken();
if (token?.accessToken == null) {
print('AuthService: OAuth error - token null or missing accessToken');
log('OAuth error: token null or missing accessToken');
return null;
}
print('AuthService: Got access token, fetching username...');
_displayName = await _fetchUsername(token!.accessToken!);
if (_displayName != null) {
print('AuthService: Successfully fetched username: $_displayName');
} else {
print('AuthService: Failed to fetch username from OSM API');
}
return _displayName;
} catch (e) {
print('AuthService: OAuth login failed: $e');
log('OAuth login failed: $e');
rethrow;
}
@@ -54,21 +68,58 @@ class AuthService {
_displayName = null;
}
// Force a fresh login by clearing stored tokens
Future<String?> forceLogin() async {
print('AuthService: Forcing fresh login by clearing stored tokens...');
await _helper.removeAllTokens();
_displayName = null;
return await login();
}
Future<String?> getAccessToken() async =>
(await _helper.getTokenFromStorage())?.accessToken;
/* ───────── helper ───────── */
Future<String?> _fetchUsername(String accessToken) async {
final resp = await http.get(
Uri.parse('https://api.openstreetmap.org/api/0.6/user/details.json'),
headers: {'Authorization': 'Bearer $accessToken'},
);
if (resp.statusCode != 200) {
log('fetchUsername response ${resp.statusCode}: ${resp.body}');
try {
print('AuthService: Fetching username from OSM API...');
print('AuthService: Access token (first 20 chars): ${accessToken.substring(0, math.min(20, accessToken.length))}...');
final resp = await http.get(
Uri.parse('https://api.openstreetmap.org/api/0.6/user/details.json'),
headers: {'Authorization': 'Bearer $accessToken'},
);
print('AuthService: OSM API response status: ${resp.statusCode}');
print('AuthService: Response headers: ${resp.headers}');
if (resp.statusCode != 200) {
print('AuthService: fetchUsername failed with ${resp.statusCode}: ${resp.body}');
log('fetchUsername response ${resp.statusCode}: ${resp.body}');
// Try to get more info about the token by checking permissions endpoint
try {
print('AuthService: Checking token permissions...');
final permResp = await http.get(
Uri.parse('https://api.openstreetmap.org/api/0.6/permissions.json'),
headers: {'Authorization': 'Bearer $accessToken'},
);
print('AuthService: Permissions response ${permResp.statusCode}: ${permResp.body}');
} catch (e) {
print('AuthService: Error checking permissions: $e');
}
return null;
}
final userData = jsonDecode(resp.body);
final displayName = userData['user']?['display_name'];
print('AuthService: Extracted display name: $displayName');
return displayName;
} catch (e) {
print('AuthService: Error fetching username: $e');
log('Error fetching username: $e');
return null;
}
return jsonDecode(resp.body)['user']?['display_name'];
}
}
+25 -6
View File
@@ -11,6 +11,8 @@ class Uploader {
Future<bool> upload(PendingUpload p) async {
try {
print('Uploader: Starting upload for camera at ${p.coord.latitude}, ${p.coord.longitude}');
// 1. open changeset
final csXml = '''
<osm>
@@ -19,9 +21,15 @@ class Uploader {
<tag k="comment" v="Add surveillance camera"/>
</changeset>
</osm>''';
final csResp = await _post('/api/0.6/changeset/create', csXml);
if (csResp.statusCode != 200) return false;
final csId = csResp.body;
print('Uploader: Creating changeset...');
final csResp = await _put('/api/0.6/changeset/create', csXml);
print('Uploader: Changeset response: ${csResp.statusCode} - ${csResp.body}');
if (csResp.statusCode != 200) {
print('Uploader: Failed to create changeset');
return false;
}
final csId = csResp.body.trim();
print('Uploader: Created changeset ID: $csId');
// 2. create node
final nodeXml = '''
@@ -33,15 +41,26 @@ class Uploader {
<tag k="direction" v="${p.direction.round()}"/>
</node>
</osm>''';
print('Uploader: Creating node...');
final nodeResp = await _put('/api/0.6/node/create', nodeXml);
if (nodeResp.statusCode != 200) return false;
print('Uploader: Node response: ${nodeResp.statusCode} - ${nodeResp.body}');
if (nodeResp.statusCode != 200) {
print('Uploader: Failed to create node');
return false;
}
final nodeId = nodeResp.body.trim();
print('Uploader: Created node ID: $nodeId');
// 3. close changeset
await _put('/api/0.6/changeset/$csId/close', '');
print('Uploader: Closing changeset...');
final closeResp = await _put('/api/0.6/changeset/$csId/close', '');
print('Uploader: Close response: ${closeResp.statusCode}');
print('Uploader: Upload successful!');
onSuccess();
return true;
} catch (_) {
} catch (e) {
print('Uploader: Upload failed with error: $e');
return false;
}
}