Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support of LinkedIn API v2 #167

Open
anibalsanchez opened this issue Jan 23, 2019 · 13 comments
Open

Support of LinkedIn API v2 #167

anibalsanchez opened this issue Jan 23, 2019 · 13 comments

Comments

@anibalsanchez
Copy link

Q A
Bug? no
New Feature? yes
Are you using composer? yes
Version 1.0.0

Actual Behavior

Support of LinkedIn API v1.

Expected Behavior

Support of LinkedIn API v2.

... To reiterate: all developers need to migrate their apps to our newest APIs and OAuth 2.0 by March 1, 2019. To help navigate the migration process, please see the technical migration guide on the LinkedIn section of the Microsoft Docs website. Developers can expect follow-up communications via email about these changes and other future announcements about the LinkedIn Developers program.

Reference: LinkedIn v1 to v2 API Migration Frequently Asked Questions

Steps to Reproduce

Create a new App and try to use the new API v2 endpoints.

Possible Solutions

Create a new major version 2.0 of the library. It'd be great to use Happyr/LinkedIn-API-client for LinkedIn API v2.0.

Do you plan to support the new API? Or, will Happyr/LinkedIn-API-client be obsolete after March 1, 2019?

@anibalsanchez
Copy link
Author

anibalsanchez commented Jan 31, 2019

A few notes, the library can support v2 with some special provisions:

	$options = [];
	$options['headers']['X-RestLi-Protocol-Version'] = '2.0.0';

	$api->setFormat('');
	$response = $api->api('GET', '/v2/me', $options);

But, the calls and the responses are different.

@ryanjmorris
Copy link

ryanjmorris commented Feb 13, 2019

+1 To this request.

I've been able to mitigate the damage and am still using this API. Luckily, like the API states, it's easy to extend and you'll more than likely need to for posting for sure.

Here is what I've done and hope it helps someone out in the interim.

Posting content change (make sure to extend LinkedIn in whatever class you're using)
`
/**
* Overriding HappyR's LinkedIn API method to support the v2 version.
* In his method he's got v1 hard coded in there.
*/
public function isAuthenticated(): bool
{
$accessToken = $this->getAccessToken();
if ($accessToken === null) {
return false;
}

    $user = $this->linkedin->api('GET', '/v2/me');

    return !empty($user['id']);
}

/**
 * Overridden HappyR's LinkedIn API method to support the v2 version.
 * It needed to have the $options['query'] to not exist when POSTing data.
 *
 * @param string $method
 * @param string $resource
 * @param array $options
 * @return mixed|ResponseInterface|\Psr\Http\Message\StreamInterface|\SimpleXMLElement|string
 */
public function api($method, $resource, array $options = [])
{
    // Recreate the constructor to enable the HttpClient/HttpMessage
    LinkedIn::__construct(
        $this->settings['app_id'],
        $this->settings['app_secret']
    );

    // Add access token to the headers
    $options['headers']['Authorization'] = sprintf('Bearer %s', (string) $this->getAccessToken());

    // Do logic and adjustments to the options
    $requestFormat = $this->filterRequestOption($options);

    // Needs to unset this in order to send out message - otherwise this appends ?filter which doesnt work with v2 (from what I've been able to tell)
    if ($options['unset_query']) {
        unset($options['query']);
    }

    // Generate an url
    $url = $this->getUrlGenerator()->getUrl(
        'api',
        $resource,
        $options['query'] ?? []
    );

    $body = $options['body'] ?? null;
    $this->getRequestManager();
    try {
        $this->lastResponse = $this->getRequestManager()->sendRequest($method, $url, $options['headers'], $body);
    } catch (LinkedInTransferException $e) {}

    //Get the response data format
    if (isset($options['response_data_type'])) {
        $responseDataType = $options['response_data_type'];
    } else {
        $responseDataType = $this->getResponseDataType();
    }

    try {
        return ResponseConverter::convert($this->lastResponse, $requestFormat, $responseDataType);
    } catch (InvalidArgumentException $e) {
        return null;
    } catch (LinkedInTransferException $e) {
        return null;
    }
}

`

Whatever method you're posting your content from do something similar to this (for including images, you'll need to add some custom functionality as that is overhauled from the docs I've read - haven't got to that yet):

`
$options = [
'headers' => [
'X-Restli-Protocol-Version' => '2.0.0'
],
'unset_query' => true,
'json' => [
'author' => 'urn:li:person:' . $liUserId,
'lifecycleState' => 'PUBLISHED',
'specificContent' => [
'com.linkedin.ugc.ShareContent' => [
'shareCommentary' => [
'text' => $message . "\r\n" . $url
],
'shareMediaCategory' => 'NONE',
]
],
'visibility' => [
'com.linkedin.ugc.MemberNetworkVisibility' => 'PUBLIC'
]
]
];

        $this->api('POST', 'v2/ugcPosts', $options);

`

LinkedIn Signing in/out. This is basically the same as what @anibalsanchez has here.
` $options = [];
$options['headers']['X-RestLi-Protocol-Version'] = '2.0.0';

// Defining explicitly what permissions we want/need - update this if we need more than this.
$callback['scope'] = 'r_liteprofile,w_share,w_member_social';
$this->api->getLoginUrl($callback);

// To get the user information (the $userInfo used to only be $userInfo['firstName'])
$userInfo = $this->api->get('v2/me');
$userName = $userInfo['firstName']['localized']['en_US'] . ' ' . $userInfo['lastName']['localized']['en_US']; `

@jksteelman
Copy link

It is now after March 1, 2019 and this library no longer works without overriding the hard-coded v1 uri and modding the methods.

This package served me well.

@giovannialbero1992
Copy link

@anibalsanchez @ryanjmorris @jksteelman @andreascreten @irfanevrens
I've started to work to a new library that works with v2, if you would like to collaborate you are welcome. this is the link https://github.com/r-everse/linkedin-php.
Thanks

@Florianp123
Copy link

@giovannialbero1992 : thanks for your sharing. Is your code finished / working ?
have a great day
Florian

@giovannialbero1992
Copy link

@Florianp123 at the moment it works to retrieve information on users and post content.
The library is open to contributors

@Florianp123
Copy link

ok thanks :-)

@simonbuehler
Copy link

hi,
whats the way to go, using https://github.com/r-everse/linkedin-php oder will this package get a update?

@akelimad
Copy link

hello,
me also im facing the same problem, https://github.com/martin-georgiev/social-post-bundle
work as expected for facebook and twitter but for linkedIn NOTHING POSTED

@theedov
Copy link

theedov commented Aug 9, 2020

Looks like this one is dead.

@andreascreten
Copy link
Contributor

Sorry for not getting back earlier. I guess it makes sense to use https://github.com/r-everse/linkedin-php instead.

@c33s
Copy link

c33s commented Oct 23, 2021

r-everse/linkedin-php -> This repository has been archived by the owner. It is now read-only.

looks like the next choice could be https://packagist.org/packages/zoonman/linkedin-api-php-client

@ddonnini
Copy link

@c33s you may also consider this: https://github.com/SMarTStrategy/linkedin-php
it's developed by @giovannialbero1992

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests