PHP oauth2 authorization returns html login


#1

Hi there,

I’m trying to implement an integration between airvantage and a local network intranet.
I’ve been following the instructions on the various api documents but all seem to brush over the log in requirement.

Could someone shed some light on how to authenticate and get a token in PHP please?

This is what I’m doing, and I’m expecting to receive the code for the token request.

$auth_url = 'https://eu.airvantage.net/api/oauth/authorize?client_id=' . $client_id . '&response_type=code&redirect_uri=' . $redirect_uri;

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $auth_url);
$result = curl_exec($curl);

Thank you in advance.


#2

Hello,
Did you succeeded?? I suffer from the same problem, and not able to find a solution…
Please, provide some additional info, if you were successful with this.
Thank you!


#3

Hi,

here a sample code which may help you:
// Setup a handle for CURL
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,5);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,true);

// An AirVantage M2M Cloud authorization temporary code is passed on the URL (we are in the callback of the OAuth process)
  	// Using it to require a token
  	$authUrl = "http://na.airvantage.net/api/oauth/token?grant_type=authorization_code&code=" . $authCode
  		. "&client_id=" . $clientId . "&client_secret=" . $secretKey;
  	$response = decodeAVJson($curl_handle, $authUrl);

  	if (!empty($response->access_token))
  	{
		$token = $response->access_token;
		$validity = time() + $response->expires_in;
		$refresh = $response->refresh_token;
	
		// Store the token info into the session
		$_SESSION['AirVantageToken'] = $token;
		$_SESSION['AirVantageTokenValidity'] = $validity;
		$_SESSION['AirVantageRefreshToken'] = $refresh;
	
		print "AirVantage authentication succeeded and token retrieved<br />";

		return $token;
	}

And
// Returns an objet decoded from the Ajax response from AirVantage to the REST query passed in the $url
// parameter
function decodeAVJson($curl_handle, $url)
{
// uncomment to trace each query sent to AV
print "HTTP REQUEST = " . $url . “
”;

	curl_setopt($curl_handle,CURLOPT_URL,$url);
	$json = curl_exec($curl_handle);
	
	// uncomment to trace each JSON responses received from AV
	print "<font color='brown'><i>JSON RECEIVED = " . $json . "</i></font><br/>";

	if($json !== false)
	{
		return json_decode($json);
	}
	else
	{
		return "Could not communicate with AirVantage (CURL error): " . curl_error($curl_handle);
	}
}

#4

Hi there,

I believe I did but it’s been so long since I figured it out that I can’t remember what the solution was now.
The comment from rjacolin might help but I’ve not tried it. If I find the answer, I will post it for people having this issue in the future.

Cheers,
Natasha