Skip to content

Instantly share code, notes, and snippets.

@n0mimono
Created February 12, 2018 06:10
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save n0mimono/06d3c75decff15b7c788fce9cf2a40ec to your computer and use it in GitHub Desktop.
Save n0mimono/06d3c75decff15b7c788fce9cf2a40ec to your computer and use it in GitHub Desktop.
Unity script example for YouTube Live Streaming API
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System;
using System.Net;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.Networking;
using SimpleJSON;
public class YouTubeLiveController : MonoBehaviour {
IEnumerator Start() {
var clientId = "client_id";
var clientSecret = "client_secret";
var code = "";
LocalServer (c => code = c);
var authUrl = "https://accounts.google.com/o/oauth2/v2/auth?response_type=code"
+ "&client_id=" + clientId
+ "&redirect_uri=" + "http://localhost:8080"
+ "&scope=" + "https://www.googleapis.com/auth/youtube.readonly"
+ "&access_type=" + "offline";
Application.OpenURL (authUrl);
yield return new WaitUntil (() => code != "");
Debug.Log (code);
var tokenUrl = "https://www.googleapis.com/oauth2/v4/token";
var content = new Dictionary<string,string> () {
{ "code", code },
{ "client_id", clientId },
{ "client_secret", clientSecret },
{ "redirect_uri", "http://localhost:8080" },
{ "grant_type", "authorization_code" },
{ "access_type", "offline" },
};
var request = UnityWebRequest.Post (tokenUrl, content);
yield return request.SendWebRequest();
var json = JSON.Parse (request.downloadHandler.text);
var token = json["access_token"].RawString();
Debug.Log (token);
var url = "https://www.googleapis.com/youtube/v3/liveBroadcasts?part=snippet";
url += "&id=" + "xxxxxxxxxxx";
var req = UnityWebRequest.Get (url);
req.SetRequestHeader ("Authorization", "Bearer " + token);
yield return req.SendWebRequest();
json = JSON.Parse (req.downloadHandler.text);
var chatId = json["items"][0]["snippet"]["liveChatId"].RawString();
Debug.Log (chatId);
url = "https://www.googleapis.com/youtube/v3/liveChat/messages?part=snippet,authorDetails";
url += "&liveChatId=" + chatId;
req = UnityWebRequest.Get (url);
req.SetRequestHeader ("Authorization", "Bearer " + token);
yield return req.SendWebRequest();
json = JSON.Parse (req.downloadHandler.text);
var items = json["items"];
foreach (var item in items) {
var snip = item.Value ["snippet"];
var author = item.Value["authorDetails"];
Debug.Log (author ["displayName"].RawString () + ": "
+ snip ["displayMessage"].RawString());
}
Debug.Log (json["nextPageToken"]);
}
void LocalServer(Action<string> onReceive) {
ThreadStart start = () => {
try {
var listener = new HttpListener();
listener.Prefixes.Add("http://*:8080/");
listener.Start();
var context = listener.GetContext();
var req = context.Request;
var res = context.Response;
var re = new Regex (@"/\?code=(?<c>.*)");
var code = re.Match (req.RawUrl).Groups ["c"].ToString();
onReceive(code);
res.StatusCode = 200;
res.Close();
} catch (Exception e) {
Debug.LogError(e);
}
};
new Thread (start).Start ();
}
}
public static class SimpleJsonUtility {
public static string RawString(this JSONNode node) {
var len = node.ToString ().Length - 2;
return node.ToString ().Substring (1, len);
}
}
@PickyPixelStudio
Copy link

Hey,

I was looking for a CS script that Can link my youtube stream to Unity (i'll use or as an overlay) i wanted to know if i Can trigger some animation, or other things, with your script? If yes, how it work? I've no knowledge with API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment