Coder Social home page Coder Social logo

facebook-login-xamarin-forms's Introduction

Facebook-Login-Xamarin-Forms

Demo for logging in to Facebook API

Screen shots for Android application:

Screen shots for iOS application:

Screen shots for UWP application:

Watch a video demo: https://youtu.be/xGW9Q4atJNc

facebook-login-xamarin-forms's People

Contributors

houssemdellai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

facebook-login-xamarin-forms's Issues

Can't login with other facebook account.

Title - You login with facebook only where you generated client id. But i want to login with different facebook account. How is possible?Can you please tell us how to do that.Is it possible.

Actual result - Once you generated client id from one account then can't use for another account.

Expected result - Allow to any kind of other facebook account.

Get email id

Hi Houssem, thanks for this great library. Just a wee question: how do I get user email id?
Cheers mate.

Not Logged In Issue

I implemented everything in your video but when i press the butto says the following:
"Not Logged In. you are not logged in. please login and try again"

Below is my code :

using System;
using System.Collections.Generic;
using diabetics_app.ViewModels;
using Xamarin.Forms;

namespace diabetics_app.Main
{
    public partial class FacebookProfilePage : ContentPage
    {
		/// <summary>
		/// Make sure to get a new ClientId from:
		/// https://developers.facebook.com/apps/
		/// </summary>
		private string ClientId = "134161603894277";
       // 134161603894277
		public FacebookProfilePage()
		{
			InitializeComponent();

			var apiRequest =
				"https://www.facebook.com/v2.10/dialog/oauth?client_id="
				+ ClientId
				+ "&display=popup&response_type=token&redirect_uri=http://www.facebook.com/connect/login_success.html";

			var webView = new WebView
			{
				Source = apiRequest,
				HeightRequest = 1
			};

			webView.Navigated += WebViewOnNavigated;

			Content = webView;
		}

		private async void WebViewOnNavigated(object sender, WebNavigatedEventArgs e)
		{

			var accessToken = ExtractAccessTokenFromUrl(e.Url);

			if (accessToken != "")
			{
				var vm = BindingContext as FacebookViewModel;

				await vm.SetFacebookUserProfileAsync(accessToken);

				Content = MainStackLayout;
			}
		}

		private string ExtractAccessTokenFromUrl(string url)
		{
			if (url.Contains("access_token") && url.Contains("&expires_in="))
			{
				var at = url.Replace("https://www.facebook.com/connect/login_success.html#access_token=", "");

				if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
				{
					at = url.Replace("http://www.facebook.com/connect/login_success.html#access_token=", "");
				}

				var accessToken = at.Remove(at.IndexOf("&expires_in="));

				return accessToken;
			}

			return string.Empty;
		}
	}

}


Change User? or Logout?

Hi,
thanks for your work.

but what will happen if i want to sign in with another user?? or i want to logout?
how can this be done?
thansks

Bio field has to be removed

var userJson = await httpClient.GetStringAsync(requestUrl);

`this code is throwing an error
the error i get is

StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:\r\n{\r\nWWW-Authenticate: OAuth "Facebook Platform" "invalid_request" "(#12) bio field is deprecated for versions v2.8 and higher

We are not getting email address in facebook response.

I am sending following as a post request.
` public async void WebViewOnNavigated(object sender, WebNavigatedEventArgs e)
{

        var accessToken = ExtractAccessTokenFromUrl(e.Url);
        Console.WriteLine("Token ==>"+accessToken);
      
        
        if (accessToken != "")
        {
            try
            {
                await GetFacebookProfileAsync(accessToken);
                Console.WriteLine("Facebook ID ==>" + facebookProfile.id);
                if (string.IsNullOrEmpty(facebookProfile.email))
                {
                    Application.Current.MainPage = new NavigationPage(new Register());
                }
                else
                {
                    await FacebookLoginSubmit();  
                }
            }
            catch (Exception)
            {

            }
        }
    }

    async Task<FacebookProfileResponse> GetFacebookProfileAsync(string accessToken)
    {
        var requestUrl =
            "https://graph.facebook.com/v2.7/me/"
            + "?fields=first_name,last_name,email"
            + "&access_token=" + accessToken;

        var httpClient = new HttpClient();

        var userJson = await httpClient.GetStringAsync(requestUrl);

        facebookProfile = JsonConvert.DeserializeObject<FacebookProfileResponse>(userJson);

        return facebookProfile;
    }

    private string ExtractAccessTokenFromUrl(string url)
    {
        if (url.Contains("access_token") && url.Contains("&expires_in="))
        {
            var at = url.Replace("https://www.facebook.com/connect/login_success.html#access_token=", "");

            if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
            {
                at = url.Replace("http://www.facebook.com/connect/login_success.html#access_token=", "");
            }

            var accessToken = at.Remove(at.IndexOf("&expires_in="));

            return accessToken;
        }

        return string.Empty;
    }`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.