how can get message from mail.tm
i have api can create email and can get token to sign in in website but when make api to get message it didn't get
HttpWebRequest hwq = (HttpWebRequest)WebRequest.Create("https://api.mail.tm/messages?page=1");
hwq.Method = "GET";
hwq.Accept = "application/ld+json";
hwq.Headers.Add(HttpRequestHeader.Authorization, "Bearer "+"Token");
var res = hwq.GetResponse();
using (var sr = new StreamReader(res.GetResponseStream()))
{
dynamic dd = JsonConvert.DeserializeObject(sr.ReadToEnd());
}
it run but get empty like it
{{"#context": "/contexts/Message","#id": "/messages", "#type": "hydra:Collection","hydra:member": [],"hydra:totalItems": 0}}
this code run in first code i send header "Bearer" then Token but i must send header like "Bearer " + "Token"
i must send it with space after "Bearer" then Token
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpWebRequest hwq = (HttpWebRequest)WebRequest.Create("https://api.mail.tm/messages?page=1");
hwq.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + "Token");
hwq.Method = "GET";
hwq.Accept = "application/json";
var res = hwq.GetResponse();
using (var sr = new StreamReader(res.GetResponseStream()))
{
string a1 = sr.ReadToEnd();
}
Related
I have 2 programs on my localhost, one with REST API and another program that calls the api.
I'm trying to authenticate user(https://localhost:44301/token) with below function and getting badRequest.
the same is working when testing with postman.
baseUrl is "https://localhost:44301/";
static async Task<Uri> AddCartRecordAsync(CartsTable cartsTable)
{
string ResponseString = "";
HttpWebResponse response = null;
var request = (HttpWebRequest)WebRequest.Create(Utility.baseUrl + "token");
request.Accept = "application/json";
request.Method = "POST";
//Get credentials from config.
var username = "kkk#gmail.com";
var password = "Test123!";
Credentials cred = new Credentials()
{
username = username,
password = password,
grant_type = "password"
};
var myContent = JsonConvert.SerializeObject(cred);
var data = Encoding.ASCII.GetBytes(myContent);
request.ContentType = "application/json";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
try
{
using (response = (HttpWebResponse)request.GetResponse())//BadRequest Here
{
ResponseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
}
}
catch (Exception e)
{
string i = "df";
}
The common issues are :
1-Params Body Type
2-Authentication Type : if u have bearer type you should include bearer keyword before your token in the header before sending request like "token":"bearer AuthToken"
3-method type : get put post delete
After many struggles, I was finally able to get the OAuth Authentication/Refresh token process down. I am certain that the tokens I am using in this process are good. But I am struggling to communicate with the Compliance API and I think it may have more to do with my headers authentication process than it does specifically the Compliance API but I am not certain. I've tried so many different combinations of the below code unsuccessfully. I've tried to do the call as a GET and a POST (the call should be a GET). I've tried it with the access token encoded and not encoded. With all of my different code combinations tried I've been getting either an authorization error or a bad request error. You can see some of the various things I've tried from commented out code. Thank you for your help.
public static string Complaince_GetViolations(string clientId, string ruName, string clientSecret, string accessToken, ILog log)
{
var clientString = clientId + ":" + clientSecret;
//byte[] clientEncode = Encoding.UTF8.GetBytes(clientString);
//var credentials = "Basic " + System.Convert.ToBase64String(clientEncode);
byte[] clientEncode = Encoding.UTF8.GetBytes(accessToken);
var credentials = "Bearer " + System.Convert.ToBase64String(clientEncode);
var codeEncoded = System.Web.HttpUtility.UrlEncode(accessToken);
HttpWebRequest request = WebRequest.Create("https://api.ebay.com/sell/compliance/v1/listing_violation?compliance_type=PRODUCT_ADOPTION")
as HttpWebRequest;
request.Method = "GET";
// request.ContentType = "application/x-www-form-urlencoded";
//request.Headers.Add(HttpRequestHeader.Authorization, credentials);
//request.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + codeEncoded);
request.Headers.Add(HttpRequestHeader.Authorization, credentials);
//request.Headers.Add("Authorization", "Bearer " + codeEncoded);
request.Headers.Add("X-EBAY-C-MARKETPLACE-ID", "EBAY-US");
log.Debug("starting request.GetRequestStream");
string result = "";
var response = (HttpWebResponse)request.GetResponse();
using (var streamReader = new StreamReader(response.GetResponseStream())) //FAILS HERE
{
result = streamReader.ReadToEnd();
}
//DO MORE STUFF BELOW
return "STUFF";
}
And I finally figured out a resolution to this problem. The HTML encoding of the entire bearer string was the issue. If anyone needs this in the future your welcome. =)
HttpWebRequest request = WebRequest.Create("https://api.ebay.com/sell/compliance/v1/listing_violation?compliance_type=PRODUCT_ADOPTION")
as HttpWebRequest;
request.Method = "GET";
request.Headers.Add(HttpRequestHeader.Authorization, System.Web.HttpUtility.HtmlEncode("Bearer " + accessToken));
request.Headers.Add("X-EBAY-C-MARKETPLACE-ID", "EBAY-US");
log.Debug("starting request.GetRequestStream");
string result = null;
var response = (HttpWebResponse)request.GetResponse();
using (var streamReader = new StreamReader(response.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}
I'm a C# developer I need to use webhooks to get some stuff after the gethostpage with redirect.
Everything it's fine if I use GET ( get events, get my webhooks ), but when I'm going to create a new webhook I get a "The remote server returned an error: (400) Bad Request." for sure it's a stupid thing but I'm stuck.
Any tips?
The request
byte[] encoded = System.Text.Encoding.Default.GetBytes(apiLogin + ":" + transactionKey);
string base64 = System.Convert.ToBase64String(encoded);
var isPost = !string.IsNullOrWhiteSpace(json);
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "application/json; charset=utf-8";
httpWebRequest.Method = isPost ? "POST" : "GET";
httpWebRequest.Headers.Add("Authorization", "Basic " + base64);
httpWebRequest.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
if (isPost)
{
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(json);
streamWriter.Flush();
}
}
string result = null;
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
return result;
}
return result;
I'm trying the JSON sample from documentation sample
Found, it is need to create a signature in merchant panel before use "post" webhooks, "get" works also without doing it
I am trying to call a locally hosted WCF REST service over HTTPS with basic auth.
This works and the Authorization header comes thru just fine and all is happy:
ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertficate;
var request = (HttpWebRequest)WebRequest.Create("https://localhost/MyService/MyService.svc/");
request.Method = "GET";
request.ContentType = "application/json";
request.Headers.Add(
System.Net.HttpRequestHeader.Authorization,
"Basic " + this.EncodeBasicAuthenticationCredentials("UserA", "123"));
WebResponse webResponse = request.GetResponse();
using (Stream webStream = webResponse.GetResponseStream())
{
if (webStream != null)
{
using (StreamReader responseReader = new StreamReader(webStream))
{
string response = responseReader.ReadToEnd();
}
}
}
When I try to use RestSharp however, the Authorization header never comes thru on the request:
ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertficate;
var credentials = this.EncodeBasicAuthenticationCredentials("UserA", "123");
var client = new RestSharp.RestClient("https://localhost/MyService/MyService.svc/");
var restRq = new RestSharp.RestRequest("/");
restRq.Method = Method.GET;
restRq.RootElement = "/";
restRq.AddHeader("Authorization", "Basic " + credentials);
var restRs = client.Execute(restRq);
What am i doing wrong with the RestSharp method?
I know that the AddHeader method works because this:
restRq.AddHeader("Rum", "And Coke");
will come thru, only "Authorization" seems stripped out/missing.
instead of adding the header 'manually' do the following:
var client = new RestSharp.RestClient("https://localhost/MyService/MyService.svc/");
client.Authenticator = new HttpBasicAuthenticator("UserA", "123");
I used milano's answer to get my REST service call to work (using GET)
Dim client2 As RestClient = New RestClient("https://api.clever.com")
Dim request2 As RestRequest = New RestRequest("me", Method.GET)
request2.AddParameter("Authorization", "Bearer " & j.access_token, ParameterType.HttpHeader)
Dim response2 As IRestResponse = client2.Execute(request2)
Response.Write("** " & response2.StatusCode & "|" & response2.Content & " **")
The key was making sure there was a space after the word 'Bearer' but this may apply to any type of custom token authorization header
You have to use ParameterType.HttpHeader parameter:
request.AddParameter("Authorization", "data", ParameterType.HttpHeader);
I was able to get the response from my rest API using this piece of code:
My API was returning server error and I used:
request.AddHeader("Authorization", $"Bearer {accessToken}");
var request = new RestRequest("/antiforgerytokensso", Method.Get);
restClient.Authenticator = new JwtAuthenticator(accessToken);
var response = await restClient.ExecuteAsync(request);
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
in php i post a request like
$.post("/service.php?cat=c1", {
group: $this.attr('href'),
})
where group -> #$!/mycat/year,2012
now i want to do same request in c#
var httpWebRequest = (HttpWebRequest)WebRequest.Create( url);
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}
I tried
String url = domain + "./service.php?cat=c1&group=%22#$!/mycat/year,2012%22";
and
String url = domain + "./service.php?cat=c1&group=mycat&year=2012";
but "message" always returns empty
What is the problem here?
You can use the WebClient class, as this is easier to use.
Pass in the values as NameValueCollection object
var client = new WebClient();
var nameValueCollection = HttpUtility.ParseQueryString("cat=c1&group=mycat&year=2012");
var response = client.UploadValues(domain + "/service.php","POST",nameValueCollection);
var responseStr = Encoding.ASCII.GetString(response);