Skip to content

Instantly share code, notes, and snippets.

@mosluce
Created May 17, 2017 07:03
Show Gist options
  • Select an option

  • Save mosluce/82fed2d169c36e9f339f420847f47de2 to your computer and use it in GitHub Desktop.

Select an option

Save mosluce/82fed2d169c36e9f339f420847f47de2 to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
using UnityEngine.Networking;
namespace Azure
{
public class Authentication
{
private static Authentication instance;
static private string Uri = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken";
public string token;
private string subscriptionKey;
private Authentication() { }
public static Authentication Instance
{
get
{
if (instance == null)
{
instance = new Authentication();
}
return instance;
}
}
public void SetKey(string key)
{
this.subscriptionKey = key;
}
public IEnumerator AutoFetchToken()
{
yield return FetchToken();
new Timer(new TimerCallback(OnTokenExpiredCallback), this, TimeSpan.FromMinutes(9), TimeSpan.FromMilliseconds(-1));
}
private void OnTokenExpiredCallback(object stateIndo)
{
FetchToken();
}
private IEnumerator FetchToken()
{
using (UnityWebRequest www = UnityWebRequest.Post(Uri, "{}"))
{
www.SetRequestHeader("Ocp-Apim-Subscription-Key", subscriptionKey);
yield return www.Send();
token = www.downloadHandler.text;
Debug.Log(token);
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
namespace Azure
{
public class Speech
{
private static string Uri = "https://speech.platform.bing.com/speech/recognition/interactive/cognitiveservices/v1?language=zh-TW";
public static IEnumerator interactive(string filePath)
{
yield return null;
using (UnityWebRequest www = UnityWebRequest.Post(Uri, null, File.ReadAllBytes(filePath)))
{
www.SetRequestHeader("Authorization", "Bearer " + Azure.Authentication.Instance.token);
yield return www.Send();
Debug.Log(www.downloadHandler.text);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment