Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a18e451

Browse files
committed
Add support of uploading media
Returns "Not enough permissions to access media resource". I hope LinkedIn will resolve it eventually.
1 parent f212b6d commit a18e451

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

‎README.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,16 @@ Change default API root
234234
$client->setApiRoot('https://api.linkedin.com/v2/');
235235
```
236236

237+
Try to upload image to LinkedIn. See [Rich Media Shares]()https://developer.linkedin.com/docs/guide/v2/shares/rich-media-shares#upload)
238+
(returns "Not enough permissions to access media resource" for me).
239+
I assume you have to be LinkedIn partner or something like that.
240+
241+
```php
242+
$filename = '/path/to/image.jpg';
243+
$client->setApiRoot('https://api.linkedin.com/');
244+
$mp = $client->upload($filename);
245+
```
246+
237247
## Contributing
238248

239249
Please, open PR with your changes linked to an GitHub issue.

‎examples/demo.jpg‎

65 KB
Loading[フレーム]

‎examples/index.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@
105105
);
106106
pp($companyShare);
107107

108+
/*
109+
// Returns {"serviceErrorCode":100,"message":"Not enough permissions to access media resource","status":403}
110+
// You have to be whitelisted or so by LinkedIn
111+
$filename = './demo.jpg';
112+
$client->setApiRoot('https://api.linkedin.com/');
113+
$mp = $client->upload($filename);
114+
*/
108115
} catch (\LinkedIn\Exception $exception) {
109116
// in case of failure, provide with details
110117
pp($exception);

‎src/Client.php‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ public function getAccessToken($code = '')
279279
'headers' => [
280280
'Content-Type' => 'application/json',
281281
'x-li-format' => 'json',
282+
'Connection' => 'Keep-Alive'
282283
],
283284
]);
284285
try {
@@ -532,6 +533,42 @@ public function post($endpoint, array $params = [])
532533
return $this->api($endpoint, $params, Method::POST);
533534
}
534535

536+
/**
537+
* @param $path
538+
* @return array
539+
* @throws Exception
540+
*/
541+
public function upload($path)
542+
{
543+
$headers = $this->getApiHeaders();
544+
unset($headers['Content-Type']);
545+
//$headers = [];
546+
if ($this->isUsingTokenParam()) {
547+
//
548+
} else {
549+
$headers['Authorization'] = 'Bearer ' . $this->accessToken->getToken();
550+
}
551+
$guzzle = new GuzzleClient([
552+
'base_uri' => $this->getApiRoot()
553+
]);
554+
$options = [
555+
'multipart' => [
556+
[
557+
'name' => basename($path),
558+
'filename' => basename($path),
559+
'contents' => fopen($path, 'r')
560+
]
561+
],
562+
'headers' => $headers,
563+
];
564+
try {
565+
$response = $guzzle->request(Method::POST, 'media/upload', $options);
566+
} catch (RequestException $requestException) {
567+
throw Exception::fromRequestException($requestException);
568+
}
569+
return self::responseToArray($response);
570+
}
571+
535572
/**
536573
* @param array $params
537574
* @param string $method

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /