This is my first question, so I want to apologize if I am not doing it correctly.
Description
I am developing a C# mobile application using Visual Studio for Mac, and already installed the LinqToTwitter nuget package (version 4.2.1). I need to retrieve all the tweets from an account (for which I already have the credentials). The code I am using is the following:
var auth = new ApplicationOnlyAuthorizer()
{
CredentialStore = new InMemoryCredentialStore {
ConsumerKey = socialMedia.twt_consumer_key,
ConsumerSecret = socialMedia.twt_consumer_secret
}
};
await auth.AuthorizeAsync();
var ctx = new TwitterContext(auth);
var tweets =
await
(from tweet in ctx.Status
where tweet.Type == StatusType.User &&
tweet.ScreenName == socialMedia.twt_screen_name &&
tweet.Count == 30
select tweet)
.ToListAsync();
List<Tweet> list = (from tweet in tweets
select new Tweet
{
StatusID = tweet.StatusID,
ScreenName = tweet.User.ScreenNameResponse,
Text = tweet.Text,
ImageUrl = tweet.User.ProfileImageUrl,
MediaUrl = tweet?.Entities?.MediaEntities?.FirstOrDefault()?.MediaUrl
})
.ToList();
Problem
After making this call I get a list of tweets returned. All of them are associated to the desired account and contain almost all the necessary information that I will use in the app. The issue occurs when trying to access the MediaEntities; from the total 30 tweets returned, only 2 of them contain information and the majority of them are empty; which is the reason of this line:
MediaUrl = tweet?.Entities?.MediaEntities?.FirstOrDefault()?.MediaUrl
What's been tried
I've been looking for quite some time a solution for this issue, but unfortunately I couldn't find any suitable one.
In twitter documentation I found the following about the Media object:
The entities section will contain a media array containing a single media object if any media object has been ‘attached’ to the Tweet. If no native media has been attached, there will be no media array in the entities. For the following reasons the extended_entities section should be used to process Tweet native media: + Media type will always indicate ‘photo’ even in cases of a video and GIF being attached to Tweet. + Even though up to four photos can be attached, only the first one will be listed in the entities section. (https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/entities-object.html)
So what I first did was to look inside extended entities, but the result was the same (they were empty).
Looking into other posts with similar issues I found the following:
Try adding the tweet_mode=extended parameter to your API call. (https://twittercommunity.com/t/media-entities-not-showing-on-most-returned-tweets/77375)
So I added the following code to the query:
&& tweet.TweetMode == TweetMode.Extended
But then I got an exception which I didn't manage to resolve:
My guess is that it has to do with an issue with LinqToTwitter (but can't assure this).
At last I tried including some other lines to the query (which didn't solve the issue):
&& tweet.IncludeEntities == true
&& tweet.IncludeRetweets == true
Questions
Is something wrong in the code? Is the authentication process wrong and it is necessary to include all 4 fields (ConsumerKey, ConsumerSecret, OAuthToken, AccessToken) for what I am trying to achieve? (please note that the connection is successful as the tweet list is returned properly with the exclusion of the media entities).
Maybe I am missing something and the tweets are supposed to be created in a specific way? (By the way, I reviewed the tweets from the account in the twitter page and they all contain media)
The entities section will contain a media array containing a single media object if any media object has been ‘attached’ to the Tweet
Is the workaround to use TweetMode.Extendend? If so, how could the exception shown above be solved?
Thanks in advance to everyone who will take a look into this question.
Finally after a lot of time struggling with this issue the answer was found with the help of Joe Mayo (GitHub). The solution is:
Add the following properties to the tweet query:
tweet.TweetMode == TweetMode.Extended
tweet.IncludeEntities == true
After this, the media entities are returned successfully; enabling the access to the tweet's attached media.
For more information about what was tried, please follow this link. There, the solution suggested by Joe Mayo (GitHub) is explained. Thanks to everyone who took the time to review and help with this issue.