From 77c3b94ff1bbe3dc09172e38c769f61d233e08d7 Mon Sep 17 00:00:00 2001 From: Tobias Zwick Date: Wed, 22 Nov 2023 03:07:56 +0100 Subject: [PATCH 1/4] replace oauth 1.0a with oauth 2.0 the ALLOW_NOTHING_TOKEN is only preliminary because due to https://github.com/openstreetmap/openstreetmap-website/issues/4334 it is not possible to create a token that has no permissions --- build.gradle | 14 ++-- .../osmapi/changesets/ChangesetsApi.java | 6 +- libs/core/build.gradle | 1 - .../de/westnordost/osmapi/OsmConnection.java | 64 +++++-------------- .../westnordost/osmapi/OsmConnectionTest.java | 2 +- .../de/westnordost/osmapi/map/MapDataApi.java | 10 +-- .../osmapi/map/MapDataHistoryApi.java | 8 +-- .../de/westnordost/osmapi/notes/NotesApi.java | 8 +-- .../osmapi/notes/NotesApiTest.java | 11 +--- .../osmapi/traces/GpsTracesApiTest.java | 4 +- .../de/westnordost/osmapi/user/UserApi.java | 4 +- .../osmapi/ConnectionTestFactory.java | 56 ++++++---------- 12 files changed, 67 insertions(+), 121 deletions(-) diff --git a/build.gradle b/build.gradle index ae1d5d6..e7a6b4b 100644 --- a/build.gradle +++ b/build.gradle @@ -13,11 +13,11 @@ subprojects { } ext { - all_version = 4.3 - core_version = 2.0 - changesets_version = 2.3 - user_version = 2.0 - traces_version = 2.0 - notes_version = 2.0 - map_version = 2.3 + all_version = 5.0 + core_version = 3.0 + changesets_version = 3.0 + user_version = 3.0 + traces_version = 3.0 + notes_version = 3.0 + map_version = 3.0 } \ No newline at end of file diff --git a/libs/changesets/src/main/java/de/westnordost/osmapi/changesets/ChangesetsApi.java b/libs/changesets/src/main/java/de/westnordost/osmapi/changesets/ChangesetsApi.java index f6adf63..7a561e2 100644 --- a/libs/changesets/src/main/java/de/westnordost/osmapi/changesets/ChangesetsApi.java +++ b/libs/changesets/src/main/java/de/westnordost/osmapi/changesets/ChangesetsApi.java @@ -37,7 +37,7 @@ public ChangesetInfo get(long id) String query = CHANGESET + "/" + id + "?include_discussion=true"; try { - boolean authenticate = osm.getOAuth() != null; + boolean authenticate = osm.getOAuthAccessToken() != null; osm.makeRequest(query, authenticate, new ChangesetParser(handler)); } catch(OsmNotFoundException e) @@ -59,7 +59,7 @@ public void find(Handler handler, QueryChangesetsFilters filters) String query = filters != null ? "?" + filters.toParamString() : ""; try { - boolean authenticate = osm.getOAuth() != null; + boolean authenticate = osm.getOAuthAccessToken() != null; osm.makeRequest(CHANGESET + "s" + query, authenticate, new ChangesetParser(handler)); } catch(OsmNotFoundException e) @@ -196,7 +196,7 @@ public void getData(long id, MapDataChangesHandler handler) */ public void getData(long id, MapDataChangesHandler handler, MapDataFactory factory) { - boolean authenticate = osm.getOAuth() != null; + boolean authenticate = osm.getOAuthAccessToken() != null; osm.makeRequest(CHANGESET + "/" + id + "/download", authenticate, new MapDataChangesParser(handler, factory)); } } diff --git a/libs/core/build.gradle b/libs/core/build.gradle index a7e9922..406b77c 100644 --- a/libs/core/build.gradle +++ b/libs/core/build.gradle @@ -3,7 +3,6 @@ version = core_version description = 'Client for the OSM API 0.6 - Core functionality, getting server capabilities, getting user permissions' dependencies { - compile 'oauth.signpost:signpost-core:2.1.1' compile 'xmlpull:xmlpull:1.1.3.1' runtime 'net.sf.kxml:kxml2:2.3.0' } diff --git a/libs/core/src/main/java/de/westnordost/osmapi/OsmConnection.java b/libs/core/src/main/java/de/westnordost/osmapi/OsmConnection.java index 3147d29..2574ca2 100644 --- a/libs/core/src/main/java/de/westnordost/osmapi/OsmConnection.java +++ b/libs/core/src/main/java/de/westnordost/osmapi/OsmConnection.java @@ -10,12 +10,7 @@ import java.net.URL; import java.util.Locale; -import oauth.signpost.OAuthConsumer; -import oauth.signpost.basic.DefaultOAuthConsumer; -import oauth.signpost.exception.OAuthException; -import oauth.signpost.exception.OAuthExpectationFailedException; import de.westnordost.osmapi.common.errors.OsmApiReadResponseException; -import de.westnordost.osmapi.common.errors.OsmAuthorizationException; import de.westnordost.osmapi.common.errors.OsmConnectionException; import de.westnordost.osmapi.common.errors.RedirectedException; @@ -54,36 +49,36 @@ public class OsmConnection private int timeout; private String apiUrl; private String userAgent; - - private OAuthConsumer oauth; + private String oauthAccessToken; + private final Object oauthLock = new Object(); /** * Create a new OsmConnection with the given preferences * @param apiUrl the URL to the API * @param userAgent the user agent this application should identify as - * @param oauth oauth consumer to use to authenticate this app. If this is null, any attempt to - * make an API call that requires authorization will throw an OsmAuthorizationException + * @param oauthAccessToken OAuth 2.0 access token to use to authenticate this app. If this is null, any attempt + * to make an API call that requires authorization will throw an OsmAuthorizationException * @param timeout for the server connection. Defaults to 45 seconds. */ - public OsmConnection(String apiUrl, String userAgent, OAuthConsumer oauth, Integer timeout) + public OsmConnection(String apiUrl, String userAgent, String oauthAccessToken, Integer timeout) { this.apiUrl = apiUrl; this.userAgent = userAgent; - this.oauth = oauth; + this.oauthAccessToken = oauthAccessToken; this.timeout = timeout != null ? timeout : DEFAULT_TIMEOUT; } /** - * @see #OsmConnection(String, String, OAuthConsumer, Integer) + * @see #OsmConnection(String, String, String, Integer) */ - public OsmConnection(String apiUrl, String userAgent, OAuthConsumer oauth) + public OsmConnection(String apiUrl, String userAgent, String oauth) { this(apiUrl, userAgent, oauth, null); } /** - * @see #OsmConnection(String, String, OAuthConsumer, Integer) + * @see #OsmConnection(String, String, String, Integer) */ public OsmConnection(String apiUrl, String userAgent) { @@ -95,11 +90,11 @@ public synchronized void setTimeout(int timeout) this.timeout = timeout; } - public void setOAuth(OAuthConsumer oauth) + public void setOAuthAccessToken(String oauthAccessToken) { synchronized(oauthLock) { - this.oauth = oauth; + this.oauthAccessToken = oauthAccessToken; } } @@ -123,11 +118,11 @@ public synchronized String getApiUrl() return apiUrl; } - public OAuthConsumer getOAuth() + public String getOAuthAccessToken() { synchronized(oauthLock) { - return oauth; + return oauthAccessToken; } } @@ -201,21 +196,14 @@ public T makeRequest(String call, String method, boolean authenticate, { throw new OsmConnectionException(e); } - catch(OAuthException e) - { - // because it was the user's fault that he did not supply an oauth consumer and the - // error is kinda related with the call he made - throw new OsmAuthorizationException(e); - } finally { if(connection != null) connection.disconnect(); } } - private HttpURLConnection sendRequest( - String call, String method, boolean authenticate, ApiRequestWriter writer) - throws IOException, OAuthException + private HttpURLConnection sendRequest(String call, String method, boolean authenticate, ApiRequestWriter writer) + throws IOException { HttpURLConnection connection = openConnection(call); if(method != null) @@ -231,7 +219,7 @@ private HttpURLConnection sendRequest( if(authenticate) { - createOAuthConsumer().sign(connection); + connection.setRequestProperty("Authorization", "Bearer " + oauthAccessToken); } if(writer != null) @@ -290,26 +278,6 @@ private synchronized HttpURLConnection openConnection(String call) throws IOExce return connection; } - private OAuthConsumer createOAuthConsumer() throws OAuthExpectationFailedException - { - synchronized(oauthLock) - { - if(oauth == null) - { - throw new OAuthExpectationFailedException( - "This class has been initialized without a OAuthConsumer. Only API calls " + - "that do not require authentication can be made."); - } - - // "clone" the original consumer every time because the consumer is documented to be not - // thread safe and maybe multiple threads are making calls to this class - OAuthConsumer consumer = new DefaultOAuthConsumer( - oauth.getConsumerKey(), oauth.getConsumerSecret()); - consumer.setTokenWithSecret(oauth.getToken(), oauth.getTokenSecret()); - return consumer; - } - } - private T handleResponse(HttpURLConnection connection, ApiResponseReader reader) throws IOException { diff --git a/libs/core/src/test/java/de/westnordost/osmapi/OsmConnectionTest.java b/libs/core/src/test/java/de/westnordost/osmapi/OsmConnectionTest.java index 96fa5a6..8e106fe 100644 --- a/libs/core/src/test/java/de/westnordost/osmapi/OsmConnectionTest.java +++ b/libs/core/src/test/java/de/westnordost/osmapi/OsmConnectionTest.java @@ -18,7 +18,7 @@ public class OsmConnectionTest try { OsmConnection osm = ConnectionTestFactory.createConnection(null); - osm.makeAuthenticatedRequest("doesntMatter", "GET"); + osm.makeAuthenticatedRequest("changeset/create", "PUT", null, null); fail(); } catch(OsmAuthorizationException ignore) {} diff --git a/libs/map/src/main/java/de/westnordost/osmapi/map/MapDataApi.java b/libs/map/src/main/java/de/westnordost/osmapi/map/MapDataApi.java index ab00ba2..eaae2ca 100644 --- a/libs/map/src/main/java/de/westnordost/osmapi/map/MapDataApi.java +++ b/libs/map/src/main/java/de/westnordost/osmapi/map/MapDataApi.java @@ -213,7 +213,7 @@ public void getMap(BoundingBox bounds, MapDataHandler handler) } String request = "map?bbox=" + bounds.getAsLeftBottomRightTopString(); - boolean authenticate = osm.getOAuth() != null; + boolean authenticate = osm.getOAuthAccessToken() != null; try { @@ -235,7 +235,7 @@ public void getMap(BoundingBox bounds, MapDataHandler handler) * @throws OsmNotFoundException if the way with the given id does not exist */ public void getWayComplete(long id, MapDataHandler handler) { - boolean authenticate = osm.getOAuth() != null; + boolean authenticate = osm.getOAuthAccessToken() != null; osm.makeRequest(WAY + "/" + id + "/" + FULL, authenticate, new MapDataParser(handler, factory)); } @@ -249,7 +249,7 @@ public void getWayComplete(long id, MapDataHandler handler) * @throws OsmNotFoundException if the relation with the given id does not exist*/ public void getRelationComplete(long id, MapDataHandler handler) { - boolean authenticate = osm.getOAuth() != null; + boolean authenticate = osm.getOAuthAccessToken() != null; osm.makeRequest(RELATION + "/" + id + "/" + FULL, authenticate, new MapDataParser(handler, factory)); } @@ -285,7 +285,7 @@ private T getOneElement(String call, Class tClass) SingleOsmElementHandler handler = new SingleOsmElementHandler<>(tClass); try { - boolean authenticate = osm.getOAuth() != null; + boolean authenticate = osm.getOAuthAccessToken() != null; osm.makeRequest(call, authenticate, new MapDataParser(handler, factory)); } catch(OsmNotFoundException e) @@ -382,7 +382,7 @@ private static String toCommaList(Iterable vals) private List getSomeElements(String call, Class tClass) { ListOsmElementHandler handler = new ListOsmElementHandler<>(tClass); - boolean authenticate = osm.getOAuth() != null; + boolean authenticate = osm.getOAuthAccessToken() != null; osm.makeRequest(call, authenticate, new MapDataParser(handler, factory)); return handler.get(); } diff --git a/libs/map/src/main/java/de/westnordost/osmapi/map/MapDataHistoryApi.java b/libs/map/src/main/java/de/westnordost/osmapi/map/MapDataHistoryApi.java index 606634c..b9c45f7 100644 --- a/libs/map/src/main/java/de/westnordost/osmapi/map/MapDataHistoryApi.java +++ b/libs/map/src/main/java/de/westnordost/osmapi/map/MapDataHistoryApi.java @@ -49,7 +49,7 @@ public MapDataHistoryApi(OsmConnection osm) public void getNodeHistory(long id, Handler handler) { MapDataHandler mapDataHandler = new WrapperOsmElementHandler<>(Node.class, handler); - boolean authenticate = osm.getOAuth() != null; + boolean authenticate = osm.getOAuthAccessToken() != null; osm.makeRequest(NODE + "/" + id + "/" + HISTORY, authenticate, new MapDataParser(mapDataHandler, factory)); } @@ -65,7 +65,7 @@ public void getNodeHistory(long id, Handler handler) public void getWayHistory(long id, Handler handler) { MapDataHandler mapDataHandler = new WrapperOsmElementHandler<>(Way.class, handler); - boolean authenticate = osm.getOAuth() != null; + boolean authenticate = osm.getOAuthAccessToken() != null; osm.makeRequest(WAY + "/" + id + "/" + HISTORY, authenticate, new MapDataParser(mapDataHandler, factory)); } @@ -81,7 +81,7 @@ public void getWayHistory(long id, Handler handler) public void getRelationHistory(long id, Handler handler) { MapDataHandler mapDataHandler = new WrapperOsmElementHandler<>(Relation.class, handler); - boolean authenticate = osm.getOAuth() != null; + boolean authenticate = osm.getOAuthAccessToken() != null; osm.makeRequest(RELATION + "/" + id + "/" + HISTORY, authenticate, new MapDataParser(mapDataHandler, factory)); } @@ -127,7 +127,7 @@ private T getElementVersion(String call, Class tClass) SingleOsmElementHandler handler = new SingleOsmElementHandler<>(tClass); try { - boolean authenticate = osm.getOAuth() != null; + boolean authenticate = osm.getOAuthAccessToken() != null; osm.makeRequest(call, authenticate, new MapDataParser(handler, factory)); } catch(OsmApiException e) diff --git a/libs/notes/src/main/java/de/westnordost/osmapi/notes/NotesApi.java b/libs/notes/src/main/java/de/westnordost/osmapi/notes/NotesApi.java index 7bb75e2..dc72b7b 100644 --- a/libs/notes/src/main/java/de/westnordost/osmapi/notes/NotesApi.java +++ b/libs/notes/src/main/java/de/westnordost/osmapi/notes/NotesApi.java @@ -58,7 +58,7 @@ public Note create(LatLon pos, String text) "&text=" + urlEncode(text); String call = NOTES + "?" + data; - boolean authenticate = osm.getOAuth() != null; + boolean authenticate = osm.getOAuthAccessToken() != null; SingleElementHandler noteHandler = new SingleElementHandler<>(); osm.makeRequest(call, "POST", authenticate, null, new NotesParser(noteHandler)); return noteHandler.get(); @@ -157,7 +157,7 @@ public Note get(long id) SingleElementHandler noteHandler = new SingleElementHandler<>(); try { - boolean authenticate = osm.getOAuth() != null; + boolean authenticate = osm.getOAuthAccessToken() != null; osm.makeRequest(NOTES + "/" + id, authenticate, new NotesParser(noteHandler)); } catch (OsmNotFoundException e) @@ -216,7 +216,7 @@ public void getAll(BoundingBox bounds, String search, Handler handler, int try { - boolean authenticate = osm.getOAuth() != null; + boolean authenticate = osm.getOAuthAccessToken() != null; osm.makeRequest(call, authenticate, new NotesParser(handler)); } catch(OsmBadUserInputException e) @@ -238,7 +238,7 @@ public void getAll(BoundingBox bounds, String search, Handler handler, int public void find(Handler handler, QueryNotesFilters filters) { String query = filters != null ? "?" + filters.toParamString() : ""; - boolean authenticate = osm.getOAuth() != null; + boolean authenticate = osm.getOAuthAccessToken() != null; try { osm.makeRequest(NOTES + "/search" + query, authenticate, new NotesParser(handler)); } diff --git a/libs/notes/src/test/java/de/westnordost/osmapi/notes/NotesApiTest.java b/libs/notes/src/test/java/de/westnordost/osmapi/notes/NotesApiTest.java index be4945c..f22643d 100644 --- a/libs/notes/src/test/java/de/westnordost/osmapi/notes/NotesApiTest.java +++ b/libs/notes/src/test/java/de/westnordost/osmapi/notes/NotesApiTest.java @@ -7,7 +7,6 @@ import java.time.Instant; import java.util.List; -import oauth.signpost.exception.OAuthExpectationFailedException; import de.westnordost.osmapi.ConnectionTestFactory; import de.westnordost.osmapi.common.Handler; import de.westnordost.osmapi.common.errors.OsmAuthorizationException; @@ -153,10 +152,7 @@ public class NotesApiTest anonymousApi.reopen(note.id, TEXT); fail(); } - catch(OsmAuthorizationException e) - { - assertTrue(e.getCause() instanceof OAuthExpectationFailedException); - } + catch(OsmAuthorizationException ignore) { } } @Test public void closeNoteAsAnonymousFails() @@ -166,10 +162,7 @@ public class NotesApiTest anonymousApi.close(note.id, TEXT); fail(); } - catch(OsmAuthorizationException e) - { - assertTrue(e.getCause() instanceof OAuthExpectationFailedException); - } + catch(OsmAuthorizationException ignore) { } } @Test public void createNoteWithoutTextFails() diff --git a/libs/traces/src/test/java/de/westnordost/osmapi/traces/GpsTracesApiTest.java b/libs/traces/src/test/java/de/westnordost/osmapi/traces/GpsTracesApiTest.java index 14b7777..e896329 100644 --- a/libs/traces/src/test/java/de/westnordost/osmapi/traces/GpsTracesApiTest.java +++ b/libs/traces/src/test/java/de/westnordost/osmapi/traces/GpsTracesApiTest.java @@ -26,7 +26,7 @@ public class GpsTracesApiTest private static final int NONEXISTING_TRACE = 0; - private static final int PRIVATE_TRACE_OF_OTHER_USER = 928; + private static final int PRIVATE_TRACE_OF_OTHER_USER = 23; private static final int PUBLIC_TRACE = 927; @@ -196,7 +196,7 @@ public class GpsTracesApiTest assertEquals("test case desc", trace.description); assertTrue(trace.tags.contains("a tag")); assertTrue(trace.tags.contains("another")); - assertEquals("osmagent-test-allow-everything", trace.userName); + assertEquals("westnordost", trace.userName); assertTrue(Math.abs(Instant.now().toEpochMilli() - trace.createdAt.toEpochMilli()) < TEN_MINUTES); privilegedApi.update(id, Visibility.TRACKABLE, "desc", null); diff --git a/libs/user/src/main/java/de/westnordost/osmapi/user/UserApi.java b/libs/user/src/main/java/de/westnordost/osmapi/user/UserApi.java index a738cdf..4192c01 100644 --- a/libs/user/src/main/java/de/westnordost/osmapi/user/UserApi.java +++ b/libs/user/src/main/java/de/westnordost/osmapi/user/UserApi.java @@ -40,7 +40,7 @@ public UserInfo get(long userId) try { SingleElementHandler handler = new SingleElementHandler<>(); - boolean authenticate = osm.getOAuth() != null; + boolean authenticate = osm.getOAuthAccessToken() != null; osm.makeRequest("user/" + userId, authenticate, new UserInfoParser(handler)); return handler.get(); } @@ -54,7 +54,7 @@ public List getAll(Collection userIds) { if(userIds.isEmpty()) return Collections.emptyList(); ListHandler handler = new ListHandler<>(); - boolean authenticate = osm.getOAuth() != null; + boolean authenticate = osm.getOAuthAccessToken() != null; osm.makeRequest("users?users=" + toCommaList(userIds), authenticate, new UserInfoParser(handler)); return handler.get(); } diff --git a/testutils/src/main/java/de/westnordost/osmapi/ConnectionTestFactory.java b/testutils/src/main/java/de/westnordost/osmapi/ConnectionTestFactory.java index bb94521..518908a 100644 --- a/testutils/src/main/java/de/westnordost/osmapi/ConnectionTestFactory.java +++ b/testutils/src/main/java/de/westnordost/osmapi/ConnectionTestFactory.java @@ -1,12 +1,23 @@ package de.westnordost.osmapi; -import oauth.signpost.OAuthConsumer; -import oauth.signpost.basic.DefaultOAuthConsumer; - public class ConnectionTestFactory { - private static final String CONSUMER_KEY = "CuPCn3sRc8FDiepAoSkH4a9n7w2QuqVCykStfVPG"; - private static final String CONSUMER_SECRET = "D1nX6BF1NMAZtIq8ouGJJ7zGtSaTRDTz8QfZl5mo"; + private static final String CLIENT_ID = "_RN0elf1uUxGvpdRQram0s_hfPpOkimHVXhlHN5Cx5I"; + private static final String CLIENT_SECRET = "UN8rnr9zSni1504KUeTi4iFwUnErW3YyhMEIaEg0Q-E"; + + // to request new token: + // 1. GET https://master.apis.dev.openstreetmap.org/oauth2/authorize?response_type=code&client_id=_RN0elf1uUxGvpdRQram0s_hfPpOkimHVXhlHN5Cx5I&redirect_uri=https://127.0.0.1/oauth&scope=read_prefs%20write_prefs%20write_diary%20write_api%20read_gpx%20write_gpx%20write_notes + // 2. get the CODE from the url + // 3. POST 'https://master.apis.dev.openstreetmap.org/oauth2/token?grant_type=authorization_code&code=&client_id=_RN0elf1uUxGvpdRQram0s_hfPpOkimHVXhlHN5Cx5I&redirect_uri=https://127.0.0.1/oauth' + private static final String ALLOW_EVERYTHING_TOKEN = "qzaxWiG2tprF1IfEcwf4-mn7Al4f2lsM3CNrvGEaIL0"; + + // to request new token: + // 1. GET https://master.apis.dev.openstreetmap.org/oauth2/authorize?response_type=code&client_id=_RN0elf1uUxGvpdRQram0s_hfPpOkimHVXhlHN5Cx5I&redirect_uri=https://127.0.0.1/oauth&scope=read_prefs + // 2. get the CODE from the url + // 3. POST 'https://master.apis.dev.openstreetmap.org/oauth2/token?grant_type=authorization_code&code=&client_id=_RN0elf1uUxGvpdRQram0s_hfPpOkimHVXhlHN5Cx5I&redirect_uri=https://127.0.0.1/oauth' + private static final String ALLOW_NOTHING_TOKEN = "fp2SjHKQ55rSdI2x4FN_s0wNUh67dgNbf9x3WdjCa5Y"; + + private static final String UNKNOWN_TOKEN = "unknown"; private static final String TEST_API_URL = "https://master.apis.dev.openstreetmap.org/api/0.6/"; @@ -26,39 +37,14 @@ public static OsmConnection createLiveConnection() public static OsmConnection createConnection(User user) { - OAuthConsumer consumer = null; - + String accessToken = ""; if(user == User.ALLOW_EVERYTHING) - consumer = createConsumerThatAllowsEverything(); + accessToken = ALLOW_EVERYTHING_TOKEN; else if(user == User.ALLOW_NOTHING) - consumer = createConsumerThatProhibitsEverything(); + accessToken = ALLOW_NOTHING_TOKEN; else if(user == User.UNKNOWN) - consumer = createUnknownUser(); - - return new OsmConnection(TEST_API_URL, USER_AGENT, consumer); - } + accessToken = UNKNOWN_TOKEN; - - private static OAuthConsumer createConsumerThatProhibitsEverything() - { - OAuthConsumer result = new DefaultOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET); - result.setTokenWithSecret( - "RCamNf4TT7uNeFjmigvOUWhajp5ERFZmcN1qvi7a", - "72dzmAvuNBEOVKkif3JSYdzMlAq2dw5OnIG75dtX"); - return result; - } - - private static OAuthConsumer createConsumerThatAllowsEverything() - { - OAuthConsumer result = new DefaultOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET); - result.setTokenWithSecret( - "2C4LiOQBOn96kXHyal7uzMJiqpCsiyDBvb8pomyX", - "1bFMIQpgmu5yjywt3kknopQpcRmwJ6snDDGF7kdr"); - return result; - } - - private static OAuthConsumer createUnknownUser() - { - return new DefaultOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET); + return new OsmConnection(TEST_API_URL, USER_AGENT, accessToken); } } From 1aab9b562adff84940d04683060a4d11b35758ec Mon Sep 17 00:00:00 2001 From: Tobias Zwick Date: Wed, 22 Nov 2023 03:11:15 +0100 Subject: [PATCH 2/4] reference new versions in the readme, too --- README.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index a6e233a..5d4833a 100644 --- a/README.md +++ b/README.md @@ -16,18 +16,18 @@ Depending on which part of the API you use, you can only include what you need: - - - - - - - - - + + + + + + + + +
ClassDependencyDescription
CapabilitiesApi
de.westnordost:osmapi-core:2.0
Getting server capabilities
PermissionsApi
de.westnordost:osmapi-core:2.0
Getting user permissions
MapDataApi
de.westnordost:osmapi-map:2.3
Getting map data, querying single elements and their relations toward each other and uploading changes in changesets
MapDataHistoryApi
de.westnordost:osmapi-map:2.3
Getting the history and specific versions of elements
NotesApi
de.westnordost:osmapi-notes:2.0
Getting finding, creating, commenting on and solving notes
GpsTracesApi
de.westnordost:osmapi-traces:2.0
Getting, uploading, updating and deleting GPS traces and trackpoints
ChangesetsApi
de.westnordost:osmapi-changesets:2.3
Finding changesets, changeset discussion, subscription and data
UserApi
de.westnordost:osmapi-user:2.0
Getting user information
UserPreferencesApi
de.westnordost:osmapi-user:2.0
Managing user preferences
CapabilitiesApi
de.westnordost:osmapi-core:3.0
Getting server capabilities
PermissionsApi
de.westnordost:osmapi-core:3.0
Getting user permissions
MapDataApi
de.westnordost:osmapi-map:3.0
Getting map data, querying single elements and their relations toward each other and uploading changes in changesets
MapDataHistoryApi
de.westnordost:osmapi-map:3.0
Getting the history and specific versions of elements
NotesApi
de.westnordost:osmapi-notes:3.0
Getting finding, creating, commenting on and solving notes
GpsTracesApi
de.westnordost:osmapi-traces:3.0
Getting, uploading, updating and deleting GPS traces and trackpoints
ChangesetsApi
de.westnordost:osmapi-changesets:3.0
Finding changesets, changeset discussion, subscription and data
UserApi
de.westnordost:osmapi-user:3.0
Getting user information
UserPreferencesApi
de.westnordost:osmapi-user:3.0
Managing user preferences
-To include everything, add [`de.westnordost:osmapi:4.3`](https://mvnrepository.com/artifact/de.westnordost/osmapi/4.0) as a Maven dependency or download the jar from there. +To include everything, add [`de.westnordost:osmapi:5.0`](https://mvnrepository.com/artifact/de.westnordost/osmapi/4.0) as a Maven dependency or download the jar from there. ### Android @@ -35,7 +35,7 @@ On Android, you need to exclude kxml2 from the dependencies since it is already ```gradle dependencies { - implementation 'de.westnordost:osmapi:4.3' + implementation 'de.westnordost:osmapi:5.0' } configurations { From d6bbe3f86bb16167d25b993fb767aac4b8ac04aa Mon Sep 17 00:00:00 2001 From: Tobias Zwick Date: Wed, 22 Nov 2023 03:14:43 +0100 Subject: [PATCH 3/4] comments --- .../java/de/westnordost/osmapi/ConnectionTestFactory.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/testutils/src/main/java/de/westnordost/osmapi/ConnectionTestFactory.java b/testutils/src/main/java/de/westnordost/osmapi/ConnectionTestFactory.java index 518908a..e7d563d 100644 --- a/testutils/src/main/java/de/westnordost/osmapi/ConnectionTestFactory.java +++ b/testutils/src/main/java/de/westnordost/osmapi/ConnectionTestFactory.java @@ -3,16 +3,17 @@ public class ConnectionTestFactory { private static final String CLIENT_ID = "_RN0elf1uUxGvpdRQram0s_hfPpOkimHVXhlHN5Cx5I"; + // actually not used anywhere. Only necessary to request a token if app was registered as "confidential" private static final String CLIENT_SECRET = "UN8rnr9zSni1504KUeTi4iFwUnErW3YyhMEIaEg0Q-E"; // to request new token: - // 1. GET https://master.apis.dev.openstreetmap.org/oauth2/authorize?response_type=code&client_id=_RN0elf1uUxGvpdRQram0s_hfPpOkimHVXhlHN5Cx5I&redirect_uri=https://127.0.0.1/oauth&scope=read_prefs%20write_prefs%20write_diary%20write_api%20read_gpx%20write_gpx%20write_notes + // 1. open in browser https://master.apis.dev.openstreetmap.org/oauth2/authorize?response_type=code&client_id=_RN0elf1uUxGvpdRQram0s_hfPpOkimHVXhlHN5Cx5I&redirect_uri=https://127.0.0.1/oauth&scope=read_prefs%20write_prefs%20write_diary%20write_api%20read_gpx%20write_gpx%20write_notes // 2. get the CODE from the url // 3. POST 'https://master.apis.dev.openstreetmap.org/oauth2/token?grant_type=authorization_code&code=&client_id=_RN0elf1uUxGvpdRQram0s_hfPpOkimHVXhlHN5Cx5I&redirect_uri=https://127.0.0.1/oauth' private static final String ALLOW_EVERYTHING_TOKEN = "qzaxWiG2tprF1IfEcwf4-mn7Al4f2lsM3CNrvGEaIL0"; // to request new token: - // 1. GET https://master.apis.dev.openstreetmap.org/oauth2/authorize?response_type=code&client_id=_RN0elf1uUxGvpdRQram0s_hfPpOkimHVXhlHN5Cx5I&redirect_uri=https://127.0.0.1/oauth&scope=read_prefs + // 1. open in browser https://master.apis.dev.openstreetmap.org/oauth2/authorize?response_type=code&client_id=_RN0elf1uUxGvpdRQram0s_hfPpOkimHVXhlHN5Cx5I&redirect_uri=https://127.0.0.1/oauth&scope=read_prefs // 2. get the CODE from the url // 3. POST 'https://master.apis.dev.openstreetmap.org/oauth2/token?grant_type=authorization_code&code=&client_id=_RN0elf1uUxGvpdRQram0s_hfPpOkimHVXhlHN5Cx5I&redirect_uri=https://127.0.0.1/oauth' private static final String ALLOW_NOTHING_TOKEN = "fp2SjHKQ55rSdI2x4FN_s0wNUh67dgNbf9x3WdjCa5Y"; From 2361a801d5b42154c0913c4429bc3fc9f145c748 Mon Sep 17 00:00:00 2001 From: Tobias Zwick Date: Fri, 24 Nov 2023 22:48:17 +0100 Subject: [PATCH 4/4] update readme --- README.md | 31 +++++++++---------------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 5d4833a..eb5fdc7 100644 --- a/README.md +++ b/README.md @@ -31,32 +31,19 @@ To include everything, add [`de.westnordost:osmapi:5.0`](https://mvnrepository.c ### Android -On Android, you need to exclude kxml2 from the dependencies since it is already built-in, like so: - -```gradle -dependencies { - implementation 'de.westnordost:osmapi:5.0' -} +On Android, you need to exclude kxml2 from the dependencies in your `gradle.kts` since it is already built-in, like so: +```kotlin configurations { - // already included in Android - all*.exclude group: 'net.sf.kxml', module: 'kxml2' - - // @NonNull etc annotations are also already included in Android - cleanedAnnotations - compile.exclude group: 'org.jetbrains', module:'annotations' - compile.exclude group: 'com.intellij', module:'annotations' - compile.exclude group: 'org.intellij', module:'annotations' - compile.exclude group: 'xmlpull', module:'xmlpull' + all { + // it's already included in Android + exclude(group = "net.sf.kxml", module = "kxml2") + exclude(group = "xmlpull", module = "xmlpull") + } } ``` -Also, starting with v4.0 (or v2.0 of the modularized version respectively), this library uses the classes from the Java 8 time API, like [`Instant`](https://developer.android.com/reference/java/time/Instant) etc. instead of `Date` which [leads to about 50% faster parsing times](https://github.com/streetcomplete/StreetComplete/discussions/2740) when receiving a result. - -If your app supports Android API levels below 26, you have two options: - -1. Either stick to using version 3.x (or v1.x of the modularized version respectively) of this library... -2. ...or enable [Java 8+ API desugaring support](https://developer.android.com/studio/write/java8-support#library-desugaring) for your app +This library uses classes from the Java 8 time API, like [`Instant`](https://developer.android.com/reference/java/time/Instant) etc., so if your app supports Android API levels below 26, you need to enable [Java 8+ API desugaring support](https://developer.android.com/studio/write/java8-support#library-desugaring). ## Basic Usage @@ -70,7 +57,7 @@ If you plan to make calls that can only be made by a logged in user, such as upl ); ``` -You can call osm.makeRequest(...) yourself to talk with the RESTful Api and write your own ApiRequestWriter and ApiResponseReader to write/read the request. +You can call `osm.makeRequest(...)` yourself to talk with the RESTful Api and write your own ApiRequestWriter and ApiResponseReader to write/read the request. It is more convenient however to use the appropriate class to do that for you and return the data you are interested in. See the table above for which classes are available. For example...