I trying implement Auth0 authentication/authorization in web api. But get 403 error code (No such host is known. (https:443))
Simple code for get token is below
var client = new AuthenticationApiClient(new Uri($"https://{ _config.GetValue<string>("auth0:Authority")}"));
var authenticationResponse = await client.GetTokenAsync(new RefreshTokenRequest
{
Audience = "https://dev-rignosgw.us.auth0.com/api/v2/",
ClientSecret = _config.GetValue<string>("auth0:ClientSecret"),
ClientId = _config.GetValue<string>("auth0:ClientId")
});
But if execute analogous query in postman it is work correctly and i get auth token in response. Query is below
curl --request POST \ --url
'https://dev-rignosgw.us.auth0.com/oauth/token' \ --header
'content-type: application/x-www-form-urlencoded' \ --data
grant_type=client_credentials \ --data client_id=CLIENT_ID \
--data client_secret=CLIENT_SECRET \ --data audience=https://dev-rignosgw.us.auth0.com/api/v2/
Question is folowing. What url must using in AuthenticationApiClient constructor? As I understand in my case it must beeen ttps://dev-rignosgw.us.auth0.com/oauth/token. Or I'm wrong?
I have never used Auth0 before. What i'm doing wrong? Thanks.
I am experimenting with the REST API for sending batch messages in Firebase Cloud Messaging. I prepared a multipart HTTP request in C#:
using var request = new HttpRequestMessage(HttpMethod.Post, "https://fcm.googleapis.com/batch");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "xxx");
request.Content = new StringContent(multicast);
request.Content.Headers.Remove("Content-Type");
request.Content.Headers.TryAddWithoutValidation("Content-Type", "multipart/mixed; boundary=--subrequest_boundary");
var response = await FcmHttpClient.SendAsync(request);
The string value of the multicast field above is an HTTP content similar the one provided in the Firebase documentation:
--subrequest_boundary
Content-Type: application/http
Content-Transfer-Encoding: binary
Authorization: Bearer xxx
POST /v1/projects/myproject/messages:send
Content-Type: application/json
accept: application/json
{
"message":{
"topic":"global",
"notification":{
"title":"FCM Message",
"body":"This is an FCM notification message to device 0!"
}
}
}
--subrequest_boundary
Content-Type: application/http
Content-Transfer-Encoding: binary
Authorization: Bearer xxx
POST /v1/projects/myproject/messages:send
Content-Type: application/json
accept: application/json
{
"message":{
"topic":"readers-club",
"notification":{
"title":"Price drop",
"body":"2% off all books"
}
}
}
--subrequest_boundary--
Firebase server returns Bad Request-400 with error message: "Failed to parse batch request, error: 0 items. Received batch body: --subrequest_boundary--" which indicates that Firebase directly handles the content with the terminating --subrequest_boundary--.
What could be the cause of the problem?
yesterday, i need write bash script to send bath FCM notifaction, and see your code #Ugur, thanks.
now its working, u need change
Content-Type", "multipart/mixed; boundary=--subrequest_boundary
to
Content-Type", "multipart/mixed; boundary=subrequest_boundary
the script
#!/bin/bash
curl \
-X POST \
-H "Authorization: Bearer [token_auth]" \
-H "Content-Type: multipart/mixed; boundary=subrequest_boundary" \
--data-binary #test2.txt \
https://fcm.googleapis.com/batch
and test2.txt, example send 2 notification
--subrequest_boundary
Content-Type: application/http
Content-Transfer-Encoding: binary
Authorization: Bearer [token_auth]
POST /v1/projects/[project_name_firebase]/messages:send
Content-Type: application/json
accept: application/json
{
"message":{
"token":"[token_device]",
"notification":{
"title":"FCM Message",
"body":"This is an FCM notification message to device 0!",
}
}
}
--subrequest_boundary
Content-Type: application/http
Content-Transfer-Encoding: binary
Authorization: Bearer [token_auth]
POST /v1/projects/[project_name_firebase]/messages:send
Content-Type: application/json
accept: application/json
{
"message":{
"token":"[token_device]",
"notification":{
"title":"FCM Message",
"body":"This is an FCM notification message to device 0!",
}
}
}
--subrequest_boundary--
change [project_name_firebase] with name your project in firebase
console example : project_3323.
change [token_device] with token target device.
change [token_auth] with your google auth credentials token.
Notification configuration: https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#AndroidConfig
to get [token_auth]
java :
https://developers.google.com/identity/protocols/oauth2/service-account#java_1
phyton:
https://developers.google.com/identity/protocols/oauth2/service-account#python_2
curl:
https://developers.google.com/identity/protocols/oauth2/service-account#httprest
code for get [token_auth], getToken.sh
#!/bin/bash
client_email="[email_project_firebase]"
service="https://oauth2.googleapis.com/token"
time=3600
scope="https://www.googleapis.com/auth/cloud-platform"
private_key=$(echo -ne "[private_key]")
datenow="$(date +%s)"
expired="$(( datenow + time ))";
header='{"alg": "RS256","typ": "JWT"}'
payload='{"iss": "'$client_email'","scope": "'$scope'","aud": "'$service'","exp": '$expired',"iat": '$datenow'}'
HEADEREnc=$( echo -n "${header}" | openssl base64 | tr -d '=' | tr '/+' '_-' | tr -d '\n' )
PAYLOADEnc=$( echo -n "${payload}" | openssl base64 | tr -d '=' | tr '/+' '_-' | tr -d '\n' )
data="${HEADEREnc}"."${PAYLOADEnc}"
signature=$( openssl dgst -sha256 -sign <(echo -n "${private_key}") <(echo -n "${data}") | openssl base64 | tr -d '=' | tr '/+' '_-' | tr -d '\n' )
JWT="${data}"."${signature}"
jsonDataToken=$(curl -d 'grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion='$JWT https://oauth2.googleapis.com/token)
tokenAuth=$(echo -n $jsonDataToken |grep -Po '"access_token":.*?[^\\]"'|cut -d ':' -f2 |sed 's/"//g')
echo $tokenAuth
[email_project_firebase],[private_key] u can get it from file json in "firebase console" -> project name -> project setting or click gear icon -> account services -> click button "create new secret key"
list scope for other service
https://developers.google.com/identity/protocols/oauth2/scopes
Try to change your code
request.Content.Headers.TryAddWithoutValidation("Content-Type", "multipart/mixed; boundary=--subrequest_boundary");
to
request.Content.Headers.TryAddWithoutValidation("Content-Type", "multipart/mixed; boundary=subrequest_boundary");
The devil's in the details of building the Content. You can't simply create a string matching that payload, because certain pieces of it, like the headers and boundaries, are not technically considered part of the body by the HttpClient stack.
If you really need to do this with a raw HttpClient, take a look at how the Google APIs .NET client builds and sends a batch request. But I suspect that once you realize how cumbersome this is, you're going to conclude that you're best off using a higher-level SDK, such as firebase-admin-dotnet, which is mentioned in the same documentation you linked to and utilizes Google's .NET client under the hood.
I'm making a call to third-party api. Here's the third-party api info and what they expect:
POST /api/ HTTP/1.1
Host: testurl.com
Content-Type: multipart/form-data
curl https://testurl.com/api \
-H "Content-Type: multipart/form-data" \
-F "document[description]=meeting notes" \
-F "document[matter][id]=123" \
-F "document[document_category][name]=Offers" \
-F "document_version[last_modified_at]=2013-12-03T23:35:32+00:00" \
-F "document_version[uploaded_data]=#test.txt"
I need to write a c# equivalent post method to send these info. I have taken care of the header in the curl call, but I'm not sure on the rest of the form-data. For instance, the "document" and "document_version" along with their respective attributes (or whatever they are), how do I pass those info along?
Here's what I found: http://www.briangrinstead.com/blog/multipart-form-post-in-c
I did exactly what's done in that link, but got back Bad Request error. More specific error:
{"error":"api error","message":"undefined method `key?' for nil:NilClass"}
I have no idea what's going on in the third-api so I don't know what this error means. Also, I'm trying to post pdf doc.
You can use the HttpClient
var client = new HttpClient();
var image = File.ReadAllBytes("c:\\test.png");
var formData = new MultipartFormDataContent();
formData.Add(new StreamContent(new MemoryStream(image)), "name","fileName.png");
formData.Add(new StringContent("content"), "name");
var response = client.PostAsync("http://localhost:5001/api/someMethod", formData).Result;
if (!response.IsSuccessStatusCode)
{
Console.WriteLine(response.StatusCode);
}
else
{
var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
Console.WriteLine(content);
}
This works good for me.
I am trying to write a program to authenticate to rackspace cloud files. The following command works with curl just fine:
curl -k -X POST https://identity.api.rackspacecloud.com/v2.0/tokens -d '{ "auth":{"RAX-KSKEY:apiKeyCredentials":{"username":"myusername","apiKey":"mykey"}}}' -H "Content-type: application/json"
However, I get a bad request (400) error with the following code:
var client = new RestClient("https://identity.api.rackspacecloud.com/v2.0");
var request = new RestRequest("tokens", Method.POST);
request.RequestFormat = DataFormat.Json;
string serText = "{ \"auth\":{\"RAX-KSKEY:apiKeyCredentials\"{\"username\":\"myusername\",\"apiKey\":\"mykey\"}}}";
request.AddBody(serText);
RestResponse response = (RestResponse)client.Execute(request);
Anybody have any ideas?
So, when adding my json body I need to do so as follows:
request.AddParameter("application/json", serText, ParameterType.RequestBody);
So basically, it was trying to serialize my already serialized json. I found this out by finding some other questions on stack overflow. I would point out this is not explained at all the official "documentation" for RestSharp.
I'm using curl to send POST request to web service http ://localhost 2325//Service
How can I desirialize body of the POST request into a variable which I could then access within my POST method ?
Can someone give me an example?
This is my method
[WebInvoke(RequestFormat = WebMessageFormat.Json, UriTemplate = "/user", Method = "POST")] public void Create(User us)
Class User contains user_id and user_name.
Can anyone please help?
All I need is an example how to formulate POST request in curl
I've tried this but it doesn't work
curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d ' {"user":{"user_name":"Name1","user:id":"11"}}' http:// localhost :3000/api/1/users
Because you named your parameter us in the method signature your JSON needs to be:
{"us":{"user_name":"Name1","user:id":"11"}}
Alternatively rename your parameter "user" in the method signature.