Skip to main content

500 Error with PHP OAuth

I'm having an odd problem with the API. I wrote a script which was working fine in early August, but when I returned to using it I now get a 500 error when generating a token. If I make a new app, it will generate a token fine once but any repeated executions will return a 500. I haven't changed anything, and it was worked fine before. Help?

NB doesn't seem to have a good way to post code, but here's the blog post I wrote a few weeks ago with my code: http://www.richiroutreach.com/blog/working-php-and-nationbuilder-api

// OAuth 2 Library
require_once ROOT . '/OAuth2/Client.php';
require_once ROOT . '/OAuth2/GrantType/IGrantType.php';
require_once ROOT . '/OAuth2/GrantType/AuthorizationCode.php'; 

// Client ID and Secret from Nation Builder 
const CLIENT_ID = 'id';
const CLIENT_SECRET = 'secret';

// Constants we need to talk to Nation Builder
const REDIRECT_URI = "http://example.com/create_user.php";
const AUTHORIZATION_ENDPOINT = "https://xxx.nationbuilder.com/oauth/authorize"; 
const TOKEN_ENDPOINT = "https://xxx.nationbuilder.com/oauth/token";
const REQUEST_ENDPOINT = "https://xxx.nationbuilder.com/api/v1";

// Start a new OAuth2 Client 
$client = new OAuth2\Client(CLIENT_ID, CLIENT_SECRET); 

// Check if we have an authorize code, if not obtain one 
if (!isset($_GET['code'])) { 
     // Generate the URL we need to go to get the token 
     $auth_url = $client->getAuthenticationUrl(AUTHORIZATION_ENDPOINT, REDIRECT_URI); 

     // Redirect to that URL (which then sends us back to the REDIRECT_URI with a code, IE this page) 
     header('Location: ' . $auth_url); 

     // End this process 
     die('Redirect'); 
} 

// We have a code, so generate the parameters we need to get an access token 
$params = array('code' => $_GET['code'], 'redirect_uri' => REDIRECT_URI); 

// Obtain our access token 
$response = $client->getAccessToken(TOKEN_ENDPOINT, 'authorization_code', $params);

Showing 2 reactions

How would you tag this suggestion?
Please check your e-mail for a link to activate your account.