Domanda

I'm trying to send a share content to my linkedin from my web application, but it looks like I'm doing something wrong.

This is my java code:

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

All looked like it worked in the debug console but when I try to print response body I get this:

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

How can I fix this?

È stato utile?

Soluzione

Thanks Pablo, the debug mode do no help me in this case, but with some search i find the issue :

add header for Post requests like this

    request.addHeader("Content-Length", Integer.toString(payload.length()));  
    request.addHeader("Content-Type", "text/xml"); 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top