Skip to content

Instantly share code, notes, and snippets.

@mmattax
Last active May 6, 2020 13:30
Show Gist options
  • Save mmattax/4565791 to your computer and use it in GitHub Desktop.
Save mmattax/4565791 to your computer and use it in GitHub Desktop.
Formstack OAuth2 Flow
<?php
define('CLIENT_ID', 'YOUR_CLIENT_ID');
define('CLIENT_SECRET', 'YOUR_CLIENT_SECRET');
define('REDIRECT_URL', 'YOUR_REDIRECT_URL'); // for testing, use the URL to this PHP file.
define('AUTHORIZE_URL', 'https://www.formstack.com/api/v2/oauth2/authorize');
define('TOKEN_URL', 'https://www.formstack.com/api/v2/oauth2/token');
if (!empty($_GET['code'])) {
/**
* We have an authorization code. We now exchange it for an access token.
*/
$ch = curl_init(TOKEN_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array(
'grant_type' => 'authorization_code',
'client_id' => CLIENT_ID,
'redirect_uri' => REDIRECT_URL,
'client_secret' => CLIENT_SECRET,
'code' => $_GET['code']
)));
// oauth2 contains the the access_token.
$oauth2 = json_decode(curl_exec($ch));
/**
* Make an API call.
*/
$ch = curl_init('https://www.formstack.com/api/v2/form');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $oauth2->access_token
));
$forms = json_decode(curl_exec($ch));
print '<pre>';
print_r($forms);
print '</pre>';
} else {
/**
* Send the user to the authorization page.
*/
$auth_url = AUTHORIZE_URL . '?' . http_build_query(array(
'client_id' => CLIENT_ID,
'redirect_uri' => REDIRECT_URL,
'response_type' => 'code'
));
header('Location:' . $auth_url);
exit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment