29/06/2015, 08:10
|
| | Fecha de Ingreso: junio-2007
Mensajes: 298
Antigüedad: 17 años, 7 meses Puntos: 5 | |
Respuesta: API Linkedin: obtener r_fullprofile Refloto este tema para ver si alguien ha podido encontrar alguna solución.
He probado varias maneras pero no consigo obtener el r_fullprofile. Con este código me obtiene solo los datos básicos y si en el scope le pongo r_fullprofile me da error. Os copio el código por si alguien se le ocurre alguna posible solución:
Código:
<?php
define('API_KEY','xxxxx');
define('API_SECRET','xxxx');
define('REDIRECT_URI', 'http://midominio.com/pruebaLinkedin.php');
session_name('linkedin');
session_start();
if (isset($_GET['error'])) {
// LinkedIn returned an error
print $_GET['error'] . ': ' . $_GET['error_description'];
exit;
} elseif (isset($_GET['code'])) {
// User authorized your application
if ($_SESSION['state'] == $_GET['state']) {
// Get token so you can make API calls
$resultado = getAccessToken();
if ($resultado)
echo "Obtenido bien el Access Token<br>";
else
echo "Error al obtener el Access Token<br>";
} else {
// CSRF attack? Or did you mix up your states?
exit;
}
} else {
if ((empty($_SESSION['expires_at'])) || (time() > $_SESSION['expires_at'])) {
// Token has expired, clear the state
$_SESSION = array();
}
if (empty($_SESSION['access_token'])) {
// Start authorization process
getAuthorizationCode();
}
}
$user = fetch('GET','/v1/people/~:(id,first-name,skills,educations,languages,twitter-accounts)?format=json');
echo "<pre>";
print_r($user);
echo "</pre>";
exit;
// *********************************************************
function getAuthorizationCode() {
echo "entra en getAuthorizationCode<hr>";
$params = array(
'response_type' => 'code',
'client_id' => API_KEY,
/*'scope' => 'r_fullprofile',*/
'state' => uniqid('', true), // unique long string
'redirect_uri' => REDIRECT_URI,
);
// Authentication request
$url = 'https://www.linkedin.com/uas/oauth2/authorization?' . http_build_query($params);
// Needed to identify request when it returns to us
$_SESSION['state'] = $params['state'];
// Redirect user to authenticate
header("Location: $url");
exit;
}
// **********************************************
function getAccessToken() {
echo date('Y-m-d H:i:s')." entra en getAccessToken<hr>";
$params = array(
'grant_type' => 'authorization_code',
'client_id' => API_KEY,
'client_secret' => API_SECRET,
'code' => $_GET['code'],
'redirect_uri' => REDIRECT_URI,
);
// Access Token request
$url = 'https://www.linkedin.com/uas/oauth2/accessToken?' . http_build_query($params);
// Tell streams to make a POST request
$context = stream_context_create(
array('http' =>
array('method' => 'POST',
)
)
);
// Retrieve access token information
echo "<hr>";
$response = file_get_contents($url, false, $context);
// Native PHP object, please
$token = json_decode($response);
// Store access token and expiration time
$_SESSION['access_token'] = $token->access_token; // guard this!
$_SESSION['expires_in'] = $token->expires_in; // relative time (in seconds)
$_SESSION['expires_at'] = time() + $_SESSION['expires_in']; // absolute time
return true;
}
// *********************************************************
function fetch($method, $resource, $body = '') {
echo date('Y-m-d H:i:s')." entra en fetch<hr>";
// print $_SESSION['access_token'];
$opts = array(
'http'=>array(
'method' => $method,
'header' => "Authorization: Bearer " . $_SESSION['access_token'] . "\r\n" . "x-li-format: json\r\n"
)
);
// Need to use HTTPS
$url = 'https://api.linkedin.com' . $resource;
// Append query parameters (if there are any)
if (count($params)) { $url .= '?' . http_build_query($params); }
// Tell streams to make a (GET, POST, PUT, or DELETE) request
// And use OAuth 2 access token as Authorization
$context = stream_context_create($opts);
///echo "FETCH:".$url." con los parametros ".$context;
$rutaTotal = $url."&oauth2_access_token=".$_SESSION['access_token'];
echo "<br><br>FETCH:".$rutaTotal;
$response = file_get_contents($rutaTotal);
echo "<br><br>RESPUESTA:".$response;
exit;
// Hocus Pocus
// $response = file_get_contents($url, false, $context);
// Native PHP object, please
return json_decode($response);
}
?>
|