0
次のXamarinフォームビューがあり、Facebook認証を行うためのWebビューを読み込もうとしています。Xamarin Forms WebViewでfacebook宣誓書ページをロードするにはどうすればいいですか
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);
}
}
}
私はまだ取得したWebビューのロードでエラーが私はAppsドメインを追加する必要があると言ったときのiOSプロジェクト
<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>
の私のplistファイルに以下を追加しました。