I am currently developing an app that must include the functionality to be able to send a message to a user on LinkedIn. I am creating this app in Xamarin.Forms and am using Xamarin.Auth in order to authenticate my app against LinkedIn and perform the following requests.
The basic workflow is as follows:
The user will connect their account to LinkedIn and be asked to log in to their account. This is done using Xamarin.Auth's OAuth2Authenticator method and works perfectly.
As soon as the user is authenticated, I then contact the LinkedIn API again using Xamarin.Auths OAuth2Request to get the users list of connections and store them under a contacts heading the app. Again, this works fine.
Now the user is back in the app, they can choose to click on one of their contacts and send a message to them on LinkedIn, prompting them to type in the subject and body of the message. This is where the problem arises.
I am using the same OAuth2Request method when posting to the LinkedIn message API as I do when getting the list of connections of the user but this time I always get the response 'Bad Request'. I don't receive any error codes, just the status description of 'Bad Request'. Below is the code I am using to carry out this post:
var account = AccountStore.Create(activity).FindAccountsForService("LinkedIn").FirstOrDefault();
var xmlDoc = "<?xml version='1.0' encoding='UTF-8'?><mailbox-item><recipients><recipient><person path='/people/~' /></recipient></recipients><subject>" + subject + "</subject><body>" + comment + "</body></mailbox-item>";
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("oauth2_access_token", account.Properties["access_token"]);
parameters.Add("text", xmlDoc);
var commentRequest = new OAuth2Request("POST", new Uri("https://api.linkedin.com/v1/people/~/mailbox"), parameters, account);
commentRequest.AccessTokenParameterName = "oauth2_access_token";
commentRequest.GetResponseAsync().ContinueWith(t =>
{
if (!t.IsFaulted)
{
string jsonResult = t.Result.GetResponseText();
}
});
I can confirm that the account line is definitely pulling back the correct account to send along with the request, the access token is the same access token as I receive from the OAuth authentication and hasn't expired. The 'subject' and 'comment' variables are passed to the above method from the screen beforehand where the user fills in these fields.
I have tried this exact same request using the apigee console and it works perfectly fine, sending the message to my account. For testing purposes I am just using ~ as the id to send the message to myself.
Has anyone had any experience with this response in the past as I can't see what I am missing? I'm sure it's just something silly though.
I should also state that this error is happening on Android and I am yet to test it on iOS. However, as the previous OAuth2Request to get the users connections is working, I assume that I have the correct permissions set up in the Android Manifest. I checked and also request the correct permission from LinkedIn which I believe is 'w_message'.
Thanks for your time.