How to call https://api.datamarket.azure.com/Services/My/Datasets for getting ResourceBalance

StackOverflow https://stackoverflow.com/questions/15517252

  •  24-03-2022
  •  | 
  •  

Вопрос

I am facing problem for accessing Microsoft Translator Services which will provide the remained characters limit.

I have done token accessing part, only the thing is to get the user Datasets information by calling

https://api.datamarket.azure.com/Services/My/Datasets service.

I referred this link

Это было полезно?

Решение

There are 2 ways to authorize for getting the Resource Balance

  1. using OAuth ( requires auth token)
  2. using Basic (requires account key)

Basic authorization process

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import org.apache.commons.codec.binary.Base64;

public class MSTranslaterUserInforService {
    public static void main(String[] args) throws Exception {
        String bingUrl = "https://api.datamarket.azure.com/Services/My/Datasets?$format=json";
        String accountKey = "place your account_key here";
        byte[] accountKeyBytes = Base64
                .encodeBase64((accountKey + ":" + accountKey).getBytes());
        String accountKeyEnc = new String(accountKeyBytes);
        URL urlb = new URL(bingUrl);
        URLConnection urlConnection = urlb.openConnection();
        urlConnection.setRequestProperty("Authorization", "Basic "
                + accountKeyEnc);
        String line;
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                urlConnection.getInputStream()));
        line = reader.readLine();
        System.out.println(line);
    }
}

Другие советы

Since no language was specified, this covers the C# side.

This includes quick&dirty (current as of 2013-12-05) XML parsing for extracting the Microsoft Translator resource count.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Xml;
using System.IO;

namespace translation_testing
{
    class Class1
    {
        public static int GetUsage()
        {
            string uri = "https://api.datamarket.azure.com/Services/My/Datasets";
            string general_accountkey = "your account key";

            string accountkeyraw = general_accountkey + ":" + general_accountkey;
            byte[] accountkeybytes = Encoding.UTF8.GetBytes(accountkeyraw);
            string accountkeystring = Convert.ToBase64String(accountkeybytes);

            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
            httpWebRequest.Headers.Add("Authorization", "Basic " + accountkeystring);

            WebResponse response = null;
            try
            {
                response = httpWebRequest.GetResponse();
                using (Stream stream = response.GetResponseStream())
                {
                    StringBuilder sbresponse = new StringBuilder();
                    int len = 1;
                    while (stream.CanRead && len > 0)
                    {
                        byte[] chunk = new byte[4096];
                        len = stream.Read(chunk, 0, 4096);
                        sbresponse.Append(Encoding.UTF8.GetString(chunk, 0, len));
                    }

                    return translator_resource_count(sbresponse.ToString());
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                    response = null;
                }
            }
        }

        private static int translator_resource_count(string input)
        {
            bool isentry = false;
            bool iscontent = false;
            bool istitle = false;
            bool isbalance = false;

            string title = null;
            string balance = null;

            using (XmlReader reader = XmlReader.Create(new StringReader(input)))
            {
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Text)
                    {
                        if (istitle) title = reader.Value;
                        if (isbalance) balance = reader.Value;
                    }
                    else if (reader.NodeType == XmlNodeType.Element)
                    {
                        if (reader.Name == "entry") isentry = true;
                        if (isentry && reader.Name == "content") iscontent = true;
                        if (isentry && !iscontent && reader.Name == "title") istitle = true;
                        if (isentry && iscontent && title == "Microsoft Translator" && reader.Name == "d:ResourceBalance") isbalance = true;
                    }
                    else if (reader.NodeType == XmlNodeType.EndElement)
                    {
                        if (reader.Name == "entry") isentry = false;
                        if (isentry && reader.Name == "content") iscontent = false;
                        if (isentry && !iscontent && reader.Name == "title") istitle = false;
                        if (isentry && iscontent && title == "Microsoft Translator" && reader.Name == "d:ResourceBalance") isbalance = false;
                    }
                }
            }

            return int.Parse(balance);
        }

    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top