Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 向LinkedIn API发出请求将导致401_Php_Linkedin_Http Status Code 401 - Fatal编程技术网

Php 向LinkedIn API发出请求将导致401

Php 向LinkedIn API发出请求将导致401,php,linkedin,http-status-code-401,Php,Linkedin,Http Status Code 401,我正在尝试使用PHP为LinkedIn用户配置文件制作API。 我已经成功注册了我的应用程序,我注意到了我的API和密钥,并列出了我的重定向url 用户从这个页面开始:index.php。此页面包含指向linkedIn对话框的链接: <a href="https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=<?php echo $api_key ?>&state

我正在尝试使用
PHP
为LinkedIn用户配置文件制作
API
。 我已经成功注册了我的应用程序,我注意到了我的API和密钥,并列出了我的重定向url

用户从这个页面开始:
index.php
。此页面包含指向linkedIn对话框的链接:

<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>
但是,上述代码会导致此输出:

"401 Unknown authentication scheme"

在做了一点研究之后,我想可能是因为我现在还没有获得访问令牌?有人知道我应该怎么做才能解决这个问题吗?

对于正在阅读本文并希望使用
LinkedIn配置文件API的人来说,我的问题的解决方案是,当我尝试发出第一个请求时,我没有有效的
访问令牌

我做的第一件事是创建一个链接,将用户指向
LinkedIn
authentication对话框

接下来,用户将选择批准或拒绝我的应用程序请求以访问其配置文件。无论他们的选择如何,他们都会被重定向到我的
重定向url

从这里我现在有了一个
访问代码
,我可以使用它来请求
访问令牌
,从而进行api调用

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()
方法

然后是同样来自LinkedIn API的
fetch()
方法

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
发出请求。公平地说,世邦魏理仕发表了上述评论。我错过了这个信息。如果他/她想留下一个答案,我很乐意接受,但以防万一,我已经为遇到我的问题的任何人提供了我的解决方案

我不知道您是否已经获得了访问令牌–但您肯定没有在cURL请求中使用它。@CBroe我确实有一个
access\u令牌
,我可以在url
code=MY\u access\u令牌
中看到它。我想我不确定如何在我的cURL请求中使用它。他们的文档中有很多PHP代码示例,所以我建议你去那里看看。我一直在那里看,但他们提供的示例代码似乎是通过交换授权代码来获取令牌的。我试图通过将用户重定向到LinkedIn的授权对话框来生成授权代码,他们没有提供我所看到的示例…示例中的函数getAuthorizationCode正是这样做的–构建登录URL并将用户重定向到那里…
 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;
}
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);
}