Microsoft Vision API in Xamarin exception - c#

I was trying this tutorial in Xamarin:
Performing OCR for iOS, Android, and Windows with Microsoft Cognitive Services
But I get an: Exception of type 'Microsoft.ProjectOxford.Vision.ClientException' was thrown.'
It happens in the line:text = await client.RecognizeTextAsync(photoStream);
I have looked at other post (none Xamarin) and they solved by setting the position of the stream to 0. I tried it but still get the same error.
The tutorial was written by Pierce Boggan.
Thanks for any help.
OcrResults text;
var client = new VisionServiceClient("my api key");
using (var photoStream = photo.GetStream())
text = await client.RecognizeTextAsync(photoStream);

It happens in the line:text = await client.RecognizeTextAsync(photoStream); I have looked at other post (none Xamarin) and they solved by setting the position of the stream to 0. I tried it but still get the same error.
Different Areas have different Rest API Url, So in most cases, you need to set your apiRoot manually, which wasn't mentioned the tutorial, you posted. For detailed description, you can refer to Obtain Subscription Keyps.
To do that, you can obtain the API url in your subscription page:
And use the url to construct VisionServiceClient object:
OcrResults text;
var client=new VisionServiceClient("Your API Key", "https://westcentralus.api.cognitive.microsoft.com/vision/v1.0");
using (var stream = photo.GetStream())
...

Related

How to view SOAP fault details from Bing Ads API failure

I have a request I'm making to the Bing Ads API that looks like this:
var _campaignManagementService = new ServiceClient<ICampaignManagementService>(
_authorizationData,
apiEnvironment
);
var getCampaignRequest = new GetCampaignsByIdsRequest
{
AccountId = <the-accountid-is-here>,
CampaignIds = new List<long>() { <the-campaignid-is-here> }
};
var getCampaignResponse = (await _campaignManagementService.CallAsync((s, r) => s.GetCampaignsByIdsAsync(r), getCampaignRequest));
When I run this, an exception is thrown on the last line that reports:
"Invalid client data. Check the SOAP fault details for more information."
Unfortunately, using the API, I'm not seeing the details of the actual response. Is there a way to get at this info? (Short of rewriting the whole thing without using the API?)
Edit:
Thanks to Panagiotis Kanavos I found the solution i think. This is for a different api (reporting) but it should look the same.
The exception has an InnerException that is of type System.ServiceModel.FaultException<Microsoft.BingAds.V13.Reporting.AdApiFaultDetail> or something similar. This has the Detail property with additional information:
Original post:
Can't seem to be able to easily inspect the SOAP request. But you can sniff the request with Fiddler classic The classic version is free.

How to Use application Insights REST API in Visual Studio MVC project

I want to retrieve all the data that is present in my resource on the azure portal. I have found out that there is a REST API for application insights that can help in retrieving the data. What I want is to get the data and generate a grid report on my web page which displays the events related information, that is, date, type, message and all the related information. I haven't worked with REST API's before and what I want as a help is a proper guideline to use this REST API in my MVC based web project in visual studio. If anyone can help will be a great assistance.
You can follow the steps below:
step 1: Get the Application ID and an API key.
Nav to your application insights -> API Access, see the screenshot(Please remember, when the api key is generated, write it down):
step 2: Understand the API Format, for details, refer to here:
Here is an example for get requests count in the last 6 hours:
https://api.applicationinsights.io/v1/apps/your-application-id/metrics/requests/count?timespan=PT6H
This part https://api.applicationinsights.io/v1/apps/ do not need to change.
Then input your-application-id which you get from last step.
Then you can specify metrics or events as per your demand.
This part requests/count, you can refer to this, screenshot below:
The last part ?timespan=PT6H, you can refer to this, screenshot below:
step 3: Write your code to call this api, like below:
public class Test
{
private const string URL_requests = "https://api.applicationinsights.io/v1/apps/your-application-id/metrics/requests/count?timespan=PT6H";
public string GetRequestsCount()
{
// in step 1, you get this api key
string apikey = "flk2bqn1ydur57p7pa74yc3aazhbzf52xbyxthef";
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("x-api-key", apikey);
var req = string.Format(URL_requests);
HttpResponseMessage response = client.GetAsync(req).Result;
if (response.IsSuccessStatusCode)
{
// you can get the request count here
return response.Content.ReadAsStringAsync().Result;
}
else
{
return response.ReasonPhrase;
}
}
}

bing maps, looking for a nearby place

I'm writing application on WP 8.1 in c# and xaml that should return shops that are in some specific radius from owner of the phone. And as I looked through web I found something like points of interest but it only works for North America, I also found some out-of-date ways, but nothing works for now.
I tried this:
string uriToLaunch = #"bingmaps:?&lvl=13&q={nameOfShop}&where={city}"
var uri = new Uri(uriToLaunch);
Windows.System.Launcher.LaunchUriAsync(uri);
but it forces me to open new app and I would prefer putting pins on Map Control
but if there is no other way, is it possible to get a list of found places from this quote to bing maps?
I also tried connecting to google maps api as it has perfect option for this, I checked it from the browser and it worked just fine, but from the application it failes:
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://maps.googleapis.com/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new
MediaTypeWithQualityHeaderValue("application/json"));
// HTTP GET
String uri = "maps/api/place/search/json?key={MyKey}&location="
+ position.Coordinate.Latitude
+ ","
+ position.Coordinate.Longitude +
"&radius=500&keyword={nameOfSho}&sensor=true";
HttpResponseMessage response = await client.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
String content = await response.Content.ReadAsStringAsync();
}
}
I used key for web request, is it correct? There is no specific key for Windows Phone.
Thanks in advance for any suggestion!
Bing Maps has two REST services for getting point of interest data. The first is the NAVTEQ data sources in the Bing Spatial Data Services. These include data for North America and Europe. Here is some information on this service:
https://msdn.microsoft.com/en-us/library/hh478189.aspx
https://msdn.microsoft.com/en-us/library/gg585126.aspx
https://msdn.microsoft.com/en-us/library/hh757509.aspx
I also wrote a free ebook on creating spatial apps for Windows 8. In there is some good information on how to use this service. You can download a copy of the book here: https://rbrundritt.wordpress.com/my-book/
The other service is a legacy SOAP based web service. It includes some data in a lot more countries. You can find documentation on this here:
https://msdn.microsoft.com/en-us/library/dn448599.aspx
https://msdn.microsoft.com/en-us/library/dd221354.aspx

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.

Getting 404 Error during login using oAuth and RestSharp in C# for WP

I'm getting 404- Not Found error during google's authentication in windows phone 7.5 app.
public void login()
{
var cli = new RestClient("https://accounts.google.com/o/oauth2/auth");
cli.Authenticator = new HttpBasicAuthenticator(user, password);
var request = new RestRequest("token", Method.POST);
cli.ExecuteAsync(request, response =>
{
MessageBox.Show(response.Content);
});
}
I given valid login credential, but its shows the response as 404-NotFound. could please help me to resolve this issue
Thanks
For some reason the URI is probably not formed correctly.
If you check the documentation you'll notice an example URI which obviously works if you click on it. That means that the service exists and everything is OK with it. You are doing something wrong with RestSharp, and for that I recommend a detailed tutorial available on CodeProject called Google OAuth2 on Windows Phone

Categories