Search code examples
microsoft-translator

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


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


Solution

  • 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);
        }
    }