Android Question Bing Translate with HTTP

Rusty

Well-Known Member
Licensed User
Longtime User
I am trying to use Bing translate to translate text from one language to another. i.e. en to fr
I have provided my key from my account and it keep getting an error.
Argument Exception
Method: Translate()

Parameter: appId

Message: Invalid appId Parameter name: appId

message id=0824.V2_Rest.Translate.38B5BC37
I'm not clear on what it means by AppID...any suggestions?
thanks
Rusty
B4X:
Private Const BingKey As String = "AAAAAAAAAAAAAAAAAAAAAAAAAAAA" 
    Private Const BingURLTranslateRoot As String = "http://api.microsofttranslator.com/v2/http.svc/translate?key=" & BingKey & "&source="
B4X:
Dim SU As StringUtils
    Dim TranslationURL As String = BingURLTranslateRoot  & "&format=text&q=" & SU.EncodeUrl(txt, "UTF8") & _
                                    "&from=en" & _
                                    "&To=fr" & _
                                    "&category=general"
    TranslateText(TranslationURL, txt)

B4X:
Sub TranslateText(Link As String, edt As String)        'EditText)
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(Link)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        edt = j.GetString
        Dim JSON As JSONParser
        Dim Map1 As Map
        JSON.Initialize(j.GetString)
        Map1 = JSON.NextObject
        Dim m As Map                     'helper map for navigating
        Dim MenuItems As List
        m = Map1.Get("data")
        MenuItems = m.Get("translations")
        For i = 0 To MenuItems.Size - 1
            m = MenuItems.Get(i)
            edt = m.Get("translatedText")
            Dim Parts() As String = Regex.Split("\|", edt)
            If Parts.Length > 2 Then
                edt = Parts(2)
            End If
            Log(edt)
        Next
        AddText(edt)
    End If
    j.Release
End Sub
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Thanks Don,
I am not sure how that translates to B4a, can you advise?
Regards,
Rusty
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
You need to have an accesstoken which you probably get through an OAuth2 call?
You need to check how to aquire a Accesstoken or use OAuth with bing. I´m not familar with it.

If you know the url and the scope then you can probably use Erels OAuth2Class to authenticate and get the accesstoken

Once you have them you can adapts your httpjob and add a header after post
B4X:
    j.Download(Link)
    j.GetRequest.SetHeader("Authorization", "Bearer "&mAccessToken)
 
Upvote 0
Top