Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Michael Nielsen 99 posts 619 karma points
    Apr 28, 2015 @ 12:18
    Michael Nielsen
    0

    Cannot implicitly convert type

    I am getting:

    Cannot implicitly convert type 'Skybrud.Social.Twitter.Responses.TwitterUserResponse' to 'Skybrud.Social.Twitter.Objects.TwitterUser'

    From this:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using Skybrud.Social.Twitter
    @using Skybrud.Social.Twitter.Objects
    @using Skybrud.Social.Umbraco.Twitter.PropertyEditors.OAuth
    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @{
        TwitterOAuthData twitter = Model.GetPropertyValue("twitter") as TwitterOAuthData;
        if (twitter != null && twitter.IsValid) {
            TwitterService service = twitter.GetService();
            TwitterUser user = service.Users.GetUser(twitter.Id);
        }
    }

    I am using the exact same code with Skybrud.Social for Umbraco 7 v1.0.0 package on another site.

    What has changed from v1.0.0 to v1.0.1 and how do I fix the problem?

  • Anders Bjerner 419 posts 2314 karma points MVP 3x admin c-trib
    Apr 28, 2015 @ 12:44
    Anders Bjerner
    1

    Hi,

    I'm sorry if I haven't made it clear, but v1.0.1 uses a newer version of Skybrud.Social (that has some breaking changes).

    For your example, the change is however rather small - so rather than:

    TwitterUser user = service.Users.GetUser(twitter.Id);
    

    You should write:

    TwitterUser user = service.Users.GetUser(twitter.Id).Body;
    

    The reason for this change is so that the call to the API will return information about the entire response (eg. Twitter send rate limiting information in the HTTP headers).

  • Søren Mastrup 110 posts 495 karma points c-trib
    Apr 28, 2015 @ 13:13
    Søren Mastrup
    0

    How would your original example look in relation to the new version? 

    @using Skybrud.Social.Twitter
    @using Skybrud.Social.Twitter.Objects
    @using Skybrud.Social.Umbraco.Twitter.PropertyEditors.OAuth
    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @{
    
        // Get the OAuth information stored in the property
        TwitterOAuthData twitter = Model.GetPropertyValue("twitter") as TwitterOAuthData;
    
        // Check whether the OAuth data is valid
        if (twitter != null && twitter.IsValid) {
    
            // Gets an instance of TwitterService based on the OAuth data
            TwitterService service = twitter.GetService();
    
            // Get information about the authenticated user
            TwitterUser user = service.Users.GetUser(twitter.Id);
    
            // Get recent status messages (tweets) from the authenticated user
            TwitterTimeline recent = service.Statuses.UserTimeline(user.Id);
    
            <fieldset>
                <legend>Twitter</legend>
                <p>@user.Name (ID: @user.Id)</p>
                <hr />
                @foreach (TwitterStatusMessage status in recent.StatusMessages) {
                    <p>@status.Text</p>
                }
            </fieldset>
        }
    }

    When adding .Body to the TwitterService, I just get a new error on the TwitterTimeline.

  • Anders Bjerner 419 posts 2314 karma points MVP 3x admin c-trib
    Apr 28, 2015 @ 14:10
    Anders Bjerner
    101

    Hi Søren,

    An updated example would look something along:

    @using Skybrud.Social.Twitter
    @using Skybrud.Social.Twitter.Objects
    @using Skybrud.Social.Umbraco.Twitter.PropertyEditors.OAuth
    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @{
    
        // Get the OAuth information stored in the property
        TwitterOAuthData twitter = Model.GetPropertyValue("twitter") as TwitterOAuthData;
    
        // Check whether the OAuth data is valid
        if (twitter != null && twitter.IsValid) {
    
            // Gets an instance of TwitterService based on the OAuth data
            TwitterService service = twitter.GetService();
    
            // Get information about the authenticated user
            TwitterUser user = service.Users.GetUser(twitter.Id).Body;
    
            // Get recent status messages (tweets) from the authenticated user
            TwitterStatusMessage[] recent = service.Statuses.GetUserTimeline(user.Id).Body;
    
            <fieldset>
                <legend>Twitter</legend>
                <p>@user.Name (ID: @user.Id)</p>
                <hr />
                @foreach (TwitterStatusMessage status in recent) {
                <p>@status.Text</p>
                }
            </fieldset>
    
        }
    
    }
    
  • Søren Mastrup 110 posts 495 karma points c-trib
    Apr 28, 2015 @ 14:24
    Søren Mastrup
    0

    Thanks! That did the trick! :)

  • Søren Mastrup 110 posts 495 karma points c-trib
    Apr 28, 2015 @ 15:02
    Søren Mastrup
    0

    How do I format urls and hashtags in a status message?
    On the GitHub repository there is a folder called Formatting, so I assume it is possible?

  • Anders Bjerner 419 posts 2314 karma points MVP 3x admin c-trib
    Apr 28, 2015 @ 18:30
    Anders Bjerner
    0

    You can use the TwitterUtils class for that:

    // Get the response for the status (tweet)
    TwitterStatusMessageResponse response = service.Statuses.GetStatusMessage(statusId);
    
    // Get the tweet from the response
    TwitterStatusMessage tweet = response.Body;
    
    // Write the text on the page
    @Html.Raw(TwitterUtils.FormatEntities(tweet.Text, tweet.Entities))
    

    This will format hashtags, URLs and mentions. The Twitter API doesn't specify any information about emojis in a tweet, so you will have to find a way to deal with this manually.

    Since images are not actually a part of the text, you can grab the first image via:

    if (tweet.Entities.Media.Any()) {
    
        TwitterMediaEntity media = tweet.Entities.Media[0];
    
        <a href="@media.DisplayUrl" target="_blank"><img src="@media.MediaUrl" alt="@media.MediaUrl" /></a>
    
    }
    

    The array will ways be either empty or contain a single image - even if the tweet contains contains multiple images. This is due to the structure of the JSON received from the Twitter API.

    The API has another property containing information about all media/images, but this is currently not supported in Skybrud.Social.

  • Søren Mastrup 110 posts 495 karma points c-trib
    Apr 29, 2015 @ 08:24
    Søren Mastrup
    0

    That did the trick! Thanks! :)

  • Michael Nielsen 99 posts 619 karma points
    Apr 30, 2015 @ 10:27
    Michael Nielsen
    0

    Thanks Anders! :)

Please Sign in or register to post replies

Write your reply to:

Draft