user1529176
user1529176

Reputation:

JSAPI LinkedIn OAuth10a request with Java (based on a PHP version)

I am trying to access data on LinkedIn profile using its API.

At first I followed the LinkedIn JSPAI Doc on https://developer-programs.linkedin.com/documents/exchange-jsapi-tokens-rest-api-oauth-tokens in PHP. So I started translating code from PHP to Java using Scribe.

Then, I have found this example on Github which looks like what I did : https://github.com/fernandezpablo85/TokenExchangeSample/blob/master/src/main/java/com/linkedin/oauth/ExchangeService.java

and I got this string in the end after authorization and cookie exchange :

oauth_token=75--4ff2c506-37e2-4b77-927f-c28c5f511762&oauth_token_secret=c73110b2-0dce-43bd-8537-8c8fb4fd5290&oauth_expires_in=5183975&oauth_authorization_expires_in=5183975

In PHP, the listed code help to get user data as described in the $url :

// go to town, fetch the user's profile
$url = 'http://api.linkedin.com/v1/people/~:(id,first-name,last-name,headline)';
$oauth->fetch($url, array(), OAUTH_HTTP_METHOD_GET, array('x-li-format' => 'json')); // JSON!
$profile = json_decode($oauth->getLastResponse());
print "$profile->firstName $profile->lastName is $profile->headline.";

So the code works and returns data. In the Java version, I am wondering how to use the returned tokens.

I tried used https://api.linkedin.com/v1/people/~:(id,first-name,last-name,headline)?oauth_token=75--7ff2c506-57e2-4b77-927f-c28c5f551762&oauth_token_secret=c73330b2-0dce-48bd-8537-8c8fb4fd5290&oauth_expires_in=5183975&oauth_authorization_expires_in=5183975

But it does not work.

Upvotes: 1

Views: 109

Answers (1)

user1529176
user1529176

Reputation:

I found the solution : After getting the Oauth10a keys, you should use them in a new Request by specifying the json format.

     OAuthService service = new ServiceBuilder()
                .apiKey(APIKEY)
                .apiSecret(SECRETKEY)
                .provider(LinkedInApi.class)
                .build();

    OAuthRequest oAuthRequestData = new OAuthRequest(Verb.GET, DATAENDPOINT);
    oAuthRequestData.addHeader("x-li-format", "json");
    Token accessToken = new Token(oauth_token, oauth_token_secret);
    service.signRequest(accessToken, oAuthRequestData);
    Response oAuthResponse = oAuthRequestData.send();
    System.outt.println(oAuthResponse.getBody());

Upvotes: 1

Related Questions