Question: How do I get the facebook oath page to load in Xamarin Forms WebView

Question

How do I get the facebook oath page to load in Xamarin Forms WebView

Answers 1
Added at 2016-12-27 23:12
Tags
Question

I have the following Xamarin Forms view and I'm trying to load a web view for doing Facebook authentication.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace TestApp
{
    public partial class FacebookLoginView : ContentView
    {
        private string _fbAppId = "MY APP ID";
        private string _appName = "APP NAME";

        public TechPactLoginView()
        {
            InitializeComponent();
        }

        void Facebook_Login_Clicked(object sender, System.EventArgs e)
        {

            var apiRequest =
                "https://www.facebook.com/dialog/oauth?client_id="
                + _fbAppId
                + "&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 != "")
            {
                await GetFacebookProfileAsync(accessToken);

            }
        }

        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;
        }

        public async Task GetFacebookProfileAsync(string accessToken)
        {
            var requestUrl = "https://graph.facebook.com/v2.7/me"
                + "?fields=name,picture,cover,age_range,devices,email,gender,is_verified"
                + "&access_token=" + accessToken;

            var httpClient = new HttpClient();
            var userJson = await httpClient.GetStringAsync(requestUrl);
            Debug.WriteLine("profile json : {0}", userJson);
        }
    }
}

I've added the following to my plist file in the iOS project

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSExceptionDomains</key>
        <dict>
            <key>facebook.com</key>
            <dict>
                <!--Include to allow subdomains-->
                <key>NSIncludesSubdomains</key>
                <true/>
                <!--Include to allow HTTP requests-->
                <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <!--Include to specify minimum TLS version-->
                <key>NSTemporaryExceptionMinimumTLSVersion</key>
                <string>TLSv1.1</string>
            </dict>
        </dict>
    </dict>

When the web view loads I still get an error saying that I need to add the apps domains.

enter image description here

Answers
nr: #1 dodano: 2016-12-28 00:12

Well I figured out that I had not provided the Facebook app a call or success url and that's why it was failing.

enter image description here

Source Show
◀ Wstecz