From c2899f63f3ff2fc57ec83bb4d39601be438a7650 Mon Sep 17 00:00:00 2001 From: Jakob Date: Sun, 2 Jul 2023 23:57:17 +0200 Subject: [PATCH 1/2] add custom username & improve auth docs --- docs/Authorization.md | 37 ++++++++++++++++++++++++++++++++---- src/Immoscout/ApiRequest.php | 11 +++++++---- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/docs/Authorization.md b/docs/Authorization.md index a039fb5..fced767 100644 --- a/docs/Authorization.md +++ b/docs/Authorization.md @@ -1,9 +1,14 @@ # Authorization -**API reference**: https://api.immobilienscout24.de/api-docs/import-export/attachment/overview/ +**API reference**: https://api.immobilienscout24.de/api-docs/authentication/three-legged/

To access the api, you have to be registered on https://api.immobilienscout24.de. +
+You can then request a permanent token using Consumer Key & Secret to authenticate your account. An example of how to +get this token can be found here: https://api.immobilienscout24.de/api-docs/postman/postman-collections/. This package +only supports authentication via three-legged oAuth, as authentication via three-legged oAuth is required for user +endpoints. There are two ways bringing your auth keys into the code! @@ -18,7 +23,7 @@ IMSC_TOKEN_SECRET=YOUR_TOKEN_SECRET 2. Add it to your code -````php +```php // set auth data $authData = [ 'consumer_key' => 'YOUR_CONSUMER_KEY', @@ -31,7 +36,31 @@ $authData = [ $realEstate = new RealEstate($authData); $contact = new Contact($authData); ... -```` +``` You can find all information about the authentication system in -the [ImmoScout24 API documentation](https://api.immobilienscout24.de/api-docs/authentication/introduction/). \ No newline at end of file +the [ImmoScout24 API documentation](https://api.immobilienscout24.de/api-docs/authentication/introduction/). + +
+ +### Change the user + +You can also pass a custom user. Either via .env-file + +```dotenv +IMSC_USERNAME=yourusername +``` + +or by passing it to the class constructor + +```php +// with auth data +$realEstate = new RealEstate($authData, 'yourusername'); + +// without auth data +$contact = new RealEstate(null, 'yourusername'); +``` + +Per default, the username is set to "me" (the profile that created the auth data). +
+**Important**: Keep in mind, that additional users must be authenticated via oAuth! diff --git a/src/Immoscout/ApiRequest.php b/src/Immoscout/ApiRequest.php index dc4d5c7..96d2d36 100644 --- a/src/Immoscout/ApiRequest.php +++ b/src/Immoscout/ApiRequest.php @@ -20,11 +20,14 @@ class ApiRequest { private Client $client; - protected const API_URL = 'https://rest.immobilienscout24.de/restapi/api/offer/v1.0/user/me/%s'; + private string $user; - public function __construct(?array $auth = null) + protected const API_URL = 'https://rest.immobilienscout24.de/restapi/api/offer/v1.0/user/%s/%s'; + + public function __construct(?array $auth = null, ?string $user = null) { $this->client = $this->getPreparedClient($auth); + $this->user = $user ?? $_ENV['IMSC_USERNAME'] ?? 'me'; } /** @@ -63,7 +66,7 @@ private function getPreparedClient(?array $authData = null): Client protected function request(string $url, string $method = 'GET'): array { try { - $request = $this->client->request($method, sprintf(self::API_URL, $url)); + $request = $this->client->request($method, sprintf(self::API_URL, $this->user, $url)); } catch (GuzzleException $e) { throw new ApiException($e->getMessage(), $e->getCode()); } @@ -76,4 +79,4 @@ protected function request(string $url, string $method = 'GET'): array return $data; } -} \ No newline at end of file +} From c5dc55ec90b52baac158ca9e601412da0e6cd0a5 Mon Sep 17 00:00:00 2001 From: Jakob Date: Mon, 3 Jul 2023 00:04:37 +0200 Subject: [PATCH 2/2] better username info --- docs/Authorization.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/Authorization.md b/docs/Authorization.md index fced767..a7c2981 100644 --- a/docs/Authorization.md +++ b/docs/Authorization.md @@ -45,7 +45,9 @@ the [ImmoScout24 API documentation](https://api.immobilienscout24.de/api-docs/au ### Change the user -You can also pass a custom user. Either via .env-file +You can also pass a custom user. +The username must be he username which the user uses for logging in to www.immobilienscout24. +You can configure the user either via .env-file ```dotenv IMSC_USERNAME=yourusername