OAuth on Neoseeker

< NeoAPI


As of Spring 2011, Neoseeker uses the OAuth protocol to authenticate users using our API.

Contents

OAuth Process

Once you registered an app on Neoseeker, you may start writing an OAuth process so your user can use your app using their Neoseeker account.

PHP Example

Request Token

$consumer_key = 'YOUR_APP_CONSUMER_KEY';
$consumer_secret = 'YOUR_APP_CONSUMER_SECRET';

$request_url = 'http://api.neoseeker.com/oauth/request.php';

$oauth = new OAuth($consumer_key, $consumer_secret);
//$oauth->enableDebug(); // This would help you debug

$request_token_info = array();
try {
	$request_token_info = $oauth->getRequestToken($request_url);

} catch (OAuthException $E) {
	echo "Response: ". $E->getMessage() . "\n";

	//print_r($oauth->debugInfo);	// This would print out debug info that explains what went wrong
}

if ($request_token_info) {
	$_SESSION['testapp_oauth_token'] = $token = $request_token_info['oauth_token'];
	$_SESSION['testapp_oauth_token_secret'] = $request_token_info['oauth_token_secret'];

	header('Location: '.$request_token_info['authorize_url']."?oauth_token={$token}");
	die;
}

At the end of this process, the script will redirect the user to the authorize page, on which, the user will grant access to the app.

After the user grant access to your app, they will be redirected to your callback url as specified in your app settings.

Verify Token

$consumer_key = 'YOUR_APP_CONSUMER_KEY';
$consumer_secret = 'YOUR_APP_CONSUMER_SECRET';

$oauth_token = $_GET['oauth_token'];
$oauth_verifier = $_GET['oauth_verifier'];

$access_url = 'http://api.neoseeker.com/oauth/access.php';

$oauth_token_secret = $_SESSION['testapp_oauth_token_secret'];

$oauth = new OAuth($consumer_key, $consumer_secret);
//$oauth->enableDebug(); 

$access_token_info = array();
try {
	$oauth->setNonce(rand());
	$oauth->setToken($oauth_token, $oauth_token_secret);
	$access_token_info = $oauth->getAccessToken($access_url, null, $oauth_verifier);
	
	// OAuth access key and secret will be returned in an array format like the following
	// Array ( [oauth_token] => '_token_', [oauth_token_secret] => '_secret_' ) 
} catch (OAuthException $E) {
	echo "Response: ". $E->getMessage() . "\n";

	//print_r($oauth->debugInfo);
}

Making an OAuth call

After your user registered an OAuth key / secret on Neoseeker (as well as your site), you can make API calls on behalf of the user.

PHP example

$consumer_key = 'YOUR_APP_CONSUMER_KEY';
$consumer_secret = 'YOUR_APP_CONSUMER_SECRET';

// Log in as me
$token = 'USER_TOKEN';
$token_secret = 'USER_TOKEN_SECRET';

try {
	$oauth = new OAuth($consumer_key, $consumer_secret);
	//$oauth->enableDebug();

	$oauth->setToken($token, $token_secret);

	$oauth->fetch('http://api.neoseeker.com/forum/get_pm_counts.json');

	$response_info = $oauth->getLastResponseInfo();
	header("Content-Type: {$response_info["content_type"]}");
	echo $oauth->getLastResponse();

} catch (OAuthException $E) {
	echo "Response: ". $E->getMessage() . "\n";

	//print_r($oauth->debugInfo);
}
Last edited by Chiggins on 8 March 2011 at 18:43
This page has been accessed 1,221 times.