Login with facebook in Xamarin Forms with Azure Mobile Apps

Asked

Viewed 227 times

2

I’m building an app (with Xamarin.Forms Pcl), where I have a login with facebook, using the Azure Mobile Client SDK. It is possible to perform the authentication, however, right after the authentication, I try to make a listing in the api url: https://adoteumamigo.azurewebsites.net/api/Tipo and have Unauthorized 401 Return.

Being that the login with the social network was carried out successfully. Follows the evidence: inserir a descrição da imagem aqui

Code where I have the problem:

public async Task<List<Tipo>> GetTipoAsync()
        {
            var httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("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;
        }

Observing: Before placing authentication with facebook, accessing the api and getting the data for listing worked properly. The problem is because you are giving this access without authorization in the url, even after being logged in. Observation 2: You can test the api in the browser, with facebook authentication, and it works correctly.

  • 1

    Is using the class MobileServiceClient for authentication on Facebook?

  • @rubStackOverflow I am. Authentication works, soon on facebook, Mobileservicecliente returns me sid and token. Now in the morning I searched, and I found something that may be the following: This token who is returning me is Azure, and I must get the token from facebook and at the time of listing the api, send the token from facebook... but I haven’t tested it yet. Makes sense?

  • Yeah, that’s right, that’s right.

1 answer

3


It seems to me that you are participating in the Xamarin Marathon. To get the profile data I did as follows:

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: https://github.com/rubgithub/JogoDaVelhaMaratonaXamarin/blob/master/JogoDaVelhaMaratona/JogoDaVelhaMaratona/Service/AzureService.cs

  • I followed your lead, but I haven’t figured it out yet. When trying to use httpCliente for a getAsync on my https://adoteumamigo.azurewebsites.net/api/Tipo after logging in with facebook, I keep receiving the 401 Unauthorized.

  • and yes, I am doing the marathon yes, and following the steps of the videos, however when logging in with facebook, and trying to use an api (q this hosted on Azure) to list items I am having this problem.

  • You are using the token obtained here? identities = await Client.InvokeApiAsync<List<AppServiceIdentity>>("/.auth/me");

  • yes, I am. And here comes another question, I can do something wrong at the time q I do getAsync here var Response = await httpClient.Getasync($"{Baseurl}Type"). Configureawait(false);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.