Wasn't sure yet whether to post this publicly. I've adjusted the PHP code snippet for the OAuth. Curl allows you to trap a redirect and this snippet just shows how to do that for the token. The regex searches for the redirect url in the returned header, it can be adjusted to make sure it starts with "Location: ".
<?php
$client_id = '';
$client_secret = '';
$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
if (!isset($_REQUEST['code']))
{
header("Location: http://www.daniweb.com/api/oauth?client_id=$client_id&redirect_uri=" . urlencode($current_url));
}
$ch = curl_init('http://www.daniweb.com/api/access_token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'code' => $_REQUEST['code'],
'redirect_uri' => $current_url,
'client_id' => $client_id,
'client_secret' => $client_secret
));
$result = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_code == 301 or $http_code == 302) {
preg_match("@https?://([-\w\.]+)+(:\d+)?(/([\w/_\-\.]*(\?\S+)?)?)?@", $result, $m);
$target_url = $m[0];
$url_parts = parse_url($target_url);
parse_str($url_parts['query'], $query_parts);
$token = $query_parts['access_token'];
echo 'Your Access Token for this session is ' . $token;
}
?>
Basically, you can do the same check for the redirect to the oauth page, and only actually redirect if you do not get a code back, because at that point there is no approval yet.