I very much appreciate your answers in advance. I have struggled with this for a good week now, and have read and reread the developer documentation many times to no avail.
Trying to implement sign in with Linkedin using oauth2. I have no issues getting the authorization code, but have been struggling to get the access token consistently. I've noticed I can get the token about 3 to 4 times using curl, and after that the web server stops responding (And if I keep clicking my "sign in with Linkedin" button, I eventually get a 508 error due to exceeding the Entry Processes limit. When it does work, I get the access token back within 3 seconds. I'm nowhere near the usage and limits set by Linkedin. I've tried both file_get_contents() with context post, and it also times out. The website is hosted on shared hosting through NameCheap; php ver 7.2, could they be the culprit? I have other curl codes on other pages calling the PayPal API which works without issues. Below is my code on my callback.php page. $code is the authorization code.
$code = $_GET['code'];
$url = "https://www.linkedin.com/oauth/v2/accessToken";
$params = array(
'grant_type' => 'authorization_code',
'code' => $code,
'redirect_uri' => $redirect_uri,
'client_id' => $client_id,
'client_secret' => $client_secret
);
$payload = http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_CAINFO, '/home/*****/public_html/etc/cacert.pem');
curl_setopt($ch, CURLOPT_TIMEOUT,15);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
$headers = array();
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$accessToken = curl_exec($ch);
if (curl_errno($ch)) {
//echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$accessToken = json_decode($response)->access_token;
$url = "https://api.linkedin.com/v2/me?oauth2_access_token=".$accessToken;
$urle = "https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))&oauth2_access_token=".$accessToken;
$urlp = "https://api.linkedin.com/v2/me?projection=(id,profilePicture(displayImage~:playableStreams))&oauth2_access_token=".$accessToken;
$user = file_get_contents($url, false);
$usere = file_get_contents($urle, false);
$userp = file_get_contents($urlp, false);
Below is the verbose output when the code times out.
* Trying 108.174.10.10...
* TCP_NODELAY set
* Connected to www.linkedin.com (108.174.10.10) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
* CAfile: /home/*******/public_html/etc/cacert.pem
CApath: none
* Operation timed out after 10000 milliseconds with 0 out of 0 bytes received
* Closing connection 0