I'm receiving the following error when trying to upload an image to twitter via LinqToTwitter:
LinqToTwitter.TwitterQueryException: {"request":"/1.1/media/upload.json","error":"media type unrecognized."}
Here is the code I'm using to call the UploadMediaAsync method:
public async Task TweetAsync(string accessToken, string accessTokenSecret, string status, string url)
{
const string mediaType = "image/jpg";
const string mediaCategory = "tweet_image";
byte[] mediaBytes;
using (var webClient = new WebClient())
{
mediaBytes = webClient.DownloadData(url);
}
var twitterContext = new TwitterContext(Authenticate(accessToken, accessTokenSecret));
var media = await twitterContext.UploadMediaAsync(mediaBytes, mediaType, mediaCategory);
await twitterContext.TweetAsync(status, new[] { media.MediaID });
}
The 'Authenticate' method just creates a SingleUserAuthorizer object from stored credentials.
The mediaBytes variable has data in it, and the mediaType and mediaCategory variables are exactly as in the samples provided in the LinqToTwitter documentation: https://github.com/JoeMayo/LinqToTwitter/blob/master/Samples/net46/CSharp/AspNetSamples/CoreDemo/Controllers/StatusDemosController.cs (see 'UploadMediaAsync' method on line 86).
The only difference I see is that the sample loads an image from the file system whereas mine's on a CDN, but since both result in a byte[] I don't think this can be relevant.
I have no idea what it is I'm doing wrong. I have an older project which uses LinqToTwitter v3.1.2 which did not require mediaType or mediaCategory to be provided, and that project works. The issue seems to be with v4.2.1 which does require the parameters.
Turns out the issue was that the URL I'm passing to this function was a page URI and not an image SRC. So the byte[] I was uploading to twitter was not an image.