Answered TranslateApiException

  • Wednesday, February 27, 2013 2:17 PM
     
     

    It was working fine until today. I tried it a bunch of times yesterday and it worked fine. But today is does not work.

    Translate returns:

    TranslateApiException  Method: Translate()  Message: Cannot find an active Azure Market Place Translator Subscription associated with the request credentials.

    Detect always works.

    Code:

    # -*- coding: utf-8 -*-
    import urllib, httplib
    import json
    import re
    import time

    class Token(object):
        def __init__(self, client_id, client_secret):
            self.client_id = client_id;
            self.client_secret = client_secret
            self.init = True
            self.token = None

        def getToken(self, re_init=False):
            if not self.init and not re_init:
                if time.time() - self.start_time < 580:
                    return self.token
            self.init = False
            self.start_time = time.time()
            params = urllib.urlencode({'client_id': self.client_id, 'client_secret': self.client_secret,
                    'scope': "http://api.microsofttranslator.com", "grant_type":"client_credentials"})
            headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}

            conn = httplib.HTTPSConnection("datamarket.accesscontrol.windows.net")
            conn.request("POST", "/v2/OAuth2-13", params)
            rs = json.loads ( conn.getresponse().read() )
            self.token = rs[u'access_token']
            return self.token

    class BingTranslator(object):
        def __init__(self, client_id, client_secret):
            self.token_obj = Token(client_id, client_secret)

        def unicode2utf8(self, text):
            try:
                if isinstance(text, unicode):
                    text = text.encode('utf-8')
            except Exception as (e, msg):
                print e, msg
                pass
            return text

        def getText(self, xml):
            text = re.sub(r"<.+?>", " ", xml).strip()
            return text

        def getTranslation(self, text, src, tgt, reinit_token = False):
            token = self.token_obj.getToken(reinit_token)
            headers = {'Authorization':'bearer %s' % token}
            conn = httplib.HTTPConnection('api.microsofttranslator.com')
            dic = {}
            dic['from'] = src
            dic['to'] = tgt
            dic['text'] = self.unicode2utf8(text)
            addr = '/V2/Http.svc/Translate?' + urllib.urlencode(dic)
            conn.request("GET", addr, headers=headers)
            xml = conn.getresponse().read()
            return self.getText(xml)

    if __name__ == "__main__":
        client_id = ""
        client_secret = ""
        translator = BingTranslator (client_id, client_secret)
        print translator.getTranslation ("""We can read of things that happened 5,000 years ago in the Near East.""", "en", "de")

All Replies

  • Wednesday, February 27, 2013 2:39 PM
     
     

    I tried running it multiple times in a loop and it runs sometimes and throws the exception other times.

    It is not reliable. :/

  • Wednesday, February 27, 2013 3:31 PM
     
     

    Loop of 20 clearly showing the problem.

    0
    Wir können Dinge lesen, die im Nahen Osten vor 5.000 Jahren passiert ist.
    1
    Wir können Dinge lesen, die im Nahen Osten vor 5.000 Jahren passiert ist.
    2
    Wir können Dinge lesen, die im Nahen Osten vor 5.000 Jahren passiert ist.
    3
    Wir können Dinge lesen, die im Nahen Osten vor 5.000 Jahren passiert ist.
    4
    Wir können Dinge lesen, die im Nahen Osten vor 5.000 Jahren passiert ist.
    5
    Wir können Dinge lesen, die im Nahen Osten vor 5.000 Jahren passiert ist.
    6
    TranslateApiException  Method: Translate()  Message: Cannot find an active Azure Market Place Translator Subscription associated with the request credentials.    message id=0818.V2_Rest.Translate.431B4F73
    7
    Wir können Dinge lesen, die im Nahen Osten vor 5.000 Jahren passiert ist.
    8
    TranslateApiException  Method: Translate()  Message: Cannot find an active Azure Market Place Translator Subscription associated with the request credentials.    message id=0818.V2_Rest.Translate.431B50BD
    9
    Wir können Dinge lesen, die im Nahen Osten vor 5.000 Jahren passiert ist.
    10
    Wir können Dinge lesen, die im Nahen Osten vor 5.000 Jahren passiert ist.
    11
    Wir können Dinge lesen, die im Nahen Osten vor 5.000 Jahren passiert ist.
    12
    Wir können Dinge lesen, die im Nahen Osten vor 5.000 Jahren passiert ist.
    13
    Wir können Dinge lesen, die im Nahen Osten vor 5.000 Jahren passiert ist.
    14
    Wir können Dinge lesen, die im Nahen Osten vor 5.000 Jahren passiert ist.
    15
    TranslateApiException  Method: Translate()  Message: Cannot find an active Azure Market Place Translator Subscription associated with the request credentials.    message id=0818.V2_Rest.Translate.431B55B8
    16
    Wir können Dinge lesen, die im Nahen Osten vor 5.000 Jahren passiert ist.
    17
    Wir können Dinge lesen, die im Nahen Osten vor 5.000 Jahren passiert ist.
    18
    Wir können Dinge lesen, die im Nahen Osten vor 5.000 Jahren passiert ist.
    19
    Wir können Dinge lesen, die im Nahen Osten vor 5.000 Jahren passiert ist.


    • Edited by Snype Angels Wednesday, February 27, 2013 3:32 PM
    •  
  • Wednesday, February 27, 2013 4:28 PM
     
     Answered

    Fixed it.

    For others:

    Its the client id and the client secret. Just make your own. Make them simple.

    Mine is something like client id: newapp20303030 and client secret: secretpassword20203030404050

    I think the = in the end of the client secret causes urlencode problems.

    • Marked As Answer by Snype Angels Wednesday, February 27, 2013 4:29 PM
    •  
  • Thursday, March 07, 2013 1:40 AM
    Moderator
     
     

    Hi,

    Thanks for sharing.

    Thanks,


    QinDian Tang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

  • Sunday, April 28, 2013 9:03 PM
     
     

    I've got exactly the same problem as you described. I have tried renaming my client id and secret to something simple as you advised, but it doesn't help, i.e. the API call is working from time to time only, usually it fails with:

    microsofttranslator.TranslateApiException: Cannot find an active Azure Market Place Translator Subscription associated with the request credentials.

    Anybody else experiencing this today?