2

I am using this code and getting an error:

The remote server returned an error: (401) Unauthorized.

As per their docs, I did set the headers.

https://developer.linkedin.com/docs/share-on-linkedin#

What am I missing? Any help will be appreciated. Thanks!

JToken accessCode = myAccessToken;
string requestUrl = "https://api.linkedin.com/v1/people/~/shares?oauth2_access_token=" + 
                    accessCode;

WebRequest request = WebRequest.Create(requestUrl);
request.Method = "POST";
request.ContentType = "application/json";
request.Headers.Add("x-li-format", "json");

using (var stream = new StreamWriter(request.GetRequestStream()))
{
    var shareMsg = new
    {
        comment = "comment"
        content = new
        {
            title = "title",
            submitted_url = "url,
            submitted_image_url = "image_url",
            description = string.Empty
        },
        visibility = new { code = "anyone" }
    };

    string json = JsonConvert.SerializeObject(shareMsg);
    stream.Write(json);
    stream.Flush();
    stream.Close();
}

WebResponse webResponse = request.GetResponse();
Stream dataStream = webResponse.GetResponseStream();
var reader = new StreamReader(dataStream);
string response = reader.ReadToEnd();
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • Maybe `myAccessToken` is invalid. – Uwe Keim Jan 06 '17 at 22:31
  • 1
    I am able to login with Linked In, to retrieve user's profile, but later on, inside the app, the test button is pushed and I get that 401 error. I can confirm that access token is the same as the one from login. – Stefanica Stefan Jan 06 '17 at 23:06
  • [The docs say](https://developer.linkedin.com/docs/share-on-linkedin) _"…Whether set as a default permission in your app settings or requested specifically via the scope argument during your authentication process, you will need to request the `w_share` member permission in order for your application to successfully make the API call to share content…"_. Maybe this is relevant for your case? – Uwe Keim Jan 06 '17 at 23:18
  • I have `w_share` on dashboard and as a scope argument, too. I tried an approach with `OAuth2Request`, but no success, I get _Unable to verify access token_. Thanks for your suggestions. – Stefanica Stefan Jan 06 '17 at 23:49

1 Answers1

1

Logging out and then authenticate in again with Linked In solved my problem.

For people having similar issues, you may want to check these things:

  • Access token to be valid
  • Check for the right permissions (w_share in my case)
  • Check to see if you have the right headers
  • Someone reported that if you wait few minutes it will start to work
  • Log out from Linked in from your application and authenticate again (my solution)

Additional solutions: - https://stackoverflow.com/a/41569491/7312674

Hope it helps someone save some time.

Community
  • 1
  • 1
  • 1
    Grrr... In my case (https://stackoverflow.com/questions/41513876/linkedin-401-unable-to-verify-access-token) it just works for certain amount of time, then it does not, then it starts again.... no idea what and when is wrong... No rocket science anywhere, just two REST calls. I'm tempted to write something which I shouldn't :-) – Lachezar Balev Jan 07 '17 at 09:07