Skip to content

Commit

Permalink
Merge pull request #34 from wgriffioen/add-private-app-support
Browse files Browse the repository at this point in the history
Add support for private app tokens
  • Loading branch information
rossjcooper authored Nov 12, 2022
2 parents 73dbd04 + 1078b41 commit 1bb984d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
],
"require": {
"illuminate/support": ">=5.3",
"hubspot/hubspot-php": "^3.0"
"hubspot/hubspot-php": "^5.0"
},
"autoload": {
"psr-4": {
Expand Down
8 changes: 8 additions & 0 deletions src/HubSpotServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ public function register()
{
//Bind the HubSpot wrapper class
$this->app->bind('Rossjcooper\LaravelHubSpot\HubSpot', function ($app) {
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')),
null,
Expand Down
9 changes: 9 additions & 0 deletions src/config/hubspot.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
<?php

return [
/*
* Either the API key or the private app access token
*/
'api_key' => 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,
],
Expand Down
12 changes: 12 additions & 0 deletions tests/Unit/ServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests\Unit;

use Illuminate\Support\Facades\Config;
use Rossjcooper\LaravelHubSpot\HubSpot;
use Tests\TestCase;

Expand All @@ -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(env('HUBSPOT_API_KEY'), $hubspot->getClient()->key);
$this->assertTrue($hubspot->getClient()->oauth2);
}
}

0 comments on commit 1bb984d

Please sign in to comment.