scribe 1.3对linkedin API的请求返回401 - 未经授权

时间:2012-07-07 11:00:11

标签: java linkedin scribe

我试图通过我的网络应用程序向我的linkedin发送共享内容,但看起来我做错了。

这是我的java代码:

String payload = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
    "<share>" +
        "<comment>testing</comment>" +
        "<visibility><code>anyone</code></visibility>" +
        "</share>";

String PROTECTED_RESOURCE_URL = "http://api.linkedin.com/v1/people/~/shares";
        OAuthService service = new ServiceBuilder()
        .provider(LinkedInApi.class)
        .apiKey("XXXXXXXXX")
        .apiSecret("XXXXXXXXX")
        .debug()
        .build();
Scanner in = new Scanner(System.in);
Token requestToken = service.getRequestToken();

// here is for enter the confirmation code while i'm use ooB, use callback in future
System.out.println("Now go and authorize Scribe here:");
System.out.println(service.getAuthorizationUrl(requestToken));
System.out.println("And paste the verifier here");
System.out.print(">>");
Verifier verifier = new Verifier(in.nextLine());

Token accessToken = service.getAccessToken(requestToken, verifier);
OAuthRequest request = new OAuthRequest(Verb.POST, PROTECTED_RESOURCE_URL);
request.addPayload(payload);
service.signRequest(accessToken, request);
Response response = request.send();

所有看起来都在调试控制台中工作,但是当我尝试打印响应体时,我得到了这个:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<error>
  <status>401</status>
  <timestamp>1341657286653</timestamp>
  <request-id>4STP3FUGSG</request-id>
  <error-code>0</error-code>
  <message>[unauthorized]. OAU:XXXXXXX|3619482b-ea17-4382-8008-5b108e90c4a2|*01|*01:1341657286:wp7SeE6fAPIj5cSKfd3t+lZfwpE=</message>
</error>

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:4)

谢谢Pablo, 在这种情况下,调试模式没有帮助我,但通过一些搜索我发现了问题:

为像这样的帖子请求添加标题

    request.addHeader("Content-Length", Integer.toString(payload.length()));  
    request.addHeader("Content-Type", "text/xml");