2

I am currently using Xamarin.Auth on a iOS project to handle some user authentication via Facebook and Twitter in my application. The Facebook authentication using OAuth2Authenticator works great and my implementation was based mainly off the docs (http://components.xamarin.com/gettingstarted/xamarin.auth). Twitter however still uses OAuth1 it seems and thus I based my implementation mainly off the answer in this StackOverflow questions (https://stackoverflow.com/a/21982205). Everything works properly and I am able to retrieve user, tweets, etc. but after all the code executes I receive a "Authentication Error" popup on the screen saying "Object reference not set to an instance of an object." there is nothing printed to the console however as is the case with most normal errors I have seen thus far. I can dismiss the popup and everything continues to preform correctly. I believe I have narrowed the problem down to something within the OAuth1Authenticator request as I still receive the error when all of the other handling code has been commented out. Please reference the code below to see what might be the cause of this.

public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();

        signupBtn.TouchUpInside += delegate {
            LoginToTwitter(true, this);
        };
    }
    void LoginToTwitter(bool allowCancel, UIViewController _vc)
    {

        var auth = new OAuth1Authenticator (
            consumerKey: "My Consumer Key", 
            consumerSecret: "My Consumer Secret", 
            requestTokenUrl: new Uri("https://api.twitter.com/oauth/request_token"), 
            authorizeUrl: new Uri("https://api.twitter.com/oauth/authorize"), 
            accessTokenUrl: new Uri("https://api.twitter.com/oauth/access_token"), 
            callbackUrl: new Uri("My callback url"),
            getUsernameAsync: (IDictionary<string, string> accountProperties) => {
                string screen_name = "";
                if (accountProperties.TryGetValue("screen_name", out screen_name)) {
                    Console.WriteLine("SN: {0}", screen_name);
                    Account a = new Account(screen_name, accountProperties);
                    AuthenticatorCompletedEventArgs e = new AuthenticatorCompletedEventArgs(a);
                    TwitterCompleted(e, _vc);
                }
                return null;}
        );
        auth.AllowCancel = allowCancel;

        UIViewController authView = auth.GetUI ();

        _vc.PresentViewController (authView, true, null);
    }
    void TwitterCompleted (AuthenticatorCompletedEventArgs e, UIViewController _vc)
    {
        var theAccount = e.Account;
        var theProperties = theAccount.Properties;
        foreach (var item in theProperties) {
            Console.WriteLine (item); //debugging
        }
        InvokeOnMainThread (delegate {
            _vc.DismissViewController (true, null);
        });

        AccountStore.Create ().Save (e.Account, "Twitter");

        if (!e.IsAuthenticated) {
            Console.WriteLine("Not authorized");
            return;
        }

        theScreenName = e.Account.Properties["screen_name"];
        theCount = "2";

        IDictionary<string, string> theDict = new Dictionary<string, string>();;
        theDict.Add("screen_name", theScreenName);
        theDict.Add("count", theCount);


        var request = new OAuth1Request("GET", new Uri("https://api.twitter.com/1.1/statuses/user_timeline.json"), theDict, e.Account, false); 
        request.GetResponseAsync().ContinueWith (t => {
            if (t.IsFaulted)
                Console.WriteLine("Error: {0}", t.Exception.InnerException.Message);
            else if (t.IsCanceled)
                Console.WriteLine("Canceled");
            else
            {
                var obj = JsonValue.Parse (t.Result.GetResponseText());
                Console.WriteLine("object: {0}", obj); // debugging
            }

        }, uiScheduler);
        return;
    }
    private readonly TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
Community
  • 1
  • 1

1 Answers1

-1

instead of returning null in "getUsernameAsync" return Task