【问题标题】:Request an Authorization Code Linkedin索取授权码 Linkedin
【发布时间】:2017-11-09 16:09:16
【问题描述】:

我了解了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标签,因为这方面的教学很受欢迎。

对不起我的英语

【问题讨论】:

  • Tks for Attention @TahaPaksu ,但是在这个例子中 retun 404,可能是 url 请求上的一些错误,我检查了很多次 url 参数,听起来不错,但不起作用...
  • 欢迎否定,当遵循任何提示以更好地提问时,它会带来什么好处?

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


【解决方案1】:

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

提出请求:

<?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 回复回电 uri 后。

所以在文件夹callback 我创建了一个index.php 页面来接收响应:

localhost/apiLinkedin/DevMagicHat/auth/linkedin/callback/index.php

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

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

【讨论】:

    【解决方案2】:

    首先你必须在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授权页面,只需单击允许按钮,您将使用url参数中传递的代码直接返回您的页面index.php。

    现在为了获取代码值,您应该检查 $_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);
    

    【讨论】:

    • 我在使用 curl 之前尝试过类似的操作,但返回 string 0...]
    • @MagicHat,好的,让我检查一下,我之前没试过,也许我忘记了什么
    • 哦,伙计,没问题,我真的很想用 curl 来学习... tks for attention...我等待 ;)
    • 这里不用curl了,再看我的回答
    • 对不起,我的诚实,但你的答案和我的一样,我说 curl,因为我的目的下一步是 POST 和不能 POSTheaderfunction...我感谢您的帮助,但这样的回答在 SO 上是不受欢迎的......真的很抱歉......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-07
    • 2016-01-08
    • 2015-07-24
    • 2020-09-20
    • 1970-01-01
    • 2014-03-30
    • 2015-12-02
    相关资源
    最近更新 更多