Hello everyone,
I have a problem with Xamarin.Auth, Azure Mobile Services and LinkedIn login that I haven't been able to solve for a couple of days now.
First of all I have the Azure Mobile Services running with .NET backend. I followed the following blog post (http://azure.microsoft.com/blog/2014/07/28/azure-mobile-services-net-updates/) and added the LinkedInLoginProvider by following the "Extensible Authentication Model" section of that blog post. When I try to log in to the service using the browser, everything seems to be working fine.
Next, in my Xamarin.Forms project I added the component Xamarin.Auth and added the following renderer for Android:
public class LinkedInLoginPageRenderer : PageRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs e)
{
base.OnElementChanged(e);
var activity = this.Context as Activity;
var auth = new OAuth2Authenticator(
LinkedInLogin.Client_Id,
LinkedInLogin.Client_Secret,
LinkedInLogin.Scope,
new Uri(LinkedInLogin.Authorize_Url),
new Uri(LinkedInLogin.Redirect_Url),
new Uri(LinkedInLogin.Access_Token_Url)
);
auth.AllowCancel = true;
auth.Completed += (sender, eventArgs) =>
{
if (eventArgs.IsAuthenticated)
{
string dd = eventArgs.Account.Username;
var values = eventArgs.Account.Properties;
var access_token = values["access_token"];
try
{
var request = HttpWebRequest.Create(string.Format(@"https://api.linkedin.com/v1/people/~
" +
LinkedInLogin.Requested_Profile_Fields +
")?oauth2_access_token=" + access_token + "&format=json", ""));
request.ContentType = "application/json";
request.Method = "GET";
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
System.Console.Out.WriteLine("Status Code is: {0}", response.StatusCode);
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
var content = reader.ReadToEnd();
if (!string.IsNullOrWhiteSpace(content))
{
System.Console.Out.WriteLine(content);
}
var result = JsonConvert.DeserializeObject<User>(content);
GlobalData.Instance.LoggedInUser = next50.Models.LinkedIn.User.ConvertToNormalUser(result);
}
}
//store in Android KeyStore
AccountStore.Create(activity).Save(eventArgs.Account, "LinkedIn");
App.SuccessfulLoginAction.Invoke();
}
catch (Exception exx)
{
System.Console.WriteLine(exx.ToString());
}
}
else
{
// The user cancelled
}
};
activity.StartActivity(auth.GetUI(activity));
}
}
Now, I'm able to log in when a login dialog pops up on my phone and I'm able to obtain the access token and obtain all the information I need from LinkedIn, but when I try to authenticate the user using the access token on the Mobile Services I keep getting the Method Not Allowed exception. Here is how I'm trying to authenticate:
public async Task Authenticate()
{
try
{
var token = new JObject();
token.Add("access_token", LoginStatus.GetAccessToken());
user = await azClient.LoginAsync("LinkedIn", token);
Debug.WriteLine(string.Format("you are now logged in - {0}", user.UserId), "Logged in!");
}
catch (Exception ex)
{
Debug.WriteLine("Authentication failed " + ex.ToString());
}
}
Here's the error message I receive back:
Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException: The request could not be completed. (Method Not Allowed)\n at Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient+d__18.MoveNext () [0x0022f] in :0 \n--- End of stack trace from previous location where exception was thrown ---\n ....
I'd really appreciate some help if anybody has any idea what i'm doing wrong? I'm pretty new with both Xamarin and Azure Mobile Services. And one last thing to point out, the service I'm targeting is published on Azure, so it's not local, and I'm also using my phone for debugging, not an emulator.
Thank you!