protected void Page_Load(object sender, EventArgs e) { Callback = Request.QueryString["callback"]; ContentTypeAlias = Request.QueryString["contentTypeAlias"]; PropertyAlias = Request.QueryString["propertyAlias"]; if (AuthState != null) { string[] stateValue = Session["Skybrud.Social_" + AuthState] as string[]; if (stateValue != null && stateValue.Length == 3) { Callback = stateValue[0]; ContentTypeAlias = stateValue[1]; PropertyAlias = stateValue[2]; } } // Get the prevalue options FacebookOAuthPreValueOptions options = FacebookOAuthPreValueOptions.Get(ContentTypeAlias, PropertyAlias); if (!options.IsValid) { Content.Text = "Hold on now! The options of the underlying prevalue editor isn't valid."; return; } // Configure the OAuth client based on the options of the prevalue options FacebookOAuthClient client = new FacebookOAuthClient { AppId = options.AppId, AppSecret = options.AppSecret, RedirectUri = options.RedirectUri }; // Session expired? if (AuthState != null && Session["Skybrud.Social_" + AuthState] == null) { Content.Text = "<div class=\"error\">Session expired?</div>"; return; } // Check whether an error response was received from Facebook if (AuthError != null) { Content.Text = "<div class=\"error\">Error: " + AuthErrorDescription + "</div>"; return; } // Redirect the user to the Facebook login dialog if (AuthCode == null) { // Generate a new unique/random state string state = Guid.NewGuid().ToString(); // Save the state in the current user session Session["Skybrud.Social_" + state] = new[] { Callback, ContentTypeAlias, PropertyAlias }; // Construct the authorization URL string url = client.GetAuthorizationUrl(state, options.Scope); // Redirect the user Response.Redirect(url); return; } // Exchange the authorization code for a user access token string userAccessToken; try { userAccessToken = client.GetAccessTokenFromAuthCode(AuthCode); } catch (Exception ex) { Content.Text = "<div class=\"error\"><b>Unable to acquire access token</b><br />" + ex.Message + "</div>"; return; } try { // Initialize the Facebook service (no calls are made here) FacebookService service = FacebookService.CreateFromAccessToken(userAccessToken); // Make a call to the Facebook API to get information about the user FacebookUser me = service.Users.GetUser("me").Body; //Get accounts information. Only works with "manage_pages" permission FacebookAccountsResponse response = null; if (options.Scope.Contains("manage_pages")) { response = service.Accounts.GetAccounts(); } // Get debug information about the access token FacebookDebugToken debugToken = null; try { debugToken = service.Debug.DebugToken(userAccessToken).Body; } catch (Exception ex) { Content.Text = "<div class=\"error\"><b>Unable to acquire debug token. Are you a developer?</b><br />" + ex.Message + "</div>"; } Content.Text += "<p>Hi <strong>" + me.Name + "</strong></p>"; Content.Text += "<p>Please wait while you're being redirected...</p>"; // Set the callback data FacebookOAuthData data = new FacebookOAuthData { Id = me.Id, Name = me.Name, AccessToken = userAccessToken, ExpiresAt = DateTime.Now.AddDays(60), BusinessPages = new FacebookBusinessPageData[0], SelectedBusinessPage = null }; // Set the the business pages (if available) if (response != null) { data.BusinessPages = response.Body.Data.Select(ac => new FacebookBusinessPageData { Id = ac.Id, Name = ac.Name, AccessToken = ac.AccessToken }).ToArray(); } // Update the OAuth data with information from the debug token if (debugToken != null) { data.ExpiresAt = (debugToken.Data.ExpiresAt ?? DateTime.Now.AddDays(60));//NULL when manage_pages permission is granted data.Scope = ( from scope in debugToken.Data.Scopes select scope.Name ).ToArray(); } // Update the UI and close the popup window Page.ClientScript.RegisterClientScriptBlock(GetType(), "callback", String.Format( "self.opener." + Callback + "({0}); window.close();", data.Serialize()//Save JSON data through callback parameter ), true); } catch (Exception ex) { Content.Text = "<div class=\"error\"><b>Unable to get user information</b><br />" + ex.Message + "</div>"; } }
/// <summary> /// Gets information about accounts associated with the current user by calling the <var>/me/accounts</var> method. This call requires a user access token. /// </summary> public FacebookAccountsResponse GetAccounts() { return(FacebookAccountsResponse.ParseJson(Raw.GetAccounts())); }