Microsoft Emotions API Get Recognition in Video Operation Result - c#

How do I return the status and results from a video I posted to the emotions api using Get Recognition in Video Operation Result?
I can successfully run the Get Recognition in Video Operation Result using an operation id of a video I uploaded, but the response does not show the status of the video and the results from the emotions API but only shows the following:
StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent,
Headers:{
Pragma: no-cache
apim-request-id: 010962c0-f907-4ba3-a7fd-564ddff7f97d
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
Cache-Control: no-cache
Date: Wed, 02 Nov 2016 02:33:16 GMT
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Content-Length: 16048
Content-Type: application/json; charset=utf-8 Expires: -1 }
I've tried using the same oid in the open api testing console
(https://dev.projectoxford.ai/docs/services/5639d931ca73072154c1ce89/operations/56f8d4471984551ec0a0984f/console)
and the response returns the status and the results from the emotions api.
I'm using the following code from the API reference:
var client = new HttpClient();
var queryString = HttpUtility.ParseQueryString(string.Empty);
// Request headers
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "{subscription key}");
var uri = "https://api.projectoxford.ai/emotion/v1.0/operations/{oid}?" + queryString;
var response = await client.GetAsync(uri);

Based on what you've pasted, it looks like you do have a response, 16048 bytes of JSON.
Since you're using C#, I think you'll save a lot of effort by using the client library, authored by members of the Cognitive Services team. If you use Visual Studio, you can simply pull down the NuGet package.
If you don't want to use the prebuilt library, you'll need to parse out the JSON yourself. The JSON will be in response.Content.

Related

Respond with SUCCESS in header reply

I am responding to a GET request from a field device with the following:
var reply = new HttpResponseMessage(System.Net.HttpStatusCode.OK)
{
Content = new StringContent("SUCCESS")
};
The word appears in the message of the body. But the field device is saying that it is not successful.
From this, I can gather that I shouldn't be using HttpResponseMessage but some other means.
The suggested reply needs to look like this:
HTTP/1.1 200 OK<CR><LF>
Date: Mon, 15 Feb 2016 11:34:50 GMT<CR><LF>
Server: Apache/2.2.31 (Win32) mod_ssl/2.2.31 OpenSSL/1.0.2f PHP/5.4.45<CR><LF>
X-Powered-By: PHP/5.4.45<CR><LF>
Content-Length: 7<CR><LF>
Keep-Alive: timeout=5, max=100<CR><LF>
Connection: Keep-Alive<CR><LF>
Content-Type: text/plain<CR><LF>
<CR><LF>
SUCCESS<CR><LF>
BTW I am using Microsoft for this not PHP, this is from the manual.
Should I be using HttpRequestMessage ?
The part I am concerned with is <CR><LF>SUCCESS<CR><LF>
So the issue was that the device required a time sync with my server before it could send data later on. So there never was an issue with the response.

Related to Get service call

I just created a HTTP get request to get the content(All the Badges) from stack overflow for my console application as shown below :
public void getStackBadges()
{
var client = new HttpClient();
client.BaseAddress = new Uri("https://api.stackexchange.com/docs//badges?order=desc&sort=rank&site=stackoverflow");
var res = client.GetAsync(client.BaseAddress).Result;
Console.WriteLine(res);
}
Can anybody please tell if i want to get all the badges from stack overflow using this API what i need to do. I don't realy understand the format of result that i am getting on my Cmd prmt !
Output on Console:
StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST
Access-Control-Allow-Credentials: false
X-Content-Type-Options: nosniff
Cache-Control: private
Date: Thu, 28 Dec 2017 09:49:33 GMT
Content-Length: 880
Content-Encoding: gzip
Content-Type: application/json; charset=utf-8
}
You're querying the documentation of the API and you're most likely getting the page markup.
You should be querying: https://api.stackexchange.com/2.2/badges?order=desc&sort=rank&site=stackoverflow
The format of the output in above API method is JSON.
Few things to note in the output:
quota_max - Tells you how many requests can be made by you in a day.
quota_remaining - How many requests more you can make today.
has_more - If there is another page with badges.
To change the page you append a &page= parameter to the url.
So your query for page 2 would look like this:
https://api.stackexchange.com/2.2/badges?page=2&order=desc&sort=rank&site=stackoverflow
Edit:
As I said, the API I have linked is correct, your problem is the way you try to display the content of the response from the API.
.Result is not what you think it is. .Result returns Task<TResult> which is not the response from the API but the result of the request. That's why you have status code of the request, response type, etc etc.
Here's how to retrieve the response text. This is a sample, you'll need to do your own processing if you want to access different properties of the response objects. This is a separate question though and it's outside the scope of this one.
var response = client.GetAsync("https://api.stackexchange.com/2.2/badges?order=desc&sort=rank&site=stackoverflow").Result;
string res = "";
using (HttpContent content = response.Content)
{
Task<string> result = content.ReadAsStringAsync();
res = result.Result;
}

