LinkedIn OAuth2使用curl要求访问令牌超时

时间:2019-01-15 23:17:32

标签: php linkedin

非常感谢您提前回答。我已经为此苦苦挣扎了一个星期,并且已经多次阅读并重读了开发人员文档,但无济于事。

尝试使用oauth2与Linkedin进行登录。获取授权代码没有问题,但是一直在努力获取访问令牌。我注意到我可以使用curl大约获得3到4次令牌,然后Web服务器停止响应(如果我继续单击“使用Linkedin登录”按钮,由于超出了,最终会收到508错误进入流程限制。当它起作用时,我会在3秒内收回访问令牌。我距离Linkedin设置的使用和限制还很遥远。我已经尝试了context_post的file_get_contents(),并且也超时了。该网站通过NameCheap托管在共享主机上; php ver 7.2,可能是罪魁祸首吗?我在其他页面上调用了其他curl代码,调用了PayPal API,这没有问题,下面是我在callback.php页面上的代码。是授权码。

$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);

下面是代码超时时的详细输出。

*   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

0 个答案:

没有答案