Background information:
I'm trying to create a PoC for Google Cloud Vision API using their .NET library.
What I have done:
Create a simple console apps with the following code for Vision API.
GoogleCredential credential = GoogleCredential.FromFile(ConfigurationManager.AppSettings["GoogleCredentialFile"]);
Grpc.Core.Channel channel = new Grpc.Core.Channel(Google.Cloud.Vision.V1.ImageAnnotatorClient.DefaultEndpoint.ToString(), credential.ToChannelCredentials());
var client = Google.Cloud.Vision.V1.ImageAnnotatorClient.Create(channel);
var image = Google.Cloud.Vision.V1.Image.FromFile(#"C:\Users\u065340\Documents\sample.jpg");
var response = client.DetectLabels(image);
foreach (var annotation in response)
{
if (annotation.Description != null)
result = annotation.Description;
}
Problem:
The line client.DetectLabels(image) gets stuck for a long time before ultimately throwing the error Deadline Exceeded.
My code sits behind a corporate proxy, but I have validated that it is not blocking internet access because I can call https://vision.googleapis.com/$discovery/rest?version=v1 from the same apps and get its JSON response just fine.
Any suggestions?
After digging around through github issues related to proxies as suggested by Jon Skeet, I found that Google Cloud Client APIs can be generally divided into 2 categories (Ref: here): REST-based HTTP 1.1 with JSON and gRPC.
For APIs associated as REST-based, there should be no issue with proxies. The problem starts to appear when we are using gRPC-based APIs such as Google Cloud Vision and Google Speech. In gRPC, we need to explicitly provide our proxy server information.
For those using Java Client, it seems we still can't set proxy properly because it will eventually be ignored, and causing the Deadline Exceeded error. This issue is already well known and can be found at here and further traced into here.
The Google team has determined that it is indeed a bug, and the status remains Open.
As for C# Client, we can set proxy information using gRPC Environment Variables which is documented in here. The code is Environment.SetEnvironmentVariable("http_proxy", <your_proxy_server>);
After I set the http_proxy environment variable pointing to my proxy server, all is well again. I get the expected output "This API needs Billing Account".
Many thanks to Jon Skeet for pointing me in the right direction :D
Related
I am trying to get THIS example to work (.Net Client Libraries example) - however everything I have attempted results in an error:
Basic authentication requires a secure connection to the server.
There is another example using the REST Api at the top of the page I linked and this works perfectly fine. For some reason, I just cant get this working using the libraries!
My code looks like this:
Uri uri = new Uri("http://adtfs:8080/tfs/{MyCompany}");
string personalAccessToken = "MyPATString";
VssBasicCredential credentials = new VssBasicCredential("", personalAccessToken);
using (ProjectHttpClient projectHttpClient = new ProjectHttpClient(uri, credentials))
{
IEnumerable<TeamProjectReference> projects = projectHttpClient.GetProjects().Result;
}
As I mentioned, using the same URL and PAT in the REST API example works fine, but for the libraries, I just cant get beyond the error mentioned above.
Am I missing something or can anyone suggest anything else I could try please?
Change http=>https from http://adtfs:8080/tfs/{MyCompany} to https://adtfs:8080/tfs/{MyCompany} ... easiest answer there was I guess works glad it helped ... but just as precautionary tale, I'll add this for posterity, you should use https anyways if the server supports it (had an app that was working sometimes slow, sometimes fast and I couldn't figure out why until I saw this https://httpvshttps.com, turns out the https tunnel was always being recreated cause I put http instead of https and the server was set to always switch to https).
I'm makin an API with net core 2.1 and run into a issue where I CAN gen a correct response in my local machine (even running the published files) BUT gen stuck when i upload and run the app in a aws ec2 windows server 2012.
I've tried using the IHttpClientFactory, adding the httpclient at services collection, making a service whith the AddHttpClient and running the release profile in my pc and with every method it runs fine in my pc but hangs in aws ec2 windows server 2012.
I'm running the app in aws ec2 with $netcore ./application.dll for now.
my pc has netcore 2.2.300 and aws 2.2.400
Here is the part of the code I use in my controller (also try with making a service):
WebCuitRequest req = new WebCuitRequest {cuit = "cuit", token = "Yt25zBH" };
string json = JsonConvert.SerializeObject(req);
string baseUrl = "url.com/getthis";
StringContent queryString = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage res = await _client.PostAsync(baseUrl, queryString);
//------i dont get here (no error, no response)
HttpContent content = res.Content;
string data = await content.ReadAsStringAsync();
return data;
I expect an error or response but none is returned (i have a try/catch and some Console.write that never get reached).
Update 1 (solution):
Moving the API I was trying to reach to a hosting without cloudfare solved the problem. It seems that cloudfare was blocking the aws instance (but no my pc) and for some reason I didn't get a response or error. I must clarify that I have access to the target API and the posibility to move it, I don't know what can be done if you can't do changes in that env.
Tanks to Robert Perry who get me to evaluate another part on the situation.
This is most likely caused by Azures' default security settings. I believe it locks out calls to external IP addresses by default - its worth checking this article and matching it to how your Azure is configured: https://learn.microsoft.com/en-us/azure/storage/common/storage-network-security
Moving the API I was trying to reach to a hosting without cloudfare solved the problem.
It seems that cloudfare was blocking the aws instance (but no my pc) and for some reason I didn't get a response or error.
I must clarify that I have access to the target API and the posibility to move it, I don't know what can be done if you can't do changes in that env.
I'm coming to .net web api from a JavaScript background, and I'm trying to make a proxy to help with a cross domain JSON request. I'm GETing from a server I don't control the source code for, so I can't configure CORS directly. Likewise, it doesn't speak JSONP.
So two questions as I try to get my head around Web API:
1) Is Httpclient the right tool for this job? (if not, what is?)
2) If httpclient IS the right tool, what is an absolute bare bones httpclient config so I can test this out? Not worried about throwing exceptions or anything else other than just GETing API data and feeding it to a jQuery client.
I guess one other piece of information that would be nice would be building username / password authentication into the http request.
Any help is much appreciated, as are links to any good blogs / tutorials / etc that might help as an introduction to this sort of thing. I've watched several today alone, and I'm still not able to get a basic http request going on the server side without resorting to cutting / pasting other people's code.
Thanks in advance!
** EDIT - To make this question a bit more clear, what I'm trying to test is 1) Can the proxy connect to the third party server, which involves authentication via a username and password 2) Can the proxy then respond to the jQuery client request with the JSON data it received from the third party server.
Thanks to all who have taken the time to respond.
HttpClient seems to be ok in this job.
About the minimal config- it depends on what the third party expects. In most cases would work out-of-the-box, but there always may be some minor tweaks like headers and/or auth code.
I have just found some blog entry where some author shows how to test such a proxy and shows the proxy code too. Please see: http://www.davidbreyer.com/programming/2014/10/11/create-fake-responses-to-rest-service-calls-in-c/
You can find info about sending credentials here: How to use credentials in HttpClient in c#?
HTH
EDIT:
this sample code should work (copied from blog above and modified):
public class Proxy
{
public async Task<ExampleDto> GetExample(int id)
{
var client=new HttpClient();
//set some auth here
//set other headers
var response = client.GetAsync(
string.Format("/api/restserviceexample/{0}", id))
.Result.Content.ReadAsAsync<ExampleDto>();
return await response;
}
}
It's so simple that you can just run it and see if the other server responds. If not, you can play with headers - since all the session info and user auth info are sent using ookies and/or headers, all you have to do is to see how it's made with regular browser and then fake it on the server. Probably best tool for this job will be Fiddler.
However - there is one thing to consider. If the other service has special method for authorization (other than passing credentials with each request) the whole thing becomes tricky, since your proxy should perform authorization using their service, then store their auth cookie on the server or propagate them to the browser and attach them with all next requests.
First, you don't need ASP.NET with C# if you really want minimal.
.NET has great http handling without ASP. Check out classes like HttpListener, HttpListenerContext, HttpListenerRequest, etc... Yes, you'll have to write some boilerplate as your application, but these classes are pretty good.
See among others:
http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=599978
Second, if you want user & password, I'd checkout using oauth authentication so you don't have to deal with them directly. Google Plus, Windows Live, Facebook, etc... all have similar OAuth 2.0 APIs for that. See among others:
http://msdn.microsoft.com/en-us/library/dn659750.aspx
https://developers.google.com/+/web/signin/server-side-flow
https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow/v2.2
I have successfully integrated my company's system with DocuSign using DocuSign's SOAP API. I can send, check status and retrieve Envelopes through the SOAP interface.
I have read that the preferred method of getting Envelope status is through an event. Unfortunately I haven't had any luck finding an example of this.
I found some documentation about it HERE.
Has anyone used this way of event / notification from DocuSign that would help point me in the right direction?
There are examples of it on DocuSign's own Lithium forums (which will be made read-only soon) in PHP for example. They're pretty easy to setup, you just need a server listening for the events with the rights ports open and you just add the eventNotification element to your request. You've referenced the SOAP api guide, which the sample PHP code below shows how to implement. There's also a version available for REST API.
You can download DocuSign's SOAP SDK out of GitHub and there's sample PHP project ready of out of the box for you to start modifying and adding in eventNotifications.
// Notifications
$eventNoti = new EventNotification();
$eventNoti->URL = 'http://myurl.com/docusign/updateDocStatus'.$env_id.'/';
$eventNoti->LoggingEnabled = "TRUE";
// Important Stuff below
$envEvent = new EnvelopeEvent();
$envEvent->EnvelopeEventStatusCode = "Completed"; // <---------- Fires on "Completed" only
$envEvent->IncludeDocuments = "TRUE";
$eventNoti->EnvelopeEvents = array($envEvent); // <------------ Add multiple EnvelopeEvent's
$envInfo->EventNotification = $eventNoti;
This link is where the above code is referenced from, along with further discussion that might help.
Another option is to use the DocuSign Connect module to push events to your external listener. The main difference between DocuSign Connect and the eventNotification is that eventNotification is per envelope, Connect is account wide and or user-wide.
I'm trying set up paypal express checkout using SOAP 2.0 API in ASP.NET C# code. First I try to use sandbox, I created seller/buyer test accounts, imported web service and then I try to get token, in my C# code I have:
// Create the request object
SetExpressCheckoutRequestType pp_request = new SetExpressCheckoutRequestType();
// Create the request details object
pp_request.SetExpressCheckoutRequestDetails = new SetExpressCheckoutRequestDetailsType();
pp_request.SetExpressCheckoutRequestDetails.PaymentAction = paymentAction;
pp_request.SetExpressCheckoutRequestDetails.PaymentActionSpecified = true;
pp_request.SetExpressCheckoutRequestDetails.OrderTotal = new BasicAmountType();
pp_request.SetExpressCheckoutRequestDetails.OrderTotal.currencyID = currencyCodeType;
pp_request.SetExpressCheckoutRequestDetails.OrderTotal.Value = paymentAmount;
pp_request.SetExpressCheckoutRequestDetails.CancelURL = cancelURL;
pp_request.SetExpressCheckoutRequestDetails.ReturnURL = returnURL;
SetExpressCheckoutResponseType response = (SetExpressCheckoutResponseType) caller.Call("SetExpressCheckout", pp_request);
but on the last line of that code it throws an error:
The request was aborted: Could not create SSL/TLS secure channel.
That I'm doing wrong?
Thanks.
Your code certainly seems correct but the PayPal API can be finicky when it comes to a few things. One thing to look out for is that it will generate exceptions when the payment amount is not rounded to 2 decimal places - can you try ensuring this is the case?
Also ensure that your configuration values are correct. Aside from that the code you have posted is exactly what I used to use for the SOAP API.
I stopped using the SOAP API a while ago in favour of the NVP API, which in my mind is a bit easier to deal with: https://cms.paypal.com/uk/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_nvp_NVPAPIOverview
I made available a library to do all the work for you: https://github.com/davidduffett/Moolah
The instructions here show exactly how to use PayPal Express Checkout: https://github.com/davidduffett/Moolah#paypal-express-checkout