我为我的高级项目为当地食品卡车创建了一个跨平台的移动应用程序。我特别想使用Parse用于它的User类,因为会有多个角色。我在使用Parse实现Facebook身份验证方面遇到了麻烦。出于某种原因,我一直收到一个错误,我无法访问JsonValue。谁能给我一些指导? 这是我的git存储库的链接,如果这会有所帮助。 https://github.com/mkmcgreal/FoodTruck.git
using System;
using System.Collections.Generic;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Parse;
using System.Json;
using System.Threading.Tasks;
using Xamarin.Auth;
namespace FoodTruck.Droid
{
[Activity (Label = "Food Truck", MainLauncher = true, Icon = "@mipmap/icon")]
public class MainActivity : Activity
{
//int count = 1;
public void LoginToFacebook (bool allowCancel)
{
var auth = new OAuth2Authenticator (
clientId: "**************",
scope: "",
authorizeUrl: new Uri ("https://m.facebook.com/dialog/oauth/"),
redirectUrl: new Uri ("http://www.facebook.com/connect/login_success.html"));
auth.AllowCancel = allowCancel;
// If authorization succeeds or is canceled, .Completed will be fired.
auth.Completed += LoginComplete;
// UIViewController vc = auth.GetUI ();
// PresentViewController (vc, true, null);
}
public async void LoginComplete( object sender, AuthenticatorCompletedEventArgs e )
{
// We presented the UI, so it's up to us to dismiss it.
//DismissViewController (true, null);
if (!e.IsAuthenticated) {
Console.WriteLine ("Not Authorised");
return;
}
var accessToken = e.Account.Properties["access_token"].ToString();
var expiresIn = Convert.ToDouble(e.Account.Properties["expires_in"]);
var expiryDate = DateTime.Now + TimeSpan.FromSeconds( expiresIn );
// Now that we're logged in, make a OAuth2 request to get the user's id.
var request = new OAuth2Request ("GET", new Uri ("https://graph.facebook.com/me"), null, e.Account);
request.GetResponseAsync().ContinueWith (t => {
var builder = new AlertDialog.Builder (this);
if (t.IsFaulted)
{
builder.SetTitle ("Error");
builder.SetMessage (t.Exception.Flatten().InnerException.ToString());
}
else if (t.IsCanceled)
{
builder.SetTitle ("Task Canceled");
]
else {
var obj = JsonValue.Parse (t.Result.GetResponseText());
var id = obj["id"].ToString().Replace("\"","");
var user = ParseFacebookUtils.LogInAsync(id, accessToken,expiryDate);
builder.SetTitle ("Logged in");
builder.SetMessage ("Name: " + obj["name"]);
}
}
}
protected override void OnCreate (Bundle bundle)
{
Xamarin.Insights.Initialize (global::FoodTruck.Droid.XamarinInsights.ApiKey, this);
base.OnCreate (bundle);
SetContentView (Resource.Layout.Main);
StartActivity (auth.GetUI (this));
// var facebook = FindViewById<Button> (Resource.Id.FacebookButton);
// facebook.Click += delegate { LoginToFacebook(true);};
//
// var facebookNoCancel = FindViewById<Button> (Resource.Id.FacebookButtonNoCancel);
// facebookNoCancel.Click += delegate { LoginToFacebook(false);};
//};
}
}
}