/// <summary> /// Deletes Facebook application. Called when the "Delete application" button is pressed. /// Expects the CreateFacebookApplication method to be run first. /// </summary> private bool DeleteFacebookApplication() { // Get the Facebook application from DB FacebookApplicationInfo app = FacebookApplicationInfoProvider.GetFacebookApplicationInfo("MyNewApplication", SiteContext.CurrentSiteID); // Delete the Facebook application from DB FacebookApplicationInfoProvider.DeleteFacebookApplicationInfo(app); return(app != null); }
/// <summary> /// Button get token clicked event. /// </summary> protected void btnGetToken_OnClick(object sender, EventArgs e) { if (Form != null) { // Control data, show error if something is missing int appId = ValidationHelper.GetInteger(Form.GetFieldValue("FacebookAccountFacebookApplicationID"), 0); FacebookApplicationInfo appInfo = FacebookApplicationInfoProvider.GetFacebookApplicationInfo(appId); if (appInfo == null) { ShowError(GetString("sm.facebook.account.msg.appnotset")); return; } var facebookAccessTokenService = Service <IFacebookAccessTokenService> .Entry(); FacebookContext.FacebookAccessToken = facebookAccessTokenService.GetAccessToken(appInfo.FacebookApplicationConsumerKey, appInfo.FacebookApplicationConsumerSecret); string pageId = Form.GetFieldValue("FacebookAccountPageID").ToString(); if (String.IsNullOrEmpty(pageId)) { ShowError(GetString("sm.facebook.account.msg.pageurlnotset")); return; } // Store data in session string sessionKey = Guid.NewGuid().ToString(); Hashtable parameters = new Hashtable { { "AppId", appInfo.FacebookApplicationConsumerKey }, { "AppSecret", appInfo.FacebookApplicationConsumerSecret }, { "PageId", pageId }, { "TokenCntId", hdnToken.ClientID }, { "TokenExpirationCntId", hdnTokenExpiration.ClientID }, { "InfoLblId", lblMessage.ClientID }, { "TokenPageIdCntId", hdnTokenPageId.ClientID }, { "TokenAppIdCntId", hdnTokenAppId.ClientID }, { "TokenAppInfoId", appInfo.FacebookApplicationID.ToString() } }; WindowHelper.Add(sessionKey, parameters); // Open client dialog script string openDialogScript = String.Format("fbOpenModalDialog('{0}');", sessionKey); ScriptHelper.RegisterStartupScript(this, GetType(), "FBAccessTokenOpenModal" + ClientID, openDialogScript, true); } }
/// <summary> /// Gets and updates Facebook application. Called when the "Get and update application" button is pressed. /// Expects the CreateFacebookApplication method to be run first. /// </summary> private bool GetAndUpdateFacebookApplication() { // Get the Facebook application from DB FacebookApplicationInfo app = FacebookApplicationInfoProvider.GetFacebookApplicationInfo("MyNewApplication", SiteContext.CurrentSiteID); if (app != null) { // Update the properties app.FacebookApplicationDisplayName = app.FacebookApplicationDisplayName.ToLowerCSafe(); // Save the changes into DB FacebookApplicationInfoProvider.SetFacebookApplicationInfo(app); 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> /// Button get token clicked event. /// </summary> protected void btnGetToken_OnClick(object sender, EventArgs e) { if (Form != null) { // Control data, show error if something is missing int appId = ValidationHelper.GetInteger(Form.GetFieldValue("FacebookAccountFacebookApplicationID"), 0); FacebookApplicationInfo appInfo = FacebookApplicationInfoProvider.GetFacebookApplicationInfo(appId); if (appInfo == null) { ShowError(GetString("sm.facebook.account.msg.appnotset")); return; } string pageId = Form.GetFieldValue("FacebookAccountPageID").ToString(); if (String.IsNullOrEmpty(pageId)) { ShowError(GetString("sm.facebook.account.msg.pageidnotset")); return; } // Store data in session Hashtable parameters = new Hashtable { { "AppId", appInfo.FacebookApplicationConsumerKey }, { "AppSecret", appInfo.FacebookApplicationConsumerSecret }, { "PageId", pageId }, { "TokenCntId", hdnToken.ClientID }, { "TokenExpirationCntId", hdnTokenExpiration.ClientID }, { "InfoLblId", lblMessage.ClientID }, { "TokenPageIdCntId", hdnTokenPageId.ClientID }, { "TokenAppIdCntId", hdnTokenAppId.ClientID }, { "TokenAppInfoId", appInfo.FacebookApplicationID.ToString() } }; WindowHelper.Add(FacebookHelper.PAGE_ACCESS_TOKEN_SESSION_KEY, parameters); // Open client dialog script string openDialogScript = "fbOpenModalDialog();"; ScriptHelper.RegisterStartupScript(this, GetType(), "FBAccessTokenOpenModal" + ClientID, openDialogScript, true); } }
/// <summary> /// Creates Facebook application. Called when the "Create application" button is pressed. /// </summary> private bool CreateFacebookApplication() { // Verify the app's credentials have been provided. if (String.IsNullOrWhiteSpace(txtApiKey.Text) || String.IsNullOrWhiteSpace(txtApiSecret.Text)) { throw new Exception("[FacebookApiExamples.CreateFacebookApplication]: Empty values for ApiKey and ApiSecret are not allowed. Please provide your app's credentials."); } // Create new Facebook application object FacebookApplicationInfo newApplication = new FacebookApplicationInfo(); // Set the properties newApplication.FacebookApplicationDisplayName = "My new application"; newApplication.FacebookApplicationName = "MyNewApplication"; newApplication.FacebookApplicationSiteID = SiteContext.CurrentSiteID; newApplication.FacebookApplicationConsumerKey = txtApiKey.Text; newApplication.FacebookApplicationConsumerSecret = txtApiSecret.Text; // Save the Facebook application into DB FacebookApplicationInfoProvider.SetFacebookApplicationInfo(newApplication); return true; }
/// <summary> /// Creates Facebook application. Called when the "Create application" button is pressed. /// </summary> private bool CreateFacebookApplication() { // Verify the app's credentials have been provided. if (String.IsNullOrWhiteSpace(txtApiKey.Text) || String.IsNullOrWhiteSpace(txtApiSecret.Text)) { throw new Exception("[FacebookApiExamples.CreateFacebookApplication]: Empty values for ApiKey and ApiSecret are not allowed. Please provide your app's credentials."); } // Create new Facebook application object FacebookApplicationInfo newApplication = new FacebookApplicationInfo(); // Set the properties newApplication.FacebookApplicationDisplayName = "My new application"; newApplication.FacebookApplicationName = "MyNewApplication"; newApplication.FacebookApplicationSiteID = SiteContext.CurrentSiteID; newApplication.FacebookApplicationConsumerKey = txtApiKey.Text; newApplication.FacebookApplicationConsumerSecret = txtApiSecret.Text; // Save the Facebook application into DB FacebookApplicationInfoProvider.SetFacebookApplicationInfo(newApplication); 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; }