forked from sizeid/oauth2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
userApi.php
69 lines (66 loc) · 2.31 KB
/
userApi.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
<?php
require __DIR__ . '/bootstrap.php';
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Message\Request;
use SizeID\OAuth2\Exceptions\RedirectException;
use SizeID\OAuth2\UserApi;
$redirectUri = getCurrentUrlWithoutParameters();
// For full list of parameters see UserApi::__construct().
// All parameters can be replaced with custom value or implementation.
// redirectUri - url for token retrieval, in this case this script url - replace with custom url
// don't forget to add (whitelist) redirect uri to SizeID for Business account https://business.sizeid.com/integration.settings/#redirect_uri
$userApi = new UserApi(
CLIENT_ID, //clientId from config.php
CLIENT_SECRET, //clientSecret from config.php
$redirectUri
);
if (isset($_GET['code'])) {
// finish authorization process - receive authorization code and call for access token
// code and state default from $_GET['code'] $_GET['state']
$userApi->completeAuthorization();
//redirect to this script url
redirect($redirectUri);
}
try {
// create example get request
$request = createExampleGetRequest();
// or create example put request - uncomment next line and comment line above
// $request = createExamplePutRequest();
// send request
// if needed, acquire access token using authorization code method
$response = $userApi->send($request);
// get response body
$rawBody = $response->getBody()->getContents();
} catch (RedirectException $ex) {
// no access token with refresh token found - new authorization process is required
// or
// refresh token expired - new authorization process is also required
redirect($ex->getRedirectUrl());
} catch (BadResponseException $ex) {
//something went wrong - http response code is not 2xx
$rawBody = $ex->getResponse()->getBody()->getContents();
}
// dump result
dump(json_decode($rawBody, TRUE));
/**
* create request to endpoint 'user' using 'get' method
* @return Request
*/
function createExampleGetRequest()
{
return new Request('get', 'user');
}
/**
* Create request to endpoint 'user/measures' using 'put' method and JSON attached to the request's body.
* this call will change user's bodyHeight to 200.
* @return Request
*/
function createExamplePutRequest()
{
return new Request(
'put',
'user/measures',
['Content-Type' => 'application/json'],
'[{"id": "bodyHeight", "value": 200}]'
);
}