【问题标题】:PHP - Authorizate on LinkedIN APIPHP - 对 LinkedIN API 进行授权
【发布时间】:2015-12-02 20:56:38
【问题描述】:

我尝试进行一些简单的 LinkedIN api 调用,从授权开始。 出于测试目的,我只创建了这个我知道它有效的 $url 并打开它。

但是有一个区别,如果我通过下面的代码调用这个 URL 并输入我的链接凭据,我会自动重定向到

http://localhost/uas/oauth2/authorizedialog/submit

这看起来像是用“localhost”替换了一些linkedin URL,我确定它不是我的redirect_uri,我没有在任何地方定义它。

我找不到发生这种情况的原因,如果我在登录后直接在浏览器中打开页面(只需从 $url 复制粘贴 URL),我将被重定向到 example.com。

我的代码:

//define path

$url = "https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=MyIdHere&redirect_uri=https%3A%2F%2Fwww.example.com%2Fauth%2Flinkedin&state=DCEeFWf45A53sdfKef424&scope=r_basicprofile";

// set up the curl resource
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/x-www-form-urlencoded'          
));       

 // execute the request
 $output = curl_exec($ch);

 // output the auth information - includes the header
 var_dump($output);

// close curl resource to free up system resources
curl_close($ch);
?>

感谢您的建议。

【问题讨论】:

    标签: php oauth-2.0 linkedin


    【解决方案1】:

    您的 cURL 代码不像浏览器那样跟随重定向,因此 LinkedIn 提供的响应中的 Location 标头没有后续操作。另见Make curl follow redirects?

    但是:您也不应该实际尝试修复该问题,因为 OAuth 2.0 流程旨在通过浏览器与用户交互,并且在 PHP 中对其进行逆向工程有时会中断。相反,您应该通过常规的基于浏览器的流程获取访问令牌,并在您的 PHP 代码中使用它来访问 LinkedIn API。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-28
      • 2020-10-06
      • 1970-01-01
      • 2016-08-04
      相关资源
      最近更新 更多