/** * Tests if the OAuth::getAuthUrl function returns a correct URL. */ public function testAuthUrl() { $params = ['client_id' => 'test_client', 'state' => 'St8fulbar']; $expected = 'https://z3ntestsub.zendesk.com/oauth/authorizations/new?response_type=code&client_id=test_client&state=St8fulbar&scope=read+write'; $this->assertEquals($expected, OAuth::getAuthUrl('z3ntestsub', $params)); }
<?php /** * This is a sample oAuth flow you can follow in your application. */ use Zendesk\API\Utilities\OAuth; include "../../vendor/autoload.php"; if (isset($_POST['action']) && 'redirect' === $_POST['action']) { $state = base64_encode(serialize($_POST)); // Get the oAuth URI using the utility function $oAuthUrl = OAuth::getAuthUrl($_POST['subdomain'], ['client_id' => $_POST['client_id'], 'state' => $state]); header('Location: ' . $oAuthUrl); } elseif (isset($_REQUEST['code'])) { /** * This block acts as the redirect_uri, once you receive an authorization_code ($_GET['code']). */ $params = unserialize(base64_decode($_GET['state'])); $params['code'] = $_REQUEST['code']; $params['redirect_uri'] = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; try { // Request for an access token by passing an instance of GuzzleHttp\Client, your Zendesk subdomain, and the // following params ['client_id', 'client_secret', 'redirect_uri'] $response = OAuth::getAccessToken(new GuzzleHttp\Client(), $params['subdomain'], $params); echo "<h1>Success!</h1>"; echo "<p>Your OAuth token is: " . $response->access_token . "</p>"; echo "<p>Use this code before any other API call:</p>"; echo "<code><?php<br />\$client = new ZendeskAPI(\$subdomain);<br />\$client->setAuth(\\Zendesk\\API\\Utilities\\Auth::OAUTH, ['token' => " . $response->access_token . "]');<br />?></code>"; } catch (\Zendesk\API\Exceptions\ApiResponseException $e) { echo "<h1>Error!</h1>"; echo "<p>We couldn't get an access token for you. Please check your credentials and try again.</p>"; echo "<p>" . $e->getMessage() . "</p>";