Example #1
0
        /// <summary>
        /// This function extracts access_token from the response returned from web authentication broker
        /// and uses that token to get user information using facebook graph api.
        /// </summary>
        /// <param name="responseData">responseData returned from AuthenticateAsync result.</param>
        private async Task GetFacebookUserNameAsync(string responseData)
        {
            var decoder = new WwwFormUrlDecoder(responseData);
            var error   = decoder.TryGetValue("error");

            if (error != null)
            {
                FacebookUserName.Text = $"Error: {error}";
                return;
            }

            // You can store access token locally for further use.
            string access_token = decoder.GetFirstValueByName("access_token");
            string expires_in   = decoder.TryGetValue("expires_in"); // expires_in is optional

            HttpClient httpClient = new HttpClient();
            Uri        uri        = new Uri($"https://graph.facebook.com/me?access_token={Uri.EscapeDataString(access_token)}");

            try
            {
                string response = await httpClient.GetStringAsync(uri);

                JsonObject userInfo = JsonObject.Parse(response).GetObject();
                FacebookUserName.Text = userInfo.GetNamedString("name");
            }
            catch (Exception)
            {
                FacebookUserName.Text = "Error contacting Facebook";
            }
        }
        /// <summary>
        /// This function extracts access_token from the response returned from web authentication broker
        /// and uses that token to get user information using facebook graph api.
        /// </summary>
        /// <param name="responseData">responseData returned from AuthenticateAsync result.</param>
        private async Task GetFacebookUserNameAsync(string responseData)
        {
            var decoder = new WwwFormUrlDecoder(responseData);
            var error   = decoder.TryGetValue("error");

            if (error != null)
            {
                FacebookUserName.Text = $"Error: {error}";
                return;
            }

            // You can store access token locally for further use.
            string access_token = decoder[0].Value;

            this.fb_token = decoder[0].Value;
            string expires_in = decoder.TryGetValue("expires_in"); // expires_in is optional

            Windows.Web.Http.HttpClient httpClient = new Windows.Web.Http.HttpClient();
            Uri uri = new Uri($"https://graph.facebook.com/me?fields=id,name,picture,birthday&access_token={Uri.EscapeDataString(access_token)}");

            HttpGetStringResult result = await httpClient.TryGetStringAsync(uri);

            if (result.Succeeded)
            {
                Windows.Data.Json.JsonObject userInfo = Windows.Data.Json.JsonObject.Parse(result.Value).GetObject();
                FacebookUserName.Text = userInfo.GetNamedString("name");
                this.userID           = userInfo.GetNamedString("id");
            }
            else
            {
                FacebookUserName.Text = "Error contacting Facebook";
            }

            Windows.Web.Http.HttpClient httpClient2 = new Windows.Web.Http.HttpClient();
            Uri uri2 = new Uri($"https://graph.facebook.com/{this.userID}/accounts?fields=name,access_token&access_token={this.fb_token}");

            HttpGetStringResult myresult = await httpClient.TryGetStringAsync(uri2);

            if (myresult.Succeeded)
            {
                Windows.Data.Json.JsonObject userInfo2 = Windows.Data.Json.JsonObject.Parse(myresult.Value).GetObject();
                this.Page_token = userInfo2["data"].GetArray()[0].GetObject().GetNamedString("access_token");
            }
            else
            {
                Debug.Write("man ta osso!");
            }

            Windows.Web.Http.HttpClient httpClient3 = new Windows.Web.Http.HttpClient();
            Uri uri3 = new Uri($"https://graph.facebook.com/{this.userID}/accounts?access_token={this.fb_token}");

            HttpGetStringResult result3 = await httpClient.TryGetStringAsync(uri3);

            if (result3.Succeeded)
            {
                Windows.Data.Json.JsonObject userInfo = Windows.Data.Json.JsonObject.Parse(result3.Value).GetObject();
                this.PageId = userInfo["data"].GetArray()[0].GetObject().GetNamedString("id");
            }
            else
            {
                FacebookUserName.Text = "Error contacting Facebook";
            }
        }