/// <summary>
    /// Creates a Twitter channel.
    /// </summary>
    private bool CreateTwitterChannel()
    {
        if (string.IsNullOrEmpty(txtAccessToken.Text) || string.IsNullOrEmpty(txtAccessTokenSecret.Text))
        {
            throw new Exception("[ApiExamples.CreateTwitterChannel]: Empty values for 'Channel access token' and 'Channel access token secret' are not allowed. Please provide your channel's credentials.");
        }

        // Get the app the channel is tied to.
        TwitterApplicationInfo app = TwitterApplicationInfoProvider.GetTwitterApplicationInfo("MyNewTwitterApp", SiteContext.CurrentSiteName);

        if (app == null)
        {
            throw new Exception("[ApiExamples.CreateTwitterChannel]: Application 'MyNewTwitterApp' was not found.");
        }

        // Create new channel object
        TwitterAccountInfo channel = new TwitterAccountInfo();

        // Set the properties
        channel.TwitterAccountDisplayName = "My new Twitter channel";
        channel.TwitterAccountName        = "MyNewTwitterChannel";

        channel.TwitterAccountAccessToken          = txtAccessToken.Text;
        channel.TwitterAccountAccessTokenSecret    = txtAccessTokenSecret.Text;
        channel.TwitterAccountSiteID               = SiteContext.CurrentSiteID;
        channel.TwitterAccountTwitterApplicationID = app.TwitterApplicationID;

        // Save the channel into DB
        TwitterAccountInfoProvider.SetTwitterAccountInfo(channel);

        return(true);
    }
    /// <summary>
    /// Deletes an existing Twitter app.
    /// </summary>
    private bool DeleteTwitterApp()
    {
        //Get the app from DB
        TwitterApplicationInfo app = TwitterApplicationInfoProvider.GetTwitterApplicationInfo("MyNewTwitterApp", SiteContext.CurrentSiteName);

        if (app != null)
        {
            // Delete the app from DB
            TwitterApplicationInfoProvider.DeleteTwitterApplicationInfo(app);

            return(true);
        }

        return(false);
    }
    /// <summary>
    /// Gets a Twitter app from the database and modifies it.
    /// </summary>
    private bool GetAndUpdateTwitterApp()
    {
        // Get the app from DB
        TwitterApplicationInfo app = TwitterApplicationInfoProvider.GetTwitterApplicationInfo("MyNewTwitterApp", SiteContext.CurrentSiteName);

        if (app != null)
        {
            // Update the app
            app.TwitterApplicationDisplayName = app.TwitterApplicationDisplayName.ToLowerCSafe();

            //Save the changes into DB
            TwitterApplicationInfoProvider.SetTwitterApplicationInfo(app);

            return(true);
        }

        return(false);
    }
Exemple #4
0
    private void Control_OnBeforeSave(object sender, EventArgs e)
    {
        TwitterAccountInfo account = Control.EditedObject as TwitterAccountInfo;

        if (account != null)
        {
            TwitterApplicationInfo application = TwitterApplicationInfoProvider.GetTwitterApplicationInfo(account.TwitterAccountTwitterApplicationID);
            try
            {
                account.TwitterAccountUserID = TwitterHelper.GetTwitterUserId(application.TwitterApplicationConsumerKey, application.TwitterApplicationConsumerSecret, account.TwitterAccountAccessToken, account.TwitterAccountAccessTokenSecret);
                string errorMessage = null;
                Validate <TwitterAccountInfo>("TwitterAccountUserID", account.TwitterAccountUserID, "sm.twitter.account.msg.useridexists", ref errorMessage);
                if (!String.IsNullOrEmpty(errorMessage))
                {
                    CancelPendingSave(errorMessage);
                }
            }
            catch
            {
                string errorMessage = ResHelper.GetString("sm.twitter.account.msg.getuseridfail");
                CancelPendingSave(errorMessage);
            }
        }
    }
Exemple #5
0
    /// <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 ImportTwitterSettings(SiteInfo site)
    {
        TwitterApplicationInfo twittAppInfo = new TwitterApplicationInfo()
        {
            TwitterApplicationDisplayName    = site.DisplayName + "Twitter App",
            TwitterApplicationName           = site.SiteName + "TwitterApp",
            TwitterApplicationConsumerKey    = SettingsKeyInfoProvider.GetStringValue(site.SiteName + ".CMSTwitterConsumerKey"),
            TwitterApplicationConsumerSecret = SettingsKeyInfoProvider.GetStringValue(site.SiteName + ".CMSTwitterConsumerSecret"),
            TwitterApplicationSiteID         = site.SiteID
        };

        if (String.IsNullOrWhiteSpace(twittAppInfo.TwitterApplicationConsumerKey) || String.IsNullOrWhiteSpace(twittAppInfo.TwitterApplicationConsumerSecret))
        {
            return;
        }

        try
        {
            TwitterApplicationInfoProvider.SetTwitterApplicationInfo(twittAppInfo);
            twittAppInfo = TwitterApplicationInfoProvider.GetTwitterApplicationInfo(site.SiteName + "TwitterApp", site.SiteName);
        }
        catch (Exception ex)
        {
            EventLogProvider.LogException("Upgrade to 8.0", "Upgrade of SocialMarketing", ex, additionalMessage: "Error during Twitter application storage to DB for site " + site.SiteName, siteId: site.SiteID);

            return;
        }

        TwitterAccountInfo twittAccountInfo = new TwitterAccountInfo()
        {
            TwitterAccountName                 = site.SiteName + "TwitterChannel",
            TwitterAccountDisplayName          = site.DisplayName + " Twitter Channel",
            TwitterAccountTwitterApplicationID = twittAppInfo.TwitterApplicationID,
            TwitterAccountSiteID               = site.SiteID,
            TwitterAccountAccessToken          = SettingsKeyInfoProvider.GetStringValue(site.SiteName + ".CMSTwitterAccessToken"),
            TwitterAccountAccessTokenSecret    = SettingsKeyInfoProvider.GetStringValue(site.SiteName + ".CMSTwitterAccessTokenSecret"),
            TwitterAccountIsDefault            = true,
        };

        twittAccountInfo.TwitterAccountUserID = GetTwitterUserId(twittAppInfo, twittAccountInfo);

        if (String.IsNullOrWhiteSpace(twittAccountInfo.TwitterAccountAccessToken) || String.IsNullOrWhiteSpace(twittAccountInfo.TwitterAccountAccessTokenSecret))
        {
            return;
        }

        try
        {
            TwitterAccountInfoProvider.SetTwitterAccountInfo(twittAccountInfo);
        }
        catch (Exception ex)
        {
            EventLogProvider.LogException("Upgrade to 8.0", "Upgrade of SocialMarketing", ex, additionalMessage: "Error during Twitter channel storage to DB for site " + site.SiteName, siteId: site.SiteID);

            return;
        }

        // URL shortener
        if (!SettingsKeyInfoProvider.IsValueInherited(site.SiteName + ".CMSTwitterURLShortenerType"))
        {
            string shortener = SettingsKeyInfoProvider.GetStringValue(site.SiteName + ".CMSTwitterURLShortenerType");
            SettingsKeyInfoProvider.SetValue(site.SiteName + ".CMSSocialMarketingURLShorteningTwitter", shortener);
        }

        return;
    }