タイトルにも書きましたが、YoutubeのLIve配信での視聴者のチャットコメントを習得するプログラムを以下のサイトを参考にして作っていますが、このサイトのプログラムで実行をしますと、デバッグログには次のように表示されます。
・参考サイト
https://qiita.com/n0mimono/items/0c2921c9b964cd305fe3
・ソースコード
C#
1using System.Collections; 2using System.Collections.Generic; 3using System.Threading; 4using System; 5using System.Net; 6using System.Text.RegularExpressions; 7using UnityEngine; 8using UnityEngine.Networking; 9using SimpleJSON; 10 11public class YouTubeLiveController : MonoBehaviour { 12 13 IEnumerator Start() { 14 var clientId = "僕のクライアントID"; 15 var clientSecret = "僕のクライアントシークレット"; 16 17 var code = ""; 18 LocalServer (c => code = c); 19 20 var authUrl = "https://accounts.google.com/o/oauth2/v2/auth?response_type=code" 21 + "&client_id=" + clientId 22 + "&redirect_uri=" + "http://localhost:8080" 23 + "&scope=" + "https://www.googleapis.com/auth/youtube.readonly" 24 + "&access_type=" + "offline"; 25 Application.OpenURL (authUrl); 26 yield return new WaitUntil (() => code != ""); 27 28 Debug.Log (code); 29 30 var tokenUrl = "https://www.googleapis.com/oauth2/v4/token"; 31 var content = new Dictionary<string,string> () { 32 { "code", code }, 33 { "client_id", clientId }, 34 { "client_secret", clientSecret }, 35 { "redirect_uri", "http://localhost:8080" }, 36 { "grant_type", "authorization_code" }, 37 { "access_type", "offline" }, 38 }; 39 var request = UnityWebRequest.Post (tokenUrl, content); 40 yield return request.SendWebRequest(); 41 42 var json = JSON.Parse (request.downloadHandler.text); 43 var token = json["access_token"].RawString(); 44 45 Debug.Log (token); 46 47 var url = "https://www.googleapis.com/youtube/v3/liveBroadcasts?part=snippet"; 48 url += "&id=" + "5iyt8IgOBOI"; // watch?v= 以降のクエリを入力 49 50 var req = UnityWebRequest.Get (url); 51 req.SetRequestHeader ("Authorization", "Bearer " + token); 52 yield return req.SendWebRequest(); 53 54 json = JSON.Parse (req.downloadHandler.text); 55 var chatId = json["items"][0]["snippet"]["liveChatId"].RawString(); 56 57 Debug.Log (chatId); 58 59 url = "https://www.googleapis.com/youtube/v3/liveChat/messages?part=snippet,authorDetails"; 60 url += "&liveChatId=" + chatId; 61 62 req = UnityWebRequest.Get (url); 63 req.SetRequestHeader ("Authorization", "Bearer " + token); 64 yield return req.SendWebRequest(); 65 66 json = JSON.Parse (req.downloadHandler.text); 67 var items = json["items"]; 68 69 foreach (var item in items) { 70 var snip = item.Value ["snippet"]; 71 var author = item.Value["authorDetails"]; 72 Debug.Log (author ["displayName"].RawString () + ": " 73 + snip ["displayMessage"].RawString()); 74 } 75 Debug.Log (json["nextPageToken"]); 76 } 77 78 void LocalServer(Action<string> onReceive) { 79 ThreadStart start = () => { 80 try { 81 var listener = new HttpListener(); 82 listener.Prefixes.Add("http://*:8080/"); 83 listener.Start(); 84 85 var context = listener.GetContext(); 86 var req = context.Request; 87 var res = context.Response; 88 89 var re = new Regex (@"/?code=(?<c>.*)"); 90 var code = re.Match (req.RawUrl).Groups ["c"].ToString(); 91 onReceive(code); 92 93 res.StatusCode = 200; 94 res.Close(); 95 } catch (Exception e) { 96 Debug.LogError(e); 97 } 98 }; 99 new Thread (start).Start (); 100 } 101 102} 103 104public static class SimpleJsonUtility { 105 public static string RawString(this JSONNode node) { 106 var len = node.ToString ().Length - 2; 107 return node.ToString ().Substring (1, len); 108 } 109}
このDebagログはソースコードにおける28行目、45行目、57行目、75行目です。
C#
1Debug.Log (code); 2Debug.Log (token); 3Debug.Log (chatId); 4Debug.Log (json["nextPageToken"]);
YoutubeApisはすでに有効にしており、認証も済んでいるので何が問題なのかわかりません。
対象としている動画はこちらになります。
https://www.youtube.com/watch?v=5iyt8IgOBOI
問題点と解決策をご教示していただきたいです。どうぞよろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー