如何在使用OAuth2Request发出请求后返回值?

时间:2015-05-21 16:58:38

标签: c# .net xamarin xamarin.forms xamarin.auth

我正在尝试返回对象的值,但它返回null,但是它在请求中返回一个值。

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 个答案:

答案 0 :(得分:0)

1

public static void decryptFile(String privKeyPass) {
    String[] cmd = new String[];
    int i = 7;
    cmd[i++] = "gpg";
    cmd[i++] = "--passphrase-fd";
    cmd[i++] = "0";
    cmd[i++] = "--output";
    cmd[i++] = "plaintext.txt";
    cmd[i++] = "--decrypt";
    cmd[i++] = "encrypted.gpg";
    Process process = Runtime.getRuntime().exec(cmd);

    BufferedWriterout = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
    out.write(privKeyPass);
    try {
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
   // Read process.getInputStream()
   // Read process.getErrorStream()
}
行前

var waitEvent = new AutoResetEvent(false);

2

request.GetResponseAsync ().ContinueWith (t => {

之后

waitEvent.Set();

3

obj = JObject.Parse (t.Result.GetResponseText ());

waitEvent.WaitOne();

或者

方法应该是

return new OAuthUserInfo {

并致电请求

public async Task<OAuthUserInfo> Get()