0

I already have auth token (I am confused that it is access token or authorization code) from linkedin OAuth popup. Here is the code to get the auth token

 <script type="text/javascript" src="//platform.linkedin.com/in.js">
        api_key:77ok0qpcsnr6z9
     </script>
    <script type="text/javascript">
        /*global IN*/
        IN.Event.on(IN, "systemReady", sysRed);
        function callbackFunction() {
            debugger;
            alert("oAuth Token: " +IN.ENV.auth.oauth_token);
        }
        function sysRed(){
            alert('system is ready');
            IN.User.authorize(callbackFunction);
        }
    </script>

Now I am using that token to grab the data. I am using scribejava api for the same https://github.com/scribejava/scribejava

And I ran into error when using that error.

Trading the Request Token for an Access Token...
Exception in thread "main" com.github.scribejava.core.exceptions.OAuthException: Cannot extract an access token. Response was: {"error_description":"missing required parameters, includes an invalid parameter value, parameter more than once. : Unable to retrieve access token : authorization code not found","error":"invalid_request"}
    at com.github.scribejava.core.extractors.JsonTokenExtractor.extractAccessToken(JsonTokenExtractor.java:24)
    at com.github.scribejava.core.extractors.JsonTokenExtractor.extract(JsonTokenExtractor.java:15)
    at com.github.scribejava.core.oauth.OAuth20ServiceImpl.getAccessToken(OAuth20ServiceImpl.java:37)
    at com.linkedin.data.GetLinkedInData.main(GetLinkedInData.java:50)

Below is the codes.

package com.linkedin.data;

import java.util.Scanner;

import com.github.scribejava.apis.LinkedInApi20;
import com.github.scribejava.core.builder.ServiceBuilder;
import com.github.scribejava.core.model.OAuthRequest;
import com.github.scribejava.core.model.Response;
import com.github.scribejava.core.model.Token;
import com.github.scribejava.core.model.Verb;
import com.github.scribejava.core.model.Verifier;
import com.github.scribejava.core.oauth.OAuth20ServiceImpl;

public class GetLinkedInData {

    private static final String NETWORK_NAME = "LinkedIn";
    private static final String PROTECTED_RESOURCE_URL = "https://api.linkedin.com/v1/people/~:(%s)";
    private static final Token EMPTY_TOKEN = null;

    public static void main(String[] args) {
        // Replace these with your client id and secret
        final String clientId = "secretclinetid";
        final String clientSecret = "secretcodefromapi";
        final OAuth20ServiceImpl service = (OAuth20ServiceImpl) new ServiceBuilder()
                .provider(LinkedInApi20.class).apiKey(clientId)
                .apiSecret(clientSecret)
                .scope("r_fullprofile,r_emailaddress,r_contactinfo")
                // replace with desired scope
                .callback("http://localhost:8888/linkednOauth.html").state("some_params")
                .build();
        Scanner in = new Scanner(System.in);

        System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
        System.out.println();

        // Obtain the Authorization URL
        System.out.println("Fetching the Authorization URL...");
        final String authorizationUrl = service
                .getAuthorizationUrl(EMPTY_TOKEN);
        System.out.println("Got the Authorization URL!");
        System.out.println("Now go and authorize ScribeJava here:");
        System.out.println(authorizationUrl);
        System.out.println("And paste the authorization code here");
        System.out.print(">>");
        Verifier verifier = new Verifier(oAuthcodefroLinkedinPopup);
        System.out.println();

        // Trade the Request Token and Verfier for the Access Token
        System.out.println("Trading the Request Token for an Access Token...");
        Token accessToken = service.getAccessToken(EMPTY_TOKEN, verifier);
        System.out.println("Got the Access Token!");
        System.out.println("(if your curious it looks like this: "
                + accessToken + " )");
        System.out.println();

        // Now let's go and ask for a protected resource!
        System.out.println("Now we're going to access a protected resource...");
        while (true) {
            System.out.println("Paste profile query for fetch");
            System.out.print(">>");
            final String query = in.nextLine();
            System.out.println();

            final OAuthRequest request = new OAuthRequest(Verb.GET,
                    String.format(PROTECTED_RESOURCE_URL, query), service);
            request.addHeader("x-li-format", "json");
            request.addHeader("Accept-Language", "ru-RU");
            service.signRequest(accessToken, request);
            final Response response = request.send();
            System.out.println();
            System.out.println(response.getCode());
            System.out.println(response.getBody());

            System.out.println();
        }
    }
}
Suresh Atta
  • 114,879
  • 36
  • 179
  • 284

0 Answers0