public object ConvertSourceToObject(PublishedPropertyType propertyType, object source, bool preview) { // Get the value as a string string str = source as string; // Deserialize the string return(String.IsNullOrWhiteSpace(str) ? null : TwitterOAuthData.Deserialize(str)); }
protected void Page_Load(object sender, EventArgs e) { Callback = Request.QueryString["callback"]; ContentTypeAlias = Request.QueryString["contentTypeAlias"]; PropertyAlias = Request.QueryString["propertyAlias"]; // Get the prevalue options TwitterOAuthPreValueOptions options = TwitterOAuthPreValueOptions.Get(ContentTypeAlias, PropertyAlias); if (!options.IsValid) { Content.Text += "<p><strong>ContentTypeAlias</strong> " + ContentTypeAlias + "</p>"; Content.Text += "<p><strong>PropertyAlias</strong> " + PropertyAlias + "</p>"; 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 TwitterOAuthClient client = new TwitterOAuthClient { ConsumerKey = options.ConsumerKey, ConsumerSecret = options.ConsumerSecret, Callback = Request.Url.ToString() }; // Check whether the user has denied the app if (HasUserDenied) { Session.Remove(DeniedToken); Content.Text = "<div class=\"error\">Error: The app was denied access to your account.</div>"; return; } #region OAuth 1.0a - Step 3 if (OAuthToken != null) { // Grab the request token from the session OAuthRequestToken token = Session[OAuthToken] as OAuthRequestToken; // Check whether the requets token was found in the current session if (token == null) { Content.Text = "<div class=\"error\">An error occured. Timeout?</div>"; return; } // Update the token and token secret client.Token = token.Token; client.TokenSecret = token.TokenSecret; // Now get the access token try { OAuthAccessToken accessToken = client.GetAccessToken(OAuthVerifier); client.Token = accessToken.Token; client.TokenSecret = accessToken.TokenSecret; } catch (Exception) { Content.Text = "<div class=\"error\">Unable to retrieve access token from <b>Twitter.com</b>.</div>"; return; } try { // Initialize an instance of TwitterService TwitterService service = TwitterService.CreateFromOAuthClient(client); // Get information about the server TwitterUser user = service.Account.VerifyCredentials().Body; Content.Text += "<p>Hi <strong>" + (String.IsNullOrEmpty(user.Name) ? user.ScreenName : user.Name) + "</strong></p>"; Content.Text += "<p>Please wait while you're being redirected...</p>"; // Set the callback data TwitterOAuthData data = new TwitterOAuthData { Id = user.Id, ScreenName = user.ScreenName, Name = String.IsNullOrEmpty(user.Name) ? "" : user.Name, Avatar = user.ProfileImageUrlHttps, ConsumerKey = client.ConsumerKey, ConsumerSecret = client.ConsumerSecret, AccessToken = client.Token, AccessTokenSecret = client.TokenSecret }; // Update the UI and close the popup window Page.ClientScript.RegisterClientScriptBlock(GetType(), "callback", String.Format( "self.opener." + Callback + "({0}); window.close();", data.Serialize() ), true); } catch (TwitterException ex) { Content.Text = "<div class=\"error\">Error in the communication with Twitter.com<br /><br />" + ex.Message + " (Code: " + ex.Code + ")</div>"; } catch (Exception) { Content.Text = "<div class=\"error\">Error in the communication with Twitter.com</div>"; } return; } #endregion #region OAuth 1.0a - Step 1 // Get a request token from the Twitter API OAuthRequestToken requestToken = client.GetRequestToken(); // Save the token information to the session so we can grab it later Session[requestToken.Token] = requestToken; // Redirect the user to the authentication page at Twitter.com Response.Redirect(requestToken.AuthorizeUrl); #endregion }