当我尝试调用URL GET:/ v2 / me时,收到错误消息“资源不存在”。
获取:https://api.linkedin.com/v2/me?projection=(id,firstName,lastName,profilePicture)
收到来自https://www.linkedin.com/oauth/v2/accessToken
的令牌授权网址:
$url = 'https://www.linkedin.com/oauth/v2/authorization'
.'?response_type=code'
.'&client_id='."77uuvjhz11mi71"
.'&redirect_uri='."http://localhost/linkedin"
.'&state=123123123'
.'&scope=r_liteprofile%20r_emailaddress%20w_member_social';
请求:
...
$linkedin->getAccessToken($_GET['code']);
...
$this->setMethod("GET");
$this->setUrl("https://api.linkedin.com/v2/me?projection=(id,firstName,lastName,profilePicture)");
$this->setHeaders([
"Authorization: Bearer ".$this->accessToken,
]);
$resp = $this->sendRequest();
响应:
{#81 ▼
+"serviceErrorCode": 0
+"message": "Resource me does not exist"
+"status": 404
}
API函数:
public function __construct()
{
$this->ch = curl_init();
}
public function sendRequest()
{
curl_setopt_array($this->ch, $this->options);
$result = curl_exec($this->ch);
if (curl_errno($this->ch)) {
throw new Exception("Error ". curl_error($this->ch), 1);
}
$json = json_decode($result);
return $json;
}
public function setMethod(String $method)
{
$method = strtoupper($method);
if ($method === "POST") {
$this->options[CURLOPT_POST] = true;
}
return $this;
}
public function setUrl(String $url)
{
$this->options[CURLOPT_URL] = $url;
return $this;
}
public function setHeaders(Iterable $headers)
{
$this->options[CURLOPT_HTTPHEADER] = $headers;
return $this;
}
public function getAccessToken($code)
{
$this->setMethod("POST");
$this->setUrl("https://www.linkedin.com/oauth/v2/accessToken");
$this->setPostFields([
"grant_type" => "authorization_code",
"code" => $code,
"redirect_uri" => "http://localhost/test",
"client_id" => $this->clientId,
"client_secret" => $this->clientSecret,
]);
$resp = $this->sendRequest();
$this->accessToken = $resp->access_token;
}