向LinkedIn API发出请求的结果是401 [英] Making request to LinkedIn API results in 401

查看:80
本文介绍了向LinkedIn API发出请求的结果是401的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP为àLinkedIn用户个人资料创建一个API. 我已经成功注册了我的应用程序,并记下了我的API和密钥,并列出了我的重定向URL.

I am trying to make an API to à LinkedIn users profile using PHP. I've successfully registered my application and I've noted my API and Secret Key as well as listing my redirect url.

用户从以下页面开始:index.php.此页面包含链接到对话框的链接:

The user starts on this page: index.php. This page contains a link to the linkedIn dialog box:

<a href="https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=<?php echo $api_key ?>&state=<?php echo $state ?>&redirect_uri=<?php echo $redirect_uri ?>">Apply Now</a>

单击此链接后,我将使用我的详细信息登录LinkedIn,然后成功将我重定向到application_form.php.我现在想从这里获取用户个人资料详细信息:

When I click on this link I sign in to LinkedIn using my details and I am successfully redirected to application_form.php. From here I would now like to get the users profile details:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.linkedin.com/v1/people/~");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
var_dump($output);

但是上面的代码导致此结果输出:

However the above code results in this being output:

"401 Unknown authentication scheme"

经过一些研究后,我认为这可能是因为我现在还没有获得访问令牌吗?有人会知道我应该怎么做才能解决此问题吗?

After doing a bit of research I think it might be because I still have not acquired an access token at this point? Would anyone have any idea what I should do in order to fix this?

推荐答案

对于正在阅读此书并想使用LinkedIn Profile API的任何人,解决我的问题的方法是当我没有有效的Access Token时尝试发出第一个请求.

For anyone who is reading this and would like to use the LinkedIn Profile API the solution to my problem was that I did not have a valid Access Token when I attempted to make the first request.

我做的第一件事是创建一个链接,该链接会将用户定向到LinkedIn身份验证对话框.

The first thing I did was create a link that would direct the user to LinkedIn authentication dialog box.

接下来,用户将选择批准或拒绝我的应用程序访问其个人资料的请求.无论他们选择什么,他们都将重定向到我的redirect url.

Next the user would then choose to approve or reject my applications request to access their profile. Regardless of their choice they are redirected to my redirect url.

从这里我现在有一个access code,我可以使用它来请求access token,从而进行api调用.

From here I now have an access code which I can use to request an access token and thus make api calls.

if (isset($_GET['error'])) {
    echo $_GET['error'] . ': ' . $_GET['error_description'];
} elseif (isset($_GET['code'])) {
    getAccessToken();
    //$user = fetch('GET', '/v1/people/~:(firstName,lastName)');//get name
    //$user = fetch('GET', '/v1/people/~:(phone-numbers)');//get phone numbers
    $user = fetch('GET', '/v1/people/~:(location:(name))');//get country
    var_dump($user);
}

我基于LinkedIn开发人员网站上的代码使用的getAccessToken()方法

The getAccessToken() method that I used based on the code on the LinkedIn Developers site

https://developer.linkedin.com/documents/code-samples

 function getAccessToken() {
    $params = array(
        'grant_type' => 'authorization_code',
        'client_id' => 'MY API KEY',
        'client_secret' => 'MY SECRET KEY',
        'code' => $_GET['code'],
        'redirect_uri' => 'MY REDIRECT URL',
    );
    // Access Token request
    $url = 'https://www.linkedin.com/uas/oauth2/accessToken?' . http_build_query($params);
    // Tell streams to make a POST request
    $context = stream_context_create(
            array('http' =>
                array('method' => 'POST',
                )
            )
    );
    // Retrieve access token information
    $response = file_get_contents($url, false, $context);
    // Native PHP object, please
    $token = json_decode($response);
    // Store access token and expiration time
    $_SESSION['access_token'] = $token->access_token; // guard this! 
    $_SESSION['expires_in'] = $token->expires_in; // relative time (in seconds)
    $_SESSION['expires_at'] = time() + $_SESSION['expires_in']; // absolute time
    return true;
}

然后使用fetch()方法(同样来自LinkedIn API)

Then the fetch() method, also from LinkedIn API

function fetch($method, $resource, $body = '') {
    $opts = array(
        'http' => array(
            'method' => $method,
            'header' => "Authorization: Bearer " . 
            $_SESSION['access_token'] . "\r\n" . 
            "x-li-format: json\r\n"
        )
    );
    $url = 'https://api.linkedin.com' . $resource;
    if (count($params)) {
        $url .= '?' . http_build_query($params);
    }
    $context = stream_context_create($opts);
    $response = file_get_contents($url, false, $context);
    return json_decode($response);
}

通过上述操作,我对向API发出请求没有问题.公平地对上面评论过的克布罗(Cbroe)表示感谢.我缺少此信息.如果他/她想留下一个答案,我会很乐意接受,但以防万一我将我的解决方案提供给遇到我这个问题的任何人.

By doing the above, I had no problem making requests to the API. In fairness to Cbroe who commented above. I was missing this information. If he/she would like to leave an answer I'll gladly accept but just incase I've included my solution for anyone who runs into the issue I had.

这篇关于向LinkedIn API发出请求的结果是401的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