Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API-2195 Add client credentials auth support for PHP SDK #75

Merged
merged 2 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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