Forum Xamarin.Forms

How to overcome AwaitingTaskActivation in Tasking and HttpClient

LindsayMilesLindsayMiles CAMember ✭✭✭
edited April 2018 in Xamarin.Forms

Hello,

I have a page renderer allowing facebook login. That works and the user can login.

Two handlers are assigned to the OAuth2Authenticator object:

            auth.Completed += OnAuthenticationCompleted;
            auth.Error += OnAuthenticationFailed;

This handler then needs to retrieve the user ID and name from Facebook graph

    private void OnAuthenticationCompleted(object sender, AuthenticatorCompletedEventArgs e)
    {
        if (e.IsAuthenticated)
        {
            var accessToken = e.Account.Properties["access_token"];

            Task<string> s = GetUserIDAsync(accessToken);

            App.SuccessfulLoginAction.Invoke();               
        }
        else
        {
            // The user is not authenticated
        }
    }

The async method is this:

    public async Task<string> GetUserIDAsync(string accessToken)
    {
        var httpClient = new HttpClient();
        var userIdjson = await httpClient.GetStringAsync($"https://graph.facebook.com/v2.12/me?fields=id,name&access_token=" + accessToken);            
        var userId = JsonConvert.DeserializeObject(userIdjson);
        return userId.ToString();
    }

If I put that url + params + accesstoken into a browser, I get the data returned immediately, howerver, when it gets to this same call in code...

        var userIdjson = await httpClient.GetStringAsync($"https://graph.facebook.com/v2.12/me?fields=id,name&access_token=" + accessToken);

...I see a return stating AwaitingTaskActivation.

It never makes the call to that endpoint. Waits ... and waits ... and waits...
This will simply not go further than that call.

Is there something I ought to be doing in tasking to activate that task?
Alternatively, any other way to retrieve the user Id and name (user_profile) data from FB graph?

ANY help would be much appreciated!

Thanks

Answers

  • LindsayMilesLindsayMiles CAMember ✭✭✭

    So this works and does not incur an AwaitingTaskActivation status...

                JObject obj = new JObject();
                var request = new OAuth2Request("GET", new Uri("https://graph.facebook.com/me"), null, e.Account);
                request.GetResponseAsync().ContinueWith(t => {
                    t.Wait();
                    if (t.IsFaulted)
                    {
                        //App.UnSuccessfulLoginAction.Invoke();
                    }
                    else
                    {
                        obj = JObject.Parse(t.Result.GetResponseText());
                        string userId = (string)obj["id"];
                        Settings.UserId = userId;
                    }
                });
    

    So, right now, no idea why. But it works and if someone has a similar issue, there's something to try/use...

Sign In or Register to comment.