Bhabani
Bhabani

Reputation: 138

Linkedin api call using oauth in java

I am trying to fetch Linkedin company updates to my website. Followed the process https://developer.linkedin.com/documents/authentication and generated api key and secret key by registering.

According to the documentation I am able to generate the Authorization Code in Step a. For Step b to generate Access Token I am using OAuth in java.

request = OAuthClientRequest.tokenLocation("https://www.linkedin.com/uas/oauth2/accessToken")
                        .setGrantType(GrantType.AUTHORIZATION_CODE)
                        .setCode("****")
                        .setRedirectURI("https://www.example.in")
                        .setClientId("*******")
                        .setClientSecret("******").buildBodyMessage();
                    OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
                    GitHubTokenResponse oAuthResponse = oAuthClient.accessToken(request, GitHubTokenResponse.class);
                    String accessToken = oAuthResponse.getAccessToken();
                    String expiresIn = oAuthResponse.getExpiresIn();

But getting error

OAuthProblemException{description='Missing parameters: access_token', error='invalid_request', uri='null', state='null', scope='null'}
at org.apache.amber.oauth2.common.exception.OAuthProblemException.error(OAuthProblemException.java:57)
at org.apache.amber.oauth2.common.utils.OAuthUtils.handleOAuthProblemException(OAuthUtils.java:166)
at org.apache.amber.oauth2.common.utils.OAuthUtils.handleMissingParameters(OAuthUtils.java:184)
at org.apache.amber.oauth2.client.validator.OAuthClientValidator.validateRequiredParameters(OAuthClientValidator.java:90)
at org.apache.amber.oauth2.client.validator.OAuthClientValidator.validateParameters(OAuthClientValidator.java:53)
at org.apache.amber.oauth2.client.validator.OAuthClientValidator.validate(OAuthClientValidator.java:49)
at org.apache.amber.oauth2.client.response.OAuthClientResponse.validate(OAuthClientResponse.java:64)
at org.apache.amber.oauth2.client.response.OAuthClientResponse.init(OAuthClientResponse.java:59)
at org.apache.amber.oauth2.client.response.OAuthAccessTokenResponse.init(OAuthAccessTokenResponse.java:52)
at org.apache.amber.oauth2.client.response.OAuthClientResponseFactory.createCustomResponse(OAuthClientResponseFactory.java:60)
at org.apache.amber.oauth2.client.URLConnectionClient.execute(URLConnectionClient.java:105)

Please help to fix this. Thanks in advance for any help.

Upvotes: 2

Views: 5996

Answers (2)

Antonio Sanso
Antonio Sanso

Reputation: 106

I spotted the issue.

Looks like there is a problem in the documentation. Apologies.

Right call is

request = OAuthClientRequest.tokenLocation("https://www.linkedin.com/uas/oauth2/accessToken")
                    .setGrantType(ResponseType.CODE.toString())
                    .setCode("****")
                    .setRedirectURI("https://www.example.in")
                    .setClientId("*******")
                    .setClientSecret("******").buildBodyMessage();

Mind ResponseType.CODE.toString() rather than GrantType.AUTHORIZATION_CODE

Upvotes: 2

gdp
gdp

Reputation: 8232

I suggest you go through some of the linkedin developer docs here, and check out a java OAuth library rather than performing the signing process by hand. Then you can ask a specific question.

Upvotes: 1

Related Questions