(Instagram API) Trouble Reaching Tag Endpoints

I'm trying to gather a list of recent posts that contain a certain hashtag. The API Documentation states that I should be using the following GET call:
https://api.instagram.com/v1/tags/{tag-name}/media/recent?access_token=ACCESS-TOKEN
When I load the page where I want this information displayed, I perform the following:
using(HttpClient Client = new HttpClient())
{
var uri = "https://api.instagram.com/v1/tags/" + tagToLookFor + "/media/recent?access_token=" + Session["instagramaccesstoken"].ToString();
var results = Client.GetAsync(uri).Result;
// Result handling below here.
}
For reference, tagToLookFor is a constant string defined at the top of the class (eg. foo), and I store the Access Token returned from the OAuth process in the Session object with a key of 'instagramaccesstoken'.
While debugging this, I checked to make sure the URI was being formed correctly, and it does contain both the tag name and the just-created access_token. Using Apigee with the same URI (Save for a different access_token) returns the valid results I would expect. However, attempting to GET using the URI on my webstie returns:
{
StatusCode: 400,
ReasonPhrase: 'BAD REQUEST',
Version: 1.1,
Content: System.Net.Http.StreamContent,
Headers:{
X-Ratelimit-Remaining: 499
Vary: Cookie
Vary: Accept-Language
X-Ratelimit-Limit: 500
Pragma: no-cache
Connection: keep-alive
Cache-Control: no-store, must-revalidate, no-cache, private
Date: Fri, 27 Nov 2015 21:39:56 GMT
Set-Cookie: csrftoken=97cc443e4aaf11dbc44b6c1fb9113378; expires=Fri, 25-Nov-2016 21:39:56 GMT; Max-Age=31449600; Path=/
Content-Length: 283
Content-Language: en
Content-Type: application/json; charset=utf-8
Expires: Sat, 01 Jan 2000 00:00:00 GMT
}
}
I'm trying to determine what the difference between the two could be; the only thing that I can think of is that access_token is somehow being invalidated when I switch between pages. The last thing I do on the Login/Auth page is store the access_token using Session.Add, then call Server.Transfer to move to the page that I'm calling this on.
Any Ideas on what the issue could be? Thanks.
Attach the token to the header when making the request.
Client.DefaultRequestHeaders.Add("access_token", "Bearer " + token);
The problem ended up being one regarding Sandbox Mode. I had registered an app after the switch, and I was the only user in my sandbox. As a result, it had no problem finding my posts/info, but Sandbox Mode acts as if the Sandbox users are the only users on Instagram, so naturally it would not find anything else.
It turns out there was an existing registered application in my organization (made before the switch date) that does not have any such limitations, so I have been testing using that AppID/secret.
tl;dr: If you're the only user in your app's sandbox, work on getting users into your sandbox. See their article about it for more info.

Google contacts api (gdata) syncs low resolution photos

I'm using google contacts api (gdata) to set a contact's photo in google contacts.
I'm using fiddler and I see that the request is sent according to Google Contacts Examples but the photo downloaded back from google is always 96x96.
The code I'm using to update and download the photo is:
public void UpdateUserPhoto(Contact contact, Stream photo)
{
_contactsRequest.SetPhoto(contact, photo);
}
public static void DownloadPhoto(ContactsRequest cr, Contact contact)
{
if (contact.PhotoEtag == null)
return;
Stream photoStream = cr.Service.Query(contact.PhotoUri);
FileStream outStream = File.OpenWrite(string.Format(#"c:\friends\{0}.jpg",contact.Name.FullName));
byte[] buffer;
using (var memoryStream = new MemoryStream())
{
photoStream.CopyTo(memoryStream);
buffer = memoryStream.ToArray();
}
outStream.Write(buffer, 0, buffer.Length);
photoStream.Close();
outStream.Close();
}
I tried syncing the contacts to my phone and there too, the size was always limited to 96x96.
Am I doing something wrong or does google not allow syncing more than 96x96? I can see many apps that do sync contacts with more than 96x96 then I guess it's possible, but what is the right way?
Edit
Here are the sync & retrieval of the photo as captured by fiddler:
Sync photo request:
PUT https://www.google.com/m8/feeds/photos/media/mymail#gmail.com/55f3484e8aaf1c82 HTTP/1.1
Etag: "SomeEtag"
If-Match: "SomeEtag."
Content-Type: image/jpg
User-Agent: G-GoogleContactsSync/GOAuth2RequestFactory-CS-Version=2.2.0.0
Authorization: Bearer myAuthorization
GData-Version: 3.0
Host: www.google.com
Content-Length: 34480
Sync photo response
HTTP/1.1 200 OK
Content-Type: application/atom+xml; charset=UTF-8; type=entry
GData-Version: 3.1
ETag: "KgxxHGIyfCt7I2BoA047FShUNFU3BWx8RDQ."
Date: Wed, 01 Oct 2014 20:13:06 GMT
Expires: Wed, 01 Oct 2014 20:13:06 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Alternate-Protocol: 443:quic,p=0.01
Content-Length: 694
(Here comes xml with Id, Updated, edited etc.)
Photo Request:
GET https://www.google.com/m8/feeds/photos/media/myMail#gmail.com/55f3484e8aaf1c82 HTTP/1.1
Content-Type: application/atom+xml; charset=UTF-8
User-Agent: G-GoogleContactsSync/GOAuth2RequestFactory-CS-Version=2.2.0.0
Authorization: Bearer myAuthorization
GData-Version: 3.0
Host: www.google.com
Photo Response:
HTTP/1.1 200 OK
Content-Type: image/jpeg
Expires: Wed, 01 Oct 2014 20:25:54 GMT
Date: Wed, 01 Oct 2014 20:25:54 GMT
Cache-Control: private, max-age=0, must-revalidate, no-transform
Vary: Accept, X-GData-Authorization, GData-Version
GData-Version: 3.1
ETag: "SomeEtag."
Transfer-Encoding: chunked
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Alternate-Protocol: 443:quic,p=0.01
I retested this myself and uploaded pictures from a number of sources including contacts app on my android phone and tablet, the gmail contacts and the API. All of them will take a higher resolution picture but will only allow the size to be 96x96 using the Contact API v3.So when you download via any application using that API on the PC you will get a 96x96 image and there appears to be no way to vary that with that particular API.
Meaning unless I've also missed something that the contact API v3 is limited to this size of image. I suspect the google+ equivalents are not.
The way in which I have done this is to use the google plus api
var service = new PlusService(new BaseClientService.Initializer());
var request = new PeopleResource.GetRequest(service, "<your google user id>")
{
OauthToken = authParameters.AccessToken
};
Person person = request.Execute();
Person.ImageData image = person.Image;
string pictureUrl = image.Url;
... request to url here after munging sz
now you are going to need to change the url that comes back to the size you want as the defaults is ?sz=50, if your original is at the size you specify it will show it or otherwise it will scale the image.
However the contacts api and the google plus api are different beasts. You will need to use the domain functionality of google plus to pull out your contacts there and potentially they have different photos than you do in your contacts list (if any is set).
Krystan

What more do I need for a HTTP GET request?

I'm trying to use a web api to download a page.
A link to the api: http://api.arbetsformedlingen.se/
If I use code like:
HttpClient c = new HttpClient();
c.BaseAddress = new Uri("http://api.arbetsformedlingen.se/");
var response = c.GetAsync("platsannons/soklista/kommuner?lanid=10").Result;
The request looks like:
{Method: GET, RequestUri: 'http://api.arbetsformedlingen.se/platsannons/soklista/kommuner?lanid=10', Version: 1.1, Content: <null>, Headers:{}}
Shouldn't this be enough to get a valid response? What I do get is a 400 Bad request:
StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:{ Connection: close Proxy-Connection: keep-alive Date: Fri, 28 Jun 2013 10:02:34 GMT Server: Apache Content-Length: 117 Content-Type: application/json; charset=UTF-8}
Do I need to have something in the Content?
Shouldn't this be enough to get a valid response?
That totally depends on the server. Try to compare your request with a "real" request issued from a browser, for example using Fiddler, and spot the differences. The server might block automated requests by returning a 400.

Categories

Resources