使用 OAuth2Request 发出请求后如何 return 一个值?
How to return a value after making the request with OAuth2Request?
我正在尝试 return 一个对象的值,但它 return 是空的,但是它 return 是请求中的一个值。
using System;
using Xamarin.Auth;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Linq;
using Xamarin.Forms;
namespace RegistroAgil_Couchbase.Droid
{
public class UserInfoRepository
{
public OAuthUserInfo Get()
{
var mainActivity = Forms.Context as MainActivity;
var accounts = AccountStore.Create(mainActivity).FindAccountsForService("Facebook");
var enumerable = accounts as IList<Account> ?? accounts.ToList();
var account = enumerable.FirstOrDefault () == null ? null : enumerable.First ();
if (account == null) {
return null;
} else {
var request = new OAuth2Request ("GET", new Uri ("https://graph.facebook.com/me"), null, account);
JObject obj = new JObject ();
request.GetResponseAsync ().ContinueWith (t => {
if (t.IsFaulted)
Console.WriteLine ("Error: " + t.Exception.InnerException.Message);
else {
obj = JObject.Parse (t.Result.GetResponseText ());
Console.WriteLine(obj["name"]); // <-- Here returns a value.
}
});
return new OAuthUserInfo {
AuthName = (string)obj ["name"] // <-- Returns null.
};
}
}
}
}
如果有人能提供任何帮助,我将不胜感激。提前致谢!
1.
var waitEvent = new AutoResetEvent(false);
行前
request.GetResponseAsync ().ContinueWith (t => {
2.
waitEvent.Set();
在
之后
obj = JObject.Parse (t.Result.GetResponseText ());
3.
waitEvent.WaitOne();
之前
return new OAuthUserInfo {
或
方法应该是
public async Task<OAuthUserInfo> Get()
并致电请求
var object = await request.GetResponseAsync ();
我正在尝试 return 一个对象的值,但它 return 是空的,但是它 return 是请求中的一个值。
using System;
using Xamarin.Auth;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Linq;
using Xamarin.Forms;
namespace RegistroAgil_Couchbase.Droid
{
public class UserInfoRepository
{
public OAuthUserInfo Get()
{
var mainActivity = Forms.Context as MainActivity;
var accounts = AccountStore.Create(mainActivity).FindAccountsForService("Facebook");
var enumerable = accounts as IList<Account> ?? accounts.ToList();
var account = enumerable.FirstOrDefault () == null ? null : enumerable.First ();
if (account == null) {
return null;
} else {
var request = new OAuth2Request ("GET", new Uri ("https://graph.facebook.com/me"), null, account);
JObject obj = new JObject ();
request.GetResponseAsync ().ContinueWith (t => {
if (t.IsFaulted)
Console.WriteLine ("Error: " + t.Exception.InnerException.Message);
else {
obj = JObject.Parse (t.Result.GetResponseText ());
Console.WriteLine(obj["name"]); // <-- Here returns a value.
}
});
return new OAuthUserInfo {
AuthName = (string)obj ["name"] // <-- Returns null.
};
}
}
}
}
如果有人能提供任何帮助,我将不胜感激。提前致谢!
1.
var waitEvent = new AutoResetEvent(false);
行前
request.GetResponseAsync ().ContinueWith (t => {
2.
waitEvent.Set();
在
之后obj = JObject.Parse (t.Result.GetResponseText ());
3.
waitEvent.WaitOne();
之前
return new OAuthUserInfo {
或
方法应该是
public async Task<OAuthUserInfo> Get()
并致电请求
var object = await request.GetResponseAsync ();