申请授权码Linkedin

时间:2017-11-09 16:09:17

标签: php api curl linkedin file-get-contents

我了解linkedin api所以首先我需要知道我如何申请授权码?

我在这里用这种类型尝试了很多问题,但我仍然明白。

来自docs据说:

“简单的电话”:

https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=123456789&redirect_uri=https%3A%2F%2Fwww.example.com%2Fauth%2Flinkedin&state=987654321&scope=r_basicprofile

但我需要知道如何使用php执行该调用。

我试试:

$response = file_get_contents("https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=123456789&redirect_uri=https%3A%2F%2Fwww.example.com%2Fauth%2Flinkedin&state=987654321&scope=r_basicprofile");
$response = json_decode($response);
var_dump($response);

answer开始,但我的回复是: NULL

修改

我的oAuth2.0终点授权是http://localhost,我不知道这是否有问题或者是否可能。

我也放了curl标签,因为非常欢迎为此目的进行教学。

对不起我的英文

2 个答案:

答案 0 :(得分:0)

嗯,我不知道这是否正确,但我得到了以下内容:

发出请求:

<?php
header('Location: https:/www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=xxxxxxx&redirect_uri=http%3A%2F%2Flocalhost%2FapiLinkedin%2FDevMagicHat%2Fauth%2Flinkedin/callback&state=xxxxxxxxx');
?>

这是我的回电,请求回复后请求Linkdin回答。

因此,在文件夹callback中,我创建了一个index.php页面来接收回复:

<强>本地主机/ apiLinkedin / DevMagicHat / AUTH / LinkedIn /回调/ index.php的

<?php
$code = $_GET['code'];
$state = $_GET['state'];
echo "Code =>".$code."<br/>"."State =>".$state;
?>

我真的不知道这种方式是否比其他人更好,但对我有用,我希望能为你工作。

答案 1 :(得分:0)

首先你必须在Linkedin平台上create an application并获得你的client_id

我们承认您的重定向网址为:http://localhost/index.php,您应该做的第一件事就是将用户的浏览器定向到LinkedIn:

<强>的index.php

$params = [
    'response_type' => 'code',
    'client_id' => $client_id,
    'redirect_uri'  => 'http://localhost/index.php', 
    'state' => 'textOfYourChoice'
];

$url = 'https://www.linkedin.com/oauth/v2/authorization?'.http_build_query($params);

header('Location: '.$url);

您现在将获得linkedin授权页面,只需单击“允许”按钮,您将直接返回到您的页面index.php,其中包含在url参数中传递的代码。

现在为了获取代码值,你应该检查$ _GET变量:

index.php

if (isset($_GET['code']) {
   die($_GET['code']);
}

$params = [
    'response_type' => 'code',
    'client_id' => $client_id,
    'redirect_uri'  => 'http://localhost/index.php', 
    'state' => 'textOfYourChoice'
];

$url = 'https://www.linkedin.com/oauth/v2/authorization?'.http_build_query($params);

header('Location: '.$url);
相关问题