linkedin-j & Access to posting shares denied

<1 min read

This morning I noticed that my blog wasn't able to share on Linkedin anymore. I use the linkedin-j library to automatically share new blog posts on Linkedin. I rightfully assumed that my OAuth access token had expired. Upon resetting the token, I got the following error:

Access to posting shares denied

After a bit of research I was able to ascertain that the linkedin-j library was not requesting write access permission, which apparently is now required.

If was able to fix the problem by adding the following code right before the token request:

final Field field = LinkedInApiUrls.class.getField("LINKED_IN_OAUTH_REQUEST_TOKEN_URL");
final Field modifiersField = java.lang.reflect.Field.class.getDeclaredField("modifiers");
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, LinkedInApiUrls.LINKED_IN_OAUTH_REQUEST_TOKEN_URL + "?scope=rw_nus+r_basicprofile");

You gotta love the reflection API.

I can't really take the credit for the solution, I found it on stackoverflow.