public function testAccessTokenIsRequested() { $this->mockApiResponses([new Response(200, [], json_encode(['access_token' => 12345]))]); $params = ['code' => 'adwo123ijo', 'client_id' => 'test_client', 'client_secret' => 'dwapjoJ123d8w9a01-', 'grant_type' => 'authorization_code', 'scope' => 'read write', 'redirect_uri' => 'https://test.foo.com']; OAuth::getAccessToken($this->client->guzzle, 'test', $params); $this->assertLastRequestIs(['method' => 'POST', 'requestUri' => 'https://test.zendesk.com/oauth/tokens', 'postFields' => $params, 'headers' => ['accept' => false]]); }
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>"; } } else { // A simple form to help you get started. ?> <!DOCTYPE html> <html lang="en"> <head>