Skip to content

Commit 79a3c1b

Browse files
authored
[mercedesme] New authorization process (openhab#18342)
* change auth process Signed-off-by: Bernd Weymann <[email protected]>
1 parent a0bae2f commit 79a3c1b

File tree

22 files changed

+247
-681
lines changed

22 files changed

+247
-681
lines changed

bundles/org.openhab.binding.mercedesme/README.md

+20-55
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ This binding provides access to your Mercedes Benz vehicle like _Mercedes Me_ Sm
77
First time users shall follow the following sequence
88

99
1. Setup and configure [Bridge](#bridge-configuration)
10-
2. Follow the [Bridge Authorization](#bridge-authorization) process
11-
3. [Discovery](#discovery) shall find now vehicles associated to your account
12-
4. Add your vehicle from discovery and [configure](#thing-configuration) it with correct VIN
13-
5. Connect your desired items in UI or [text-configuration](#full-example)
14-
6. Optional: you can [Discover your Vehicle](#discover-your-vehicle) more deeply
15-
7. In case of problems check [Troubleshooting](#troubleshooting) section
10+
2. [Discovery](#discovery) shall find now vehicles associated to your account
11+
3. Add your vehicle from discovery and [configure](#thing-configuration) it with correct VIN
12+
4. Connect your desired items in UI or [text-configuration](#full-example)
13+
5. Optional: you can [Discover your Vehicle](#discover-your-vehicle) more deeply
14+
6. In case of problems check [Troubleshooting](#troubleshooting) section
1615

1716
## Supported Things
1817

@@ -35,14 +34,20 @@ There's no manual discovery!
3534

3635
Bridge needs configuration in order to connect properly to your Mercedes Me account.
3736

38-
| Name | Type | Description | Default | Required | Advanced |
39-
|-----------------|---------|-----------------------------------------|-------------|----------|----------|
40-
| email | text | Mercedes Me registered email Address | N/A | yes | no |
41-
| pin | text | Mercedes Me Smartphone App PIN | N/A | no | no |
42-
| region | text | Your region | EU | yes | no |
43-
| refreshInterval | integer | API refresh interval | 15 | yes | no |
44-
| callbackIP | text | IP Address of openHAB Device | N/A | yes | yes |
45-
| callbackPort | integer | Port Number of openHAB Device | N/A | yes | yes |
37+
| Name | Type | Description | Default | Required |
38+
|-------------------|---------|---------------------------------------------|---------------------------|----------|
39+
| email | text | Mercedes Me registered email Address | N/A | yes |
40+
| refreshToken | text | Refresh Token from MB Token Requester app | takeover previous token | yes |
41+
| pin | text | Mercedes Me Smartphone App PIN | N/A | no |
42+
| region | text | Your region | EU | yes |
43+
| refreshInterval | integer | API refresh interval | 15 | yes |
44+
45+
`refreshToken` is needed to get access to your Mercedes Me account.
46+
Users already running this binding can stay on default value `takeover previous token`.
47+
New users need to generate `refreshToken` with [MB Token Requester app]( https://github.com/ReneNulschDE/mbapi2020/wiki/How%E2%80%90to:-create-the-access-and-refresh-token ).
48+
It simulates the Mercedes Me application *only for authorization process* on your computer, **not your openHAB system!**
49+
The generated *refresh token* has to be pasted into the bridge configuration.
50+
The generated *token* can be ignored!
4651

4752
Set `region` to your location
4853

@@ -63,46 +68,6 @@ Commands protected by PIN
6368
- Open / Ventilate Windows
6469
- Open / Lift Sunroof
6570

66-
IP `callbackIP` and port `callbackPort` will be auto-detected.
67-
If you're running on server with more than one network interface please select manually.
68-
69-
### Bridge Authorization
70-
71-
Authorization is needed to activate the Bridge which is connected to your Mercedes Me Account.
72-
The Bridge will indicate in the status headline if authorization is needed including the URL which needs to be opened in your browser.
73-
74-
Three steps are needed
75-
76-
1. Open the mentioned URL like 192.168.x.x:8090/mb-auth
77-
Opening this URL will request a PIN which will be send to your configured email.
78-
Check your Mail Account if you received the PIN.
79-
Click on _Continue_ to proceed with Step 2.
80-
81-
2. Enter your PIN in the shown field.
82-
Leave GUID as identifier as it is.
83-
Click on _Submit_ button.
84-
85-
3. Confirmation shall be shown that authorization was successful.
86-
87-
In case of non successful authorization check your log for errors.
88-
Below screenshots are illustrating the authorization flow.
89-
90-
### After Bridge Setup
91-
92-
<img src="./doc/OH-Step0.png" width="500" height="240"/>
93-
94-
### Authorization Step 1
95-
96-
<img src="./doc/OH-Step1.png" width="500" height="200"/>
97-
98-
### Authorization Step 2
99-
100-
<img src="./doc/OH-Step2.png" width="500" height="200"/>
101-
102-
### Authorization Step 3
103-
104-
<img src="./doc/OH-Step3.png" width="400" height="130"/>
105-
10671
## Thing Configuration
10772

10873
| Name | Type | Description | Default | Required | Advanced |
@@ -814,7 +779,7 @@ Keep these 3 channels disconnected during normal operation.
814779
### Things file
815780

816781
```java
817-
Bridge mercedesme:account:4711 "Mercedes Me John Doe" [ email="YOUR_MAIL_ADDRESS", region="EU", pin=9876, refreshInterval=15] {
782+
Bridge mercedesme:account:4711 "Mercedes Me John Doe" [ email="YOUR_MAIL_ADDRESS", region="EU", pin=9876, refreshToken="abc", refreshInterval=15] {
818783
Thing bev eqa "Mercedes EQA" [ vin="VEHICLE_VIN", batteryCapacity=66.5]
819784
}
820785
```
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

bundles/org.openhab.binding.mercedesme/src/main/java/org/openhab/binding/mercedesme/internal/Constants.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,6 @@ public class Constants {
268268
public static final String OH_CHANNEL_CONSTANT = "constant";
269269
public static final String OH_CHANNEL_BONUS_RANGE = "bonus";
270270

271-
public static final String CALLBACK_ENDPOINT = "/mb-auth";
272271
// https://developer.mercedes-benz.com/content-page/api_migration_guide
273272
public static final String IMAGE_BASE_URL = "https://api.mercedes-benz.com/vehicle_images/v2";
274273
public static final String IMAGE_EXTERIOR_RESOURCE_URL = IMAGE_BASE_URL + "/vehicles/%s";
@@ -278,9 +277,7 @@ public class Constants {
278277
public static final String STATUS_EMAIL_MISSING = ".status.email-missing";
279278
public static final String STATUS_REGION_MISSING = ".status.region-missing";
280279
public static final String STATUS_REFRESH_INVALID = ".status.refresh-invalid";
281-
public static final String STATUS_IP_MISSING = ".status.ip-missing";
282-
public static final String STATUS_PORT_MISSING = ".status.port-missing";
283-
public static final String STATUS_SERVER_RESTART = ".status.server-restart";
280+
public static final String STATUS_REFRESH_TOKEN_MISSING = ".status.refresh-token-missing";
284281
public static final String STATUS_BRIDGE_MISSING = ".status.bridge-missing";
285282

286283
public static final String SPACE = " ";
@@ -346,7 +343,6 @@ public class Constants {
346343
public static final String MAX_SOC_KEY = "maxsoc";
347344
public static final String AUTO_UNLOCK_KEY = "autolock";
348345

349-
public static final String JUNIT_SERVER_ADDR = "http://999.999.999.999:99999/mb-auth";
350346
public static final String JUNIT_TOKEN = "junitTestToken";
351347
public static final String JUNIT_REFRESH_TOKEN = "junitRefreshToken";
352348
}

bundles/org.openhab.binding.mercedesme/src/main/java/org/openhab/binding/mercedesme/internal/MercedesMeHandlerFactory.java

+2-7
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.openhab.core.i18n.UnitProvider;
3232
import org.openhab.core.io.net.http.HttpClientFactory;
3333
import org.openhab.core.items.MetadataRegistry;
34-
import org.openhab.core.net.NetworkAddressService;
3534
import org.openhab.core.storage.StorageService;
3635
import org.openhab.core.thing.Bridge;
3736
import org.openhab.core.thing.Thing;
@@ -65,7 +64,6 @@ public class MercedesMeHandlerFactory extends BaseThingHandlerFactory {
6564
private final MercedesMeDiscoveryService discoveryService;
6665
private final MercedesMeCommandOptionProvider mmcop;
6766
private final MercedesMeStateOptionProvider mmsop;
68-
private final NetworkAddressService networkService;
6967
private @Nullable ServiceRegistration<?> discoveryServiceReg;
7068
private @Nullable MercedesMeMetadataAdjuster mdAdjuster;
7169

@@ -76,10 +74,8 @@ public MercedesMeHandlerFactory(@Reference HttpClientFactory hcf, @Reference Sto
7674
final @Reference LocaleProvider lp, final @Reference LocationProvider locationP,
7775
final @Reference TimeZoneProvider tzp, final @Reference MercedesMeCommandOptionProvider cop,
7876
final @Reference MercedesMeStateOptionProvider sop, final @Reference UnitProvider up,
79-
final @Reference MetadataRegistry mdr, final @Reference ItemChannelLinkRegistry iclr,
80-
final @Reference NetworkAddressService nas) {
77+
final @Reference MetadataRegistry mdr, final @Reference ItemChannelLinkRegistry iclr) {
8178
this.storageService = storageService;
82-
networkService = nas;
8379
localeProvider = lp;
8480
locationProvider = locationP;
8581
mmcop = cop;
@@ -112,8 +108,7 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) {
112108
discoveryServiceReg = bundleContext.registerService(DiscoveryService.class.getName(), discoveryService,
113109
null);
114110
}
115-
return new AccountHandler((Bridge) thing, discoveryService, httpClient, localeProvider, storageService,
116-
networkService);
111+
return new AccountHandler((Bridge) thing, discoveryService, httpClient, localeProvider, storageService);
117112
} else if (THING_TYPE_BEV.equals(thingTypeUID) || THING_TYPE_COMB.equals(thingTypeUID)
118113
|| THING_TYPE_HYBRID.equals(thingTypeUID)) {
119114
return new VehicleHandler(thing, locationProvider, mmcop, mmsop);

bundles/org.openhab.binding.mercedesme/src/main/java/org/openhab/binding/mercedesme/internal/config/AccountConfiguration.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ public class AccountConfiguration {
2626

2727
public String email = NOT_SET;
2828
public String region = NOT_SET;
29+
public String refreshToken = "takeover previous token";
2930
public String pin = NOT_SET;
3031
public int refreshInterval = 15;
31-
32-
public String callbackIP = NOT_SET;
33-
public int callbackPort = -1;
3432
}

bundles/org.openhab.binding.mercedesme/src/main/java/org/openhab/binding/mercedesme/internal/dto/TokenResponse.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ public class TokenResponse {
3333
@SerializedName("token_type")
3434
public String tokenType = Constants.NOT_SET;
3535
@SerializedName("expires_in")
36-
public int expiresIn;
37-
public String createdOn = Instant.now().toString();
36+
public int expiresIn = 0;
37+
public String createdOn = Instant.MIN.toString();
3838
}

0 commit comments

Comments
 (0)