Information About Using the Translator Text API with Unity

Asked 2 years ago, Updated 2 years ago, 146 views

Unity is creating an application for hololens.
I want to use Azure's Translator Text API, but it doesn't work well.
I will put the error and code on it, so please let me know when you know.
The reference site is https://blogs.msdn.microsoft.com/dataplatjp/2017/12/07/translator-text-api-sample/.

Note: July 19, 2018, 17:54 PM
I received your reply and added it to ignore the certificate.
However, I cannot build the app as UWP.
Below are four errors.
Error 1

Assets\Script\TrustAllCertificatePolicy.cs(3,42): error CS0246: The type or namespace name 'ICertificatePolicy' could not be found (are you missing a using direct or an assembly reference?)

Error 2

Assets\Script\TrustAllCertificatePolicy.cs(6,50): error CS0234: The type or namespace name 'ServicePoint' does not exist in the namespace' System.Net' (are you missing an assembly reference?)

Error 3

Assets\Script\TrustAllCertificatePolicy.cs (7,38): error CS0234: The type or namespace name 'X509 Certificates ' does not exist in the namespace' System.Security.Cryptography' (are you missing an assembly reference?)

ERROR 4

Error building Player cause scripts had compiler errors

ERROR 5

Build completed with a result of 'Failed'
UnityEngine.GUIUtility: ProcessEvent (Int32, IntPtr)

ERROR 6

UnityEditor.BuildPlayerWindow+BuildMethodException:4 errors
  at UnityEditor.BuildPlayerWindow+DefaultBuildMethods.BuildPlayer(BuildPlayerOptions)[0x00207] in C:\buildslave\unity\build\Editor\Mono\BuildPlayerWindowBuildMethods.cs:172 
  at UnityEditor.BuildPlayerWindow.CallBuildMethods (Boolean askForBuildLocation, BuildOptions defaultBuildOptions)[0x00050] in C:\buildslave\unity\build\Editor\Mono\BuildPlayerWindowBuildMethods.cs:83 
UnityEngine.GUIUtility: ProcessEvent (Int32, IntPtr)

The following parts have been resolved
OnInputClicked is an input event for Hololens.
Debug.log results up to in!!
The error occurs in the httpresponse=httpWebRequest.GetResponse(); 」 section.(line 53)

using HoloToolkit.Unity.InputModule;
using System;
using System.Collections;
using System.Collections.General;
using System.IO;
using System.Net;
using UnityEngine;

public class test —MonoBehavior, IInputClickHandler
{
    stringuri, ocpApimSubscriptionKey;
    // Use this for initialization
    void Start()
    {
        uri=GetUri("hello");
        ocpApimSubscriptionKey="hogehoge";//key
    }

    // Update is called once per frame
    void Update()
    {

    }
    private string GetUri (string inputText)
    {
        // Pre-Translation Language
        string fromLanguage = "en";
        // the language of a translated word
        string toLanguage = "ja";
        // Model to be used: Specify blank (=statistical machine translation) or generalnn (= neural network)
        US>string ModelType="generaln";
        // Translator Text API Subscription Key (obtained from Azure Portal (https://portal.azure.com)

        // URI definition (to send parameters in Get, add the required parameters to the URL)
        stringuri="https://api.microsofttranslator.com/v2/http.svc/Translate?"+
        "&text=" + Uri.EscapeDataString(inputText)+
        "&from=" + fromLanguage+
        " & to=" + to Language +
        "&category="+ModelType;
        returnuri;
    }
    private IEnumerator UseAPI()
    {
        // Create httpWebRequest
        HttpWebRequest httpWebRequest=(HttpWebRequest) WebRequest.Create(uri);

        // Embed subscription key in http header for Authorization
        httpWebRequest.Headers.Add("Ocp-Apim-Subscription-Key:"+ocpApimSubscriptionKey);

        WebResponse response=null;

        // Run a request to the Translator Text API to get the results
        response = httpWebRequest.GetResponse(); //** Error here**
        Debug.Log("wait");
        yield return response;
        Debug.Log("finish");
        using(Stream stream=response.GetResponseStream())
        {
            System.Runtime.Serialization.DataContractSerializer dcs=
            new System.Runtime.Serialization.DataContractSerializer(Type.GetType("System.String"));
            string translationText=(string)dcs.ReadObject(stream);
            Debug.Log(translationText);
        }
    }

    public void OnInputClickedEventData eventData
    {
        Debug.Log("in!!!");
        StartCoroutine (UseAPI());
    }
}

Error Description:

TlsException: Invalid certificate received from server.Error code: 0xffffffff800b010a
Mono.Security.Protocol.Tls.Handshake.Client.TlsServerCertificate.validateCertificates (Mono.Security.X509.X509CertificateCollection certificates)
Mono.Security.Protocol.Tls.Handshake.Client.TlsServerCertificate.ProcessAsTls1()
Mono.Security.Protocol.Tls.Handshake.HandshakeMessage.Process()
(wrapper remoteing-invoke-with-check) Mono.Security.Protocol.Tls.Handshake.HandshakeMessage: Process()
Mono.Security.Protocol.Tls.ClientRecordProtocol.ProcessHandshakeMessage (Mono.Security.Protocol.Tls.TlsStream handMsg)
Mono.Security.Protocol.Tls.RecordProtocol.InternalReceiveRecordCallback (IAsyncResult asyncResult)
Return as IOException: The authentication or decryption has failed.
Mono.Security.Protocol.Tls.SslStreamBase.AsyncHandshakeCallback (IAsyncResult asyncResult)
Return as WebException: Error getting response stream (Write: The authentication or decryption has failed.)—SendFailure
System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult)
System.Net.HttpWebRequest.GetResponse()
Test+<UseAPI>c_Iterator0.MoveNext() (at Assets/Script/Test.cs:53)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
UnityEngine.MonoBehavior: StartCoroutine (IEnumerator)
Test: OnInputClickedEventData (at Assets/Script/Test.cs:68)
HoloToolkit.Unity.InputModule.InputManager: <OnInputClickEventHandler>m__4(IinputClickHandler, BaseEventData) (at Assets/HoloToolkit/Input/Scripts/Utilities/Managers/InputManager.cs:402)
UnityEngine.EventSystems.ExecuteEvents: ExecuteHierarchy (GameObject, BaseEventData, EventFunction`1)
HoloToolkit.Unity.InputModule.InputManager: HandleEvent(BaseEventData, EventFunction`1) (at Assets/HoloToolkit/Input/Scripts/Utilities/Managers/InputManager.cs:296)
HoloToolkit.Unity.InputModule.InputManager: RiseInputClicked (IInputSource, UInt32, InteractionSourcePressInfo, Int32, Object[]) (at Assets/HoloToolkit/Input/Scripts/Utilities/Managers/InputManager.cs:411)
HoloToolkit.Unity.InputModule.InteractionInputSource: GestureRecognizer_Taped (TappedEventArgs) (at Assets/HoloToolkit/Input/Scripts/InputSources/InteractionInputSource.cs:971)
UnityEngine.XR.WSA.Input.GestureRecognizer: InvokeTapped (InteractionSource, InteractionSourcePose, Pose, Int32)

c# unity3d api azure asynchronous

2022-09-30 18:32

1 Answers

Cancel TlsException article addresses the same error as the question.I think it will be helpful.


2022-09-30 18:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.