Skip to content

Commit c31b985

Browse files
authored
API-2195 Add client credentials auth support for PHP SDK (#75)
* API-2195 add methods for client credentials, sample for oauth client credentials * API-2195 update README
1 parent 6cc261a commit c31b985

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,17 @@ For instance, if we only wanted to retrieve **2 images** here is what the call w
9090
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.
9191
Again, for a more thorough example there is a sample [application use case](sample/sample.php) in this repo.
9292

93+
### Client Credentials
94+
95+
OAuth can be used via authorization code or client credentials. To use client credentials, initialize a Bynder client
96+
with OAuth2 Configuration and make call to get a token via:
97+
98+
`$bynder->getAccessTokenClientCredentials();`
99+
100+
Sample file found in `sample/OAuthClientCredentialsSample.php`.
101+
102+
`php OAuthClientCredentialsSample.php`
103+
93104
## Methods Available
94105
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.
95106

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
require_once(__DIR__ . '/../vendor/autoload.php');
3+
require_once(__DIR__ . '/sample_config.php');
4+
use Bynder\Api\BynderClient;
5+
use Bynder\Api\Impl\OAuth2;
6+
7+
8+
try {
9+
// instantiate BynderClient, redirectUri and token are null (client credentials)
10+
$bynder = new BynderClient(new Oauth2\Configuration(
11+
$bynderDomain,
12+
null,
13+
$clientId,
14+
$clientSecret,
15+
null,
16+
['timeout' => 5] // Guzzle HTTP request options
17+
));
18+
19+
// use client credentials grant type to get access token
20+
if ($token === null) {
21+
$token = $bynder->getAccessTokenClientCredentials();
22+
}
23+
24+
$assetBankManager = $bynder->getAssetBankManager();
25+
26+
// Get Brands. Returns a Promise.
27+
$brandsListPromise = $assetBankManager->getBrands();
28+
// Wait for the promise to be resolved.
29+
$brandsList = $brandsListPromise->wait();
30+
31+
if (!empty($brandsList)) {
32+
foreach ($brandsList as $brand) {
33+
echo("Brand ID: " . $brand['id'] . "\n");
34+
var_dump($brand);
35+
}
36+
}
37+
38+
// get derivatives
39+
$derivativesPromise = $assetBankManager->getDerivatives();
40+
$derivativesList = $derivativesPromise->wait();
41+
42+
if (!empty($derivativesList)) {
43+
echo("Derivatives: " . "\n");
44+
var_dump($derivativesList);
45+
}
46+
47+
// Get Media Items list.
48+
// Optional filter.
49+
$query = [
50+
'count' => true,
51+
'limit' => 10
52+
];
53+
54+
$mediaListPromise = $assetBankManager->getMediaList($query);
55+
$mediaList = $mediaListPromise->wait();
56+
57+
// outputs list of media items, print media item
58+
if (!empty($mediaList) && !empty($mediaList['media'])) {
59+
foreach ($mediaList['media'] as $media) {
60+
echo("Media ID: " . $media['id'] . "\n");
61+
var_dump($media);
62+
}
63+
}
64+
} catch (Exception $e) {
65+
var_dump($e);
66+
}
67+
68+
?>
69+

src/Bynder/Api/BynderClient.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,19 @@ public function getAccessToken($code)
7979
return $token;
8080
}
8181

82+
/**
83+
* Returns the Oauth access token using client credentials grant type.
84+
*
85+
* @return \League\OAuth2\Client\Token\AccessToken
86+
*/
87+
public function getAccessTokenClientCredentials()
88+
{
89+
$token = $this->requestHandler->getAccessTokenClientCredentials();
90+
$this->configuration->setToken($token);
91+
92+
return $token;
93+
}
94+
8295
/**
8396
* Retrieve all users.
8497
*

src/Bynder/Api/Impl/OAuth2/RequestHandler.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ public function getAccessToken($code)
3535
);
3636
}
3737

38+
public function getAccessTokenClientCredentials()
39+
{
40+
return $this->oauthProvider->getAccessToken(
41+
'client_credentials'
42+
);
43+
}
44+
3845
protected function sendAuthenticatedRequest($requestMethod, $uri, $options = [])
3946
{
4047
$this->configuration->refreshToken($this->oauthProvider);

0 commit comments

Comments
 (0)