使用Java从Accesstoken获取来自Linkedin的数据

时间:2015-12-13 03:44:40

标签: javascript java oauth linkedin scribe

我已经从linkedin OAuth popup中获得了身份验证令牌(我很困惑,它是访问令牌或授权码)。以下是获取身份验证令牌的代码

 <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>

现在我正在使用该令牌来获取数据。我正在使用scribejava api https://github.com/scribejava/scribejava

使用该错误时遇到了错误。

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)

以下是代码。

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();
        }
    }
}

0 个答案:

没有答案