carlos-mg89 /
PHPoAuthLib
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * Example of connecting to Quickbooks service. |
||
| 5 | * |
||
| 6 | * PHP version 5.4 |
||
| 7 | * |
||
| 8 | * @author Elliot Chance <[email protected]> |
||
| 9 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
||
| 10 | */ |
||
| 11 | |||
| 12 | use OAuth\Common\Consumer\Credentials; |
||
| 13 | use OAuth\Common\Storage\Session; |
||
| 14 | use OAuth\OAuth1\Service\QuickBooks; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Bootstrap the example. |
||
| 18 | */ |
||
| 19 | require_once __DIR__ . '/bootstrap.php'; |
||
| 20 | |||
| 21 | // Session storage |
||
| 22 | $storage = new Session(); |
||
| 23 | |||
| 24 | // Setup the credentials for the requests |
||
| 25 | $credentials = new Credentials( |
||
| 26 | $servicesCredentials['quickbooks']['key'], |
||
| 27 | $servicesCredentials['quickbooks']['secret'], |
||
| 28 | $currentUri->getAbsoluteUri() |
||
| 29 | ); |
||
| 30 | |||
| 31 | // So we don't have to repeat ourselves. |
||
| 32 | $serviceName = 'QuickBooks'; |
||
| 33 | |||
| 34 | // Instantiate the Quickbooks service using the credentials, http client and |
||
| 35 | // storage mechanism for the token |
||
| 36 | /** @var QuickBooks $quickbooksService */ |
||
| 37 | $quickbooksService = $serviceFactory->createService( |
||
| 38 | $serviceName, $credentials, $storage |
||
| 39 | ); |
||
| 40 | |||
| 41 | if (!empty($_GET['oauth_token'])) { |
||
| 42 | $token = $storage->retrieveAccessToken($serviceName); |
||
| 43 | |||
| 44 | // This was a callback request from QuickBooks, get the token |
||
| 45 | $quickbooksService->requestAccessToken( |
||
| 46 | $_GET['oauth_token'], |
||
| 47 | $_GET['oauth_verifier'], |
||
| 48 | $token->getRequestTokenSecret() |
||
| 49 | ); |
||
| 50 | |||
| 51 | // Send a request now that we have access token |
||
| 52 | $companyId = $_GET['realmId']; |
||
| 53 | $url = "/v3/company/$companyId/account/1"; |
||
| 54 | $result = json_decode($quickbooksService->request($url)); |
||
| 55 | |||
| 56 | echo 'result: <pre>' . print_r($result, true) . '</pre>'; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 57 | } elseif (!empty($_GET['go']) && $_GET['go'] === 'go') { |
||
| 58 | // extra request needed for oauth1 to request a request token :-) |
||
| 59 | $token = $quickbooksService->requestRequestToken(); |
||
| 60 | |||
| 61 | $url = $quickbooksService->getAuthorizationUri(['oauth_token' => $token->getRequestToken()]); |
||
| 62 | header('Location: ' . $url); |
||
| 63 | } else { |
||
| 64 | $url = $currentUri->getRelativeUri() . '?go=go'; |
||
| 65 | echo "<a href='$url'>Login with QuickBooks!</a>"; |
||
| 66 | } |
||
| 67 |