forked from mxr576/apigee-client-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_new_app_with_credential.php
76 lines (66 loc) · 3.26 KB
/
create_new_app_with_credential.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/*
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use Apigee\Edge\Api\Management\Controller\DeveloperAppController;
use Apigee\Edge\Api\Management\Controller\DeveloperAppCredentialController;
use Apigee\Edge\Api\Management\Entity\DeveloperApp;
use Apigee\Edge\Exception\ApiException;
use Apigee\Edge\Exception\ApiRequestException;
use Apigee\Edge\Exception\ClientErrorException;
use Apigee\Edge\Exception\ServerErrorException;
use Apigee\Edge\Structure\AttributesProperty;
require_once 'authentication.inc';
$developerMail = '[email protected]';
$dac = new DeveloperAppController($clientFactory->getOrganization(), $developerMail, $clientFactory->getClient());
try {
// Create a new developer app.
/** @var \Apigee\Edge\Api\Management\Entity\DeveloperApp $developerApp */
$developerApp = new DeveloperApp(['name' => 'test_app_1']);
$developerApp->setDisplayName('My first app');
$developerApp->setAttributes(new AttributesProperty(['bar' => 'baz']));
$dac->create($developerApp);
$dacc = new DeveloperAppCredentialController($organization, $developerMail, $developerApp->id(), $client);
$credAttributes = new AttributesProperty(['foo' => 'bar']);
$apiProducts = ['product_1', 'product_2'];
$scopes = ['scope 1', 'scope 2'];
// Add products, attributes, and scopes to the auto-generated credential that was created along with the app.
$credentials = $developerApp->getCredentials();
/** @var \Apigee\Edge\Api\Management\Entity\AppCredential $credential */
$credential = reset($credentials);
$dacc->addProducts($credential->id(), $apiProducts);
$dacc->updateAttributes($credential->id(), $credAttributes);
$dacc->overrideScopes($credential->id(), $scopes);
// Create a new, auto-generated credential that expires after 1 week.
$dacc->generate($apiProducts, $developerApp->getAttributes(), $developerApp->getCallbackUrl(), $scopes, 604800000);
// Create a credential with a specific key and secret and add the same products, attributes and scopes to it.
$credential = $dacc->create('MY_CONSUMER_KEY', 'MY_CONSUMER_SECRET');
$dacc->addProducts($credential->id(), $apiProducts);
$dacc->updateAttributes($credential->id(), $credAttributes);
$dacc->overrideScopes($credential->id(), $scopes);
} catch (ClientErrorException $e) {
// HTTP code >= 400 and < 500. Ex.: 401 Unauthorised.
if ($e->getEdgeErrorCode()) {
echo $e->getEdgeErrorCode();
} else {
echo $e;
}
} catch (ServerErrorException $e) {
// HTTP code >= 500 and < 600. Ex.: 500 Server error.
} catch (ApiRequestException $e) {
// The request has failed, ex.: networking issues.
} catch (ApiException $e) {
// Anything else, because this is the parent class of all the above.
}