How to "create the video" after file uploaded to DailyMotion using c# - c#

Im following the instructions from here to publish a new video on DailyMotion, using c# and a WebClient.
i successfully got the auth-token, then an upload url, then the actual file to upload. im stuck at step 4, called: "create the video"
it states to POST url=<the url i got from previous step> to https://api.dailymotion.com/me/videos (with the Authorization token in the header), but all my attempts result in "bad request" - without further explanation.
any ideas?
using (var client = new WebClient())
{
var createRequest = $"url={videoUpload.url}";
client.Headers.Add("Authorization", $"Bearer {authToken.access_token}");
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
var createVideo = client.UploadString("https://api.dailymotion.com/me/videos", "POST", createRequest);
}
also tried:
var createRequest = $"url={HttpUtility.UrlEncode(videoUpload.url)}";

I tried your code and my video was created successfully. As explained in our documentation a 400 error is related to a missing/invalid parameter.
I assume you are trying to send the upload url (returned in step 2) instead of the url returned by step 3 (url of your uploaded file).
You can find an article (with examples of returned values) which use a simplified way to upload on Dailymotion here.

Related

404 error when trying to upload crash to hockeyapp

I'm trying to upload crash manually to HockeyApp using public API. When calling the api link using Postman and uploading crash.log file it works fine but when I try to do the same from C# code I get 404 error.
Here is my code:
string log = ""; //log content
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("*/*"));
var content = new MultipartFormDataContent();
var stringContent = new StringContent(log);
stringContent.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("text/plain");
content.Add(stringContent, "log", "crash.log");
var response = await this.client.PostAsync("https://rink.hockeyapp.net/api/2/apps/[APP_ID]/crashes/upload", content);
}
I was using WireShark to analyse the request that Postman is sending and tried to make mine look exactly the same. The only difference I see is that request from C# code has filename* field in Content-Disposition for the attachment while the one from Postman doesn't:
Content-Disposition: form-data; name="log"; filename="crash.log"; filename*=utf-8''%22crash.log%22
It might be worth mentioning that the code is written in portable library in Xamarin project.
Following #Lukas Spieß sugestion I asked the question on HockeyApp support. Apparently they don't handle quotes in the boundary header. The one thing I missed comparing Postman request and mine.
Here is the solution:
var contentTypeString = content.Headers.ContentType.ToString().Replace("\"", "");
content.Headers.Remove("Content-Type");
content.Headers.TryAddWithoutValidation("Content-Type", contentTypeString);

Post Not Publishing to Page via FaceBook API

All I am trying to do is post to a page using the API. This task was extremely simple using Twitter; but with FaceBook, it has been very challenging.
I am using the following code:
string url = #"https://graph.facebook.com/{page_id}/feed?message=Hello&access_token={app_id}|{app_secret}";
WebClient client = new WebClient();
Stream data = client.OpenRead(url);
StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd();
Console.WriteLine(s);
It returns data like this:
{"data":[{"story":"Page updated their cover photo.","created_time":"2017-03-13T22:49:56+0000","id":"1646548358..._164741855..."}...
But, the post is never seen on the page! How can I successfully post from my app to my page?
Your request should be a POST request as the data you're getting back suggests this is a GET request.
Also you need the publish_pages permission to successfully post to a page

Azure Data Lake Store "The access token is not provided in the 'Authorization' header" using HttpClient

I'm having issues trying to access Azure Data Lake Store via the WebHDFS APIs using an HttpClient in .NET
This is my code:
using (var client = new HttpClient())
{
HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Put, resourceUri);
message.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken);
message.Content = new StringContent(JsonConvert.SerializeObject(body));
message.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var result = await client.SendAsync(message);
var content = await result.Content.ReadAsStringAsync();
result.EnsureSuccessStatusCode();
}
Which returns me a 401 with this message: "The access token is not provided in the 'Authorization' header"
The weird thing is if I take the exact accessToken and resourceUri and put it in Postman it works fine (201).
I've added the application under Access control, and in the Data Explorer > Access tab, and have given full rights (Read, Write, Execute).
I even rewrote this using RestSharp yet the same error is given to me, I checked the JWT token it is definitely correct and returning data when I use it in Postman. It seems like the header is being stripped out somewhere.
This is driving me crazy, I can't figure out what I am doing wrong! Can anyone please help?
The issue seems to be related to a redirect that was happening that added an extra parameter added to the operation in the resource URI.
The parameter was "write=true". So I've updated the Resource URI to include the parameter.
i.e. for PUT operation "https://yourstore.azuredatalakestore.net/webhdfs/v1/xxx?op=CREATE&write=true"
Very weird behaviour but it works now.

Using RestSharp to get image response from Cloud Sight C#

I want to call an API called Cloud Sight that provides image recognition.
I want to get a response that basically describes the image from a URL of an image provided from the API Cloud Sight.
This is the code that I Have thus far
var client = new RestClient ("http://api.cloudsightapi.com/image_request");
var request = new RestRequest("http://cdn.head-fi.org/c/c8/1000x500px-c8c39533_beats-by-dre-studio.jpg", Method.POST);
request.AddHeader ("CloudSight", [API KEY HERE]);
IRestResponse response = client.Execute(request);
var content = response.Content;
Console.WriteLine (content);
I get an error that says
{"status":"404","error":"Not Found"}
The documentation for Cloud Sight is not very insightful for each individual language, so I am unsure if I am calling it correctly, particularly, the AddHeader part.
It may also be an error with not waiting for a response. My code executes immediately and the API example that Cloud Sight provides on their website takes 10-15 seconds.
Any ideas for how to go about getting this API working with RestSharp?
Just a guess, but have you tried Method.GET instead of Method.POST? It'd be highly unusual to fetch an image via a POST.

Exception in BackgroundUploader

I am trying to upload some avi file to server. It works fine with HttpRequest but i need to continue uploading even if i suspend app so thats why i am trying to use BackgroundUploader. I am following this guideline on msdn http://msdn.microsoft.com/en-us/library/windows/apps/jj152727.aspx. So my code looks something like this.
StorageFile storageFile = KnownFolders.VideosLibrary.GetFileAsync("fileName");
BackgroundUploader uploader = new BackgroundUploader();
uploader.Method = "POST";
uploader.SetRequestHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
var fs = await storageFile.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
IInputStream aaaa = fs.GetInputStreamAt(0);
UploadOperation upload = uploader.CreateUploadFromStreamAsync(new Uri("uploadUri"), aaaa);
await HandleUploadAsync(upload, true);
the rest is same as on MSDN. And i am getting exception Unsupported media type (415) in method HandleUploadAsync on line
await upload.StartAsync().AsTask(cts.Token, progressCallback);
What am i doing wrong? Or what can cause this kind of exception?
EDIT : I solved my problem as i commented down here and in my answer. I think at the end i am basically just sending some data to server that are recognized and interpreted as i want to. So if i use BackgroundUploader i am not only uploading some file i am also sending information about how am i doing that(as i mentioned in my answer). So by the same idea i can also upload folder to server and by that i am not sending any actual content only some description about how to do that. And if i compare request that i am making by HttpRequest and BackgroundUploader they are equal and thats what i wanted.
So the problem part was the header of request. I have some header in my request that is recognized by server and i was trying to put it to BackgroundUploader through SetRequestheader method but it did not work. As Kieqic suggested i used Fiddler and by that i compare request made by HttpRequest and BackgroundUploader. I found out they are completely different. So through SetRequestheader i add some parts like expected Content-Type and for the rest parts of header to make them equal i add it before content of my file as array of bytes. And this works so conclusion is in my case using Fiddler that helped my how to construct request header.

Categories