Skip to content

Commit

Permalink
API-2195 Add client credentials auth support for PHP SDK (#75)
Browse files Browse the repository at this point in the history
* API-2195 add methods for client credentials, sample for oauth client credentials

* API-2195 update README
  • Loading branch information
ahongbynder authored Jan 17, 2025
1 parent 6cc261a commit c31b985
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ For instance, if we only wanted to retrieve **2 images** here is what the call w
All the calls are **Asynchronous**, which means they will return a **Promise** object, making it a bit more flexible in order to adjust to any kind of application.
Again, for a more thorough example there is a sample [application use case](sample/sample.php) in this repo.

### Client Credentials

OAuth can be used via authorization code or client credentials. To use client credentials, initialize a Bynder client
with OAuth2 Configuration and make call to get a token via:

`$bynder->getAccessTokenClientCredentials();`

Sample file found in `sample/OAuthClientCredentialsSample.php`.

`php OAuthClientCredentialsSample.php`

## Methods Available
These are the methods currently available on the **Bynder PHP SDK**, refer to the [Bynder API Docs](http://docs.bynder.apiary.io/)) for more specific details on the calls.

Expand Down
69 changes: 69 additions & 0 deletions sample/OAuthClientCredentialsSample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
require_once(__DIR__ . '/../vendor/autoload.php');
require_once(__DIR__ . '/sample_config.php');
use Bynder\Api\BynderClient;
use Bynder\Api\Impl\OAuth2;


try {
// instantiate BynderClient, redirectUri and token are null (client credentials)
$bynder = new BynderClient(new Oauth2\Configuration(
$bynderDomain,
null,
$clientId,
$clientSecret,
null,
['timeout' => 5] // Guzzle HTTP request options
));

// use client credentials grant type to get access token
if ($token === null) {
$token = $bynder->getAccessTokenClientCredentials();
}

$assetBankManager = $bynder->getAssetBankManager();

// Get Brands. Returns a Promise.
$brandsListPromise = $assetBankManager->getBrands();
// Wait for the promise to be resolved.
$brandsList = $brandsListPromise->wait();

if (!empty($brandsList)) {
foreach ($brandsList as $brand) {
echo("Brand ID: " . $brand['id'] . "\n");
var_dump($brand);
}
}

// get derivatives
$derivativesPromise = $assetBankManager->getDerivatives();
$derivativesList = $derivativesPromise->wait();

if (!empty($derivativesList)) {
echo("Derivatives: " . "\n");
var_dump($derivativesList);
}

// Get Media Items list.
// Optional filter.
$query = [
'count' => true,
'limit' => 10
];

$mediaListPromise = $assetBankManager->getMediaList($query);
$mediaList = $mediaListPromise->wait();

// outputs list of media items, print media item
if (!empty($mediaList) && !empty($mediaList['media'])) {
foreach ($mediaList['media'] as $media) {
echo("Media ID: " . $media['id'] . "\n");
var_dump($media);
}
}
} catch (Exception $e) {
var_dump($e);
}

?>

13 changes: 13 additions & 0 deletions src/Bynder/Api/BynderClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ public function getAccessToken($code)
return $token;
}

/**
* Returns the Oauth access token using client credentials grant type.
*
* @return \League\OAuth2\Client\Token\AccessToken
*/
public function getAccessTokenClientCredentials()
{
$token = $this->requestHandler->getAccessTokenClientCredentials();
$this->configuration->setToken($token);

return $token;
}

/**
* Retrieve all users.
*
Expand Down
7 changes: 7 additions & 0 deletions src/Bynder/Api/Impl/OAuth2/RequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ public function getAccessToken($code)
);
}

public function getAccessTokenClientCredentials()
{
return $this->oauthProvider->getAccessToken(
'client_credentials'
);
}

protected function sendAuthenticatedRequest($requestMethod, $uri, $options = [])
{
$this->configuration->refreshToken($this->oauthProvider);
Expand Down

0 comments on commit c31b985

Please sign in to comment.