cURL request in .NET to hide a comment in Facebook page - c#

I read in this answer: facebook api hide comment that you can hide a Facebook comment using this cURL request:
curl -XPOST \
-k \
-F 'is_hidden=true' \
-F 'access_token=[access_token]' \
https://graph.facebook.com/v2.4/[comment_id]
How can I make that request in C#?
Please give a clear working code.

I think the -F flag means it is part of a multipart form: https://ec.haxx.se/http-multipart.html
This may not necessarily work but it should give an idea:
using (var client = new HttpClient())
{
using (var content =
new MultipartFormDataContent("upload----" + DateTime.Now.ToString(CultureInfo.InvariantCulture)))
{
content.Add(new StringContent("true"), "is_hidden");
content.Add(new StringContent("[access_token]"), "access_token");
var response = client.PostAsync(new Uri("https://graph.facebook.com/v2.4/[comment_id]"),
content).Result; // You could use await here
}
}

Related

Using CURL to call WebRequest

Good day,
Been trying to call an API using CURL. The CURL is below. How can i pass the {BVN} inside?
curl --location --request GET 'https://api.paystack.co/bank/resolve_bvn/{BVN}' \
--header 'Authorization: Bearer SECRET_KEY'
The conversion of the CURL is as given below but i have issues passing the BVN parameter inside.
using (var httpClient = new HttpClient())
{ using (var request = new HttpRequestMessage(new HttpMethod("GET"),
"https://api.paystack.co/bank/resolve_bvn/{BVN}"))
{
request.Headers.TryAddWithoutValidation("Authorization", "Bearer SECRET_KEY");
var response = await httpClient.SendAsync(request);
}
}
I am not sure where the input is coming from, but you can do
string.format ("https://api.paystack.co/bank/resolve_bvn/{0}", BVN);
and the code for the string BVN will go into {0}

Converting curl to HtttClient request

Im trying to do this curl request (from API documentation) in HttpClient:
Please not that the code below is just copied from their documentation and does not contain any of my information.
POST /oauth/token (get access token)
$ curl -v -X POST https://api.tink.com/api/v1/oauth/token \
-d 'code=1a513b99126ade1e7718135019fd119a' \
-d 'client_id=YOUR_CLIENT_ID' \
-d 'client_secret=YOUR_CLIENT_SECRET' \
-d 'grant_type=authorization_code'
{
"access_token": "78b0525677c7414e8b202c48be57f3da",
"token_type": "bearer",
"expires_in": 7200,
"refresh_token": "33f10ce3cb1941b8a274af53da03f361",
"scope": "accounts:read,statistics:read,transactions:read,user:read"
}
I've tried to make a post request to the oauth/token endpoint both with all the information requested as header (with an empty body) and as a json document (with and without the headers).
When I only have the information in the headers I get a 401 back, but I've verified that all the data is correct. All other ways that I've tried has generated a 400.
As far as I can understand the json below the curl is the response, but Im not accustomed at all to curl.
There's a handy site here: https://curl.olsh.me/
I wouldn't recommend sending secrets over the web, so be sure to anonymise the values first and translate them back after.
So for the command you've pasted you'd get:
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://api.tink.com/api/v1/oauth/token"))
{
var contentList = new List<string>();
contentList.Add("code=1a513b99126ade1e7718135019fd119a");
contentList.Add("client_id=YOUR_CLIENT_ID");
contentList.Add("client_secret=YOUR_CLIENT_SECRET");
contentList.Add("grant_type=authorization_code");
request.Content = new StringContent(string.Join("&", contentList));
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
var response = await httpClient.SendAsync(request);
}
}
The 401 headers you are getting back may be due to passing in the wrong id and secret, I presume there is a way for you to get test credentials.

C# Windows Form HttpClient cURL

I need an example of sending a POST request to the server and getting a JSON response.
Problem in sending image.
I have a curl:
curl -k -v -X POST
-H "X-Auth-Token: 123"
-H "Content-Type: image / jpeg"
--data-binary # Face_foto.jpg http: // IP: port / 1 / storage / descriptors? estimate_attributes = 1.
How to implement it in C #
This should work:
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("X-Auth-Token", "123");
using (var stream = File.OpenRead(#"c:\somepath\somefile.jpg"))
{
using (var content = new StreamContent(stream))
{
content.Headers.Add("Content-Type", "image/jpeg");
var result = client.PostAsync("https://www.someuri.com", content).Result;
}
}
I wrote this in a hurry so watch out for issues with PostAsync and the File.OpenRead method.

C# GitHub SCIM API // Difference between cURL and HttpClient

I'm facing a strange error with the usage of the GITHUB API.
When I contact them with cURL it's like:
curl.exe -H "Accept: application/vnd.github.cloud-9-preview+json+scim" -H "Authorization: Bearer TOKEN" https://api.github.com/scim/v2/organizations/[ORG]/Users
When I try to take it to C#, if became:
using (var cl = new HttpClient())
{
cl.DefaultRequestHeaders.Add("Accept", "application/vnd.github.cloud-9-preview+json+scim");
cl.DefaultRequestHeaders.Add("Authorization", "Bearer " + "TOKEN");
var val = cl.GetStringAsync("https://api.github.com/scim/v2/organizations/[ORG]/Users").Result;
}
When I run my cURL everything works fine but when I try the same on C# I got a 403 Error.
Could it be related to the "Accept" non standard field?
I find out that the GITHUB API requires to have the User-Agent header Set.
Setting it as "curl" made it.
using (var cl = new HttpClient())
{
cl.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/vnd.github.cloud-9-preview+json+scim"));
cl.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "token");
cl.DefaultRequestHeaders.UserAgent.Add(new System.Net.Http.Headers.ProductInfoHeaderValue("curl", "7.46.0"));
var val = cl.GetStringAsync("https://api.github.com/scim/v2/organizations/[ORG]/Users").Result;
}

How to convert a curl call to C# using HttpClient

How do you convert a curl to be used in C# using HttpClient?
Here is the curl call that I need to use:
Request:
curl -i \
-X POST \
-H "X-Version: 1" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer Your Authorization Token" \
-H "Accept: application/json" \
-d '{"text":"Test Message","to":["Recipients Mobile Number"]}' \
-s \
https://api.clickatell.com/rest/message
I have basic knowledge of HttpClient and I know nothing of curl calls so this is what I could figure from Googling. Below is my code, not sure if it is correct?
using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "abcdefghij0123456789");
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Add("X-Version", "1");
StringContent stringContent = new StringContent("{\"text\":\"Test Message\",\"to\":[\"27936906909\"]}");
stringContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpResponseMessage httpResponseMessage = await httpClient.PostAsync("https://api.clickatell.com/rest/message", stringContent);
if (httpResponseMessage.IsSuccessStatusCode)
{
}
else
{
string failureMsg = "HTTP Status: " + httpResponseMessage.StatusCode.ToString() + " - Reason: " + httpResponseMessage.ReasonPhrase;
}
//string sjson = await httpResponseMessage.Content.ReadAsAsync<string>();
//return sjson;
}
Everything looks correct to be but when I run it then I keep on getting a bad request. I'm not sure how to fix this? Not sure if I missed something from the curl call?
I'll reply the same thing I always reply when someone is trying to debug network issues. Install wireshark, create a session with the traffic you want to mimic, then create another one with your own code. This will allow you to see exactly what is going on and which header are not transmitted correct.

Categories

Resources