linkedin-j授权失败

时间:2012-10-30 11:21:26

标签: java linkedin linkedin-j

我使用linkedin-j lib并尝试授权,但总会抛出异常:

我先尝试授权。但是如果我拿到

request

authURL

抛出异常(见下文)。

这是我的代码:

private static String consumerKeyValue="[mykey]";
private static String consumerSecretValue="[mySecret]";
private static String accessTokenValue="[myTokenValue]";
private static String tokenSecretValue="[mytokenSecretValue]";

static LinkedInApiClientFactory factory;
static LinkedInApiClient client;

static LinkedInOAuthService oAuthService;
static LinkedInRequestToken request;
static String authURL;

private static LinkedInAccessToken accessToken;

public static void main(String[]args) throws IOException{

    authorize(consumerKeyValue, tokenSecretValue);
    createClient(consumerKeyValue, consumerSecretValue, accessTokenValue, tokenSecretValue);

}


public static void createClient(String key, String secret, String token, String secretToken){
factory=LinkedInApiClientFactory.newInstance(key,secret);
client=factory.createLinkedInApiClient(accessToken);
}

public static void authorize(String key, String secret) throws IOException{
    oAuthService=LinkedInOAuthServiceFactory.getInstance().createLinkedInOAuthService(key, secret);
    request=oAuthService.getOAuthRequestToken();
    authURL=request.getAuthorizationUrl();
    System.out.println("Paste this URL to your browser:");
    System.out.println(authURL);
    System.out.println("and now paste the given PIN here and press enter");
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    String input=br.readLine();
    accessToken =oAuthService.getOAuthAccessToken(request, input);
}


}

现在抛出以下异常:

Exception in thread "main" com.google.code.linkedinapi.client.oauth.LinkedInOAuthServiceException: oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: Server returned HTTP response code: 401 for URL: https://api.linkedin.com/uas/oauth/requestToken
at com.google.code.linkedinapi.client.oauth.LinkedInOAuthServiceImpl.getOAuthRequestToken(LinkedInOAuthServiceImpl.java:159)
at Connection.authorize(Connection.java:46)
at Connection.main(Connection.java:32)
Caused by: oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: Server returned HTTP response code: 401 for URL: https://api.linkedin.com/uas/oauth/requestToken
at oauth.signpost.AbstractOAuthProvider.retrieveToken(AbstractOAuthProvider.java:214)
at oauth.signpost.AbstractOAuthProvider.retrieveRequestToken(AbstractOAuthProvider.java:69)
at com.google.code.linkedinapi.client.oauth.LinkedInOAuthServiceImpl.getOAuthRequestToken(LinkedInOAuthServiceImpl.java:148)
... 2 more
Caused by: java.io.IOException: Server returned HTTP response code: 401 for URL: https://api.linkedin.com/uas/oauth/requestToken
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
at oauth.signpost.basic.HttpURLConnectionResponseAdapter.getContent(HttpURLConnectionResponseAdapter.java:18)
at oauth.signpost.AbstractOAuthProvider.handleUnexpectedResponse(AbstractOAuthProvider.java:228)
at oauth.signpost.AbstractOAuthProvider.retrieveToken(AbstractOAuthProvider.java:189)
... 4 more
Caused by: java.io.IOException: Server returned HTTP response code: 401 for URL: https://api.linkedin.com/uas/oauth/requestToken
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.HttpURLConnection.getResponseCode(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)
at oauth.signpost.basic.HttpURLConnectionResponseAdapter.getStatusCode(HttpURLConnectionResponseAdapter.java:22)
at oauth.signpost.AbstractOAuthProvider.retrieveToken(AbstractOAuthProvider.java:178)
... 4 more

0 个答案:

没有答案