/// <summary> /// Gets and updates Facebook page. Called when the "Get and update page" button is pressed. /// Expects the CreateFacebookPage method to be run first. /// </summary> private bool GetAndUpdateFacebookPage() { // Get the Facebook page from DB FacebookAccountInfo page = FacebookAccountInfoProvider.GetFacebookAccountInfo("MyNewPage", SiteContext.CurrentSiteID); if (page != null) { // Update the properties page.FacebookAccountDisplayName = page.FacebookAccountDisplayName.ToLowerCSafe(); // Save the changes into DB FacebookAccountInfoProvider.SetFacebookAccountInfo(page); return(true); } return(false); }
/// <summary> /// Creates Facebook page. Called when the "Create page" button is pressed. /// </summary> private bool CreateFacebookPage() { // Check if all the fields have been properly set if (String.IsNullOrWhiteSpace(txtPageUrl.Text) || String.IsNullOrWhiteSpace(txtFacebookPageAccessToken.Text)) { throw new Exception("[FacebookApiExamples.CreateFacebookPage]: Empty values for 'Facebook page URL' and 'Access token' are not allowed. Please provide your page's credentials."); } // Get the application ID you want to associate the page with first. FacebookApplicationInfo app = FacebookApplicationInfoProvider.GetFacebookApplicationInfo("MyNewApplication", SiteContext.CurrentSiteID); if (app == null) { throw new Exception("[FacebookApiExamples.CreateFacebookPage]: Application 'MyNewApplication' was not found."); } int appId = app.FacebookApplicationID; // Create new Facebook page object FacebookAccountInfo page = new FacebookAccountInfo(); // Set the properties page.FacebookAccountDisplayName = "My new page"; page.FacebookAccountName = "MyNewPage"; page.FacebookAccountSiteID = SiteContext.CurrentSiteID; page.FacebookAccountFacebookApplicationID = appId; // Use FacebookHelper to get the ID that facebook has given the page. It's used to identify the page later when posting on its wall. Throw exception if failed. string myFacebookPageId; if (!FacebookHelper.TryGetFacebookPageId(txtPageUrl.Text, out myFacebookPageId)) { throw new Exception("[FacebookApiExamples.CreateFacebookPage]: Failed to get PageID from Facebook. The 'Page Url', you have entered is not a valid Facebook Page Url."); } // see https://developers.Facebook.com/docs/Facebook-login/access-tokens/ or http://Facebooksdk.net/docs/web/ or use https://developers.Facebook.com/tools/explorer/ to get access token now page.FacebookPageIdentity = new FacebookPageIdentityData(txtPageUrl.Text, myFacebookPageId); page.FacebookPageAccessToken = new FacebookPageAccessTokenData(txtFacebookPageAccessToken.Text, null); // Save the Facebook page into DB FacebookAccountInfoProvider.SetFacebookAccountInfo(page); return(true); }
/// <summary> /// Attempts to upgrade settings from the old way to he new-one. /// </summary> /// <param name="site">site we are importing for</param> /// <returns>true on success, false on failure</returns> private static void ImportFacebookSettings(SiteInfo site) { FacebookApplicationInfo fbAppInfo = new FacebookApplicationInfo() { FacebookApplicationDisplayName = site.DisplayName + " Facebook App", FacebookApplicationName = site.SiteName + "FacebookApp", FacebookApplicationSiteID = site.SiteID, FacebookApplicationConsumerKey = SettingsKeyInfoProvider.GetStringValue(site.SiteName + ".CMSFacebookConnectApiKey"), FacebookApplicationConsumerSecret = SettingsKeyInfoProvider.GetStringValue(site.SiteName + ".CMSFacebookApplicationSecret") }; if (String.IsNullOrWhiteSpace(fbAppInfo.FacebookApplicationConsumerKey) || String.IsNullOrWhiteSpace(fbAppInfo.FacebookApplicationConsumerSecret)) { return; } try { FacebookApplicationInfoProvider.SetFacebookApplicationInfo(fbAppInfo); fbAppInfo = FacebookApplicationInfoProvider.GetFacebookApplicationInfo(site.SiteName + "FacebookApp", site.SiteName); } catch (Exception ex) { // LogException EventLogProvider.LogException("Upgrade to 8.0", "Upgrade of SocialMarketing", ex, additionalMessage: "Error during Facebook Application storage to DB for site " + site.SiteName, siteId: site.SiteID); return; } // FB Page Part FacebookAccountInfo fbPageInfo = new FacebookAccountInfo() { FacebookAccountFacebookApplicationID = fbAppInfo.FacebookApplicationID, FacebookAccountSiteID = site.SiteID, FacebookAccountDisplayName = site.DisplayName + " Facebook Page", FacebookAccountName = site.SiteName + "FacebookPage", FacebookAccountIsDefault = true, }; FacebookPageAccessTokenData?accToken = GetToken(SettingsKeyInfoProvider.GetStringValue(site.SiteName + ".CMSFacebookAccessToken")); if (accToken.HasValue) { fbPageInfo.FacebookPageAccessToken = accToken.Value; } else { // Log error importing settings for site EventLogProvider.LogEvent(EventType.ERROR, "Upgrade to 8.0", "Upgrade of SocialMarketing", eventDescription: "Error during parsing of PageAccessToken for site " + site.SiteName, siteId: site.SiteID); return; } FacebookPageIdentityData?PIData = GetPageIdentity(site); if (PIData.HasValue) { fbPageInfo.FacebookPageIdentity = PIData.Value; } else { // Log error importing settings for site EventLogProvider.LogEvent(EventType.ERROR, "Upgrade to 8.0", "Upgrade of SocialMarketing", eventDescription: "Error during Getting of PageIdentity for site " + site.SiteName, siteId: site.SiteID); return; } if (String.IsNullOrWhiteSpace(fbPageInfo.FacebookPageAccessToken.AccessToken) || String.IsNullOrWhiteSpace(fbPageInfo.FacebookPageIdentity.PageId) || String.IsNullOrWhiteSpace(fbPageInfo.FacebookPageIdentity.PageUrl)) { return; } try { FacebookAccountInfoProvider.SetFacebookAccountInfo(fbPageInfo); } catch (Exception ex) { // Log Exception EventLogProvider.LogException("Upgrade to 8.0", "Upgrade of SocialMarketing", ex, additionalMessage: "Error during Facebook Page storage to DB for site " + site.SiteName, siteId: site.SiteID); return; } // URL shortener if (!SettingsKeyInfoProvider.IsValueInherited(site.SiteName + ".CMSFacebookURLShortenerType")) { string shortener = SettingsKeyInfoProvider.GetStringValue(site.SiteName + ".CMSFacebookURLShortenerType"); SettingsKeyInfoProvider.SetValue(site.SiteName + ".CMSSocialMarketingURLShorteningFacebook", shortener); } return; }