Login with facebook on Xamarin Forms with Azure Mobile Apps

2

I'm building an app (with xamarin.forms pcl ), where I have a facebook login using the Azure Mobile Client SDK . It is possible to perform the authentication, however, soon after the authentication, I try to make a listing in the api url: link and I have the 401 Unauthorized return.

Since the login with the social network was successful. The evidence follows:

CodewhereIhavetheproblem:

publicasyncTask<List<Tipo>>GetTipoAsync(){varhttpClient=newHttpClient();httpClient.DefaultRequestHeaders.Accept.Add(newMediaTypeWithQualityHeaderValue("application/json"));

            var response = await httpClient.GetAsync($"{BaseUrl}Tipo").ConfigureAwait(false);

            if (response.IsSuccessStatusCode)
            {
                using (var responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
                {
                    return JsonConvert.DeserializeObject<List<Tipo>>(
                        await new StreamReader(responseStream).ReadToEndAsync().ConfigureAwait(false));
                }
            }

            return null;
        }

Note: Before putting the authentication with facebook, the access to api and the get of the data for listing worked correctly. The problem is that you are giving this access without authorization in the url, even after being logged in. Note 2: It is possible to test the api in the browser, with facebook authentication, and it works correctly.

    
asked by anonymous 01.06.2017 / 04:14

1 answer

3

I think you're attending the Xamarin Marathon. To get the profile data I did this:

public class AzureService
{
    List<AppServiceIdentity> identities = null;
    public MobileServiceClient Client { get; set; } = null;
    .....
}
......
identities = await Client.InvokeApiAsync<List<AppServiceIdentity>>("/.auth/me");
var name = identities[0].UserClaims.Find(c => 
c.Type.Equals("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname")).Value; 

var userToken = identities[0].AccessToken; 

var requestUrl = $"https://graph.facebook.com/v2.9/me/?fields=picture&access_token={userToken}";

var httpClient = new HttpClient();

var userJson = await httpClient.GetStringAsync(requestUrl);

var facebookProfile = JsonConvert.DeserializeObject<FacebookProfile>(userJson)
......

Source:

    
01.06.2017 / 15:47