From 2764051b9fb7c56853663768bd4b33ba171a0e8a Mon Sep 17 00:00:00 2001 From: Wim Griffioen Date: Mon, 17 Oct 2022 11:28:11 +0200 Subject: [PATCH 1/2] Add support for private app tokens On November 30th, 2022 the usage of the HubSpot API key will be deprecated. See: https://developers.hubspot.com/changelog/upcoming-api-key-sunset With this change, the library is modified to allow developers to migrate to the private app tokens by changing a environment value. --- README.md | 3 ++- src/HubSpotServiceProvider.php | 8 ++++++++ src/config/hubspot.php | 9 +++++++++ tests/Unit/ServiceProviderTest.php | 12 ++++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 78c68d8..5476b54 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,8 @@ This is a wrapper for the [hubspot/hubspot-php](https://github.com/HubSpot/hubsp - Add `Rossjcooper\LaravelHubSpot\HubSpotServiceProvider::class` to your providers array. - Add `'HubSpot' => Rossjcooper\LaravelHubSpot\Facades\HubSpot::class` to your aliases array. 4. `php artisan vendor:publish --provider="Rossjcooper\LaravelHubSpot\HubSpotServiceProvider" --tag="config"` will create a `config/hubspot.php` file. -5. Add your HubSpot API key into the your `.env` file: `HUBSPOT_API_KEY=yourApiKey` +5. Add your HubSpot API key or private app access token into the `.env` file: `HUBSPOT_API_KEY=yourApiKey` +6. If you use the private app access token, you should alo add `HUBSPOT_USE_OAUTH2=true` to your `.env` file ## Usage You can use either the facade or inject the HubSpot class as a dependency: diff --git a/src/HubSpotServiceProvider.php b/src/HubSpotServiceProvider.php index c15cbf5..0f92a7c 100644 --- a/src/HubSpotServiceProvider.php +++ b/src/HubSpotServiceProvider.php @@ -13,6 +13,14 @@ public function register() { //Bind the HubSpot wrapper class $this->app->bind('Rossjcooper\LaravelHubSpot\HubSpot', function ($app) { + if (config('hubspot.use_oauth2')) { + return HubSpot::createWithOAuth2Token( + config('hubspot.api_key'), + null, + config('hubspot.client_options', []) + ); + } + return HubSpot::create( env('HUBSPOT_API_KEY', config('hubspot.api_key')), null, diff --git a/src/config/hubspot.php b/src/config/hubspot.php index 362ab47..f1f86bd 100644 --- a/src/config/hubspot.php +++ b/src/config/hubspot.php @@ -1,8 +1,17 @@ env('HUBSPOT_API_KEY'), + /* + * Should the library connect via OAuth2 or use the API key. The usage of the API key will be deprecated on + * November 30th, 2022. + */ + 'use_oauth2' => env('HUBSPOT_USE_OAUTH2', false), + 'client_options' => [ 'http_errors' => true, ], diff --git a/tests/Unit/ServiceProviderTest.php b/tests/Unit/ServiceProviderTest.php index e14fe94..73db940 100644 --- a/tests/Unit/ServiceProviderTest.php +++ b/tests/Unit/ServiceProviderTest.php @@ -2,6 +2,7 @@ namespace Tests\Unit; +use Illuminate\Support\Facades\Config; use Rossjcooper\LaravelHubSpot\HubSpot; use Tests\TestCase; @@ -20,4 +21,15 @@ public function test_api_key_set() $this->assertEquals(env('HUBSPOT_API_KEY'), $hubspot->getClient()->key); } + + public function test_oauth2_client_is_built() + { + Config::set('hubspot.use_oauth2', true); + Config::set('hubspot.api_key', 'FooBarBaz'); + + $hubspot = app(HubSpot::class); + + $this->assertEquals('FooBarBaz', $hubspot->getClient()->key); + $this->assertTrue($hubspot->getClient()->oauth2); + } } From 1078b41af348c603499e9d52eea61239a6b69039 Mon Sep 17 00:00:00 2001 From: Ross Cooper Date: Sat, 12 Nov 2022 21:26:08 +0000 Subject: [PATCH 2/2] Fixed support for private apps --- composer.json | 2 +- src/HubSpotServiceProvider.php | 14 +++++++------- src/config/hubspot.php | 16 ++++++++-------- tests/Unit/ServiceProviderTest.php | 14 +++++++------- 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/composer.json b/composer.json index 72fba7b..8ce3489 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ ], "require": { "illuminate/support": ">=5.3", - "hubspot/hubspot-php": "^3.0" + "hubspot/hubspot-php": "^5.0" }, "autoload": { "psr-4": { diff --git a/src/HubSpotServiceProvider.php b/src/HubSpotServiceProvider.php index 0f92a7c..712a814 100644 --- a/src/HubSpotServiceProvider.php +++ b/src/HubSpotServiceProvider.php @@ -13,13 +13,13 @@ public function register() { //Bind the HubSpot wrapper class $this->app->bind('Rossjcooper\LaravelHubSpot\HubSpot', function ($app) { - if (config('hubspot.use_oauth2')) { - return HubSpot::createWithOAuth2Token( - config('hubspot.api_key'), - null, - config('hubspot.client_options', []) - ); - } + if (env('HUBSPOT_USE_OAUTH2', config('hubspot.use_oauth2'))) { + return HubSpot::createWithOAuth2Token( + env('HUBSPOT_API_KEY', config('hubspot.api_key')), + null, + config('hubspot.client_options', []) + ); + } return HubSpot::create( env('HUBSPOT_API_KEY', config('hubspot.api_key')), diff --git a/src/config/hubspot.php b/src/config/hubspot.php index f1f86bd..51f69bd 100644 --- a/src/config/hubspot.php +++ b/src/config/hubspot.php @@ -1,16 +1,16 @@ env('HUBSPOT_API_KEY'), - /* - * Should the library connect via OAuth2 or use the API key. The usage of the API key will be deprecated on - * November 30th, 2022. - */ - 'use_oauth2' => env('HUBSPOT_USE_OAUTH2', false), + /* + * Should the library connect via OAuth2 or use the API key. The usage of the API key will be deprecated on + * November 30th, 2022. + */ + 'use_oauth2' => env('HUBSPOT_USE_OAUTH2', false), 'client_options' => [ 'http_errors' => true, diff --git a/tests/Unit/ServiceProviderTest.php b/tests/Unit/ServiceProviderTest.php index 73db940..a7b8fbf 100644 --- a/tests/Unit/ServiceProviderTest.php +++ b/tests/Unit/ServiceProviderTest.php @@ -23,13 +23,13 @@ public function test_api_key_set() } public function test_oauth2_client_is_built() - { - Config::set('hubspot.use_oauth2', true); - Config::set('hubspot.api_key', 'FooBarBaz'); + { + Config::set('hubspot.use_oauth2', true); + Config::set('hubspot.api_key', 'FooBarBaz'); - $hubspot = app(HubSpot::class); + $hubspot = app(HubSpot::class); - $this->assertEquals('FooBarBaz', $hubspot->getClient()->key); - $this->assertTrue($hubspot->getClient()->oauth2); - } + $this->assertEquals(env('HUBSPOT_API_KEY'), $hubspot->getClient()->key); + $this->assertTrue($hubspot->getClient()->oauth2); + } }