Youtube & Twitter API Integration w/Unity

  • Request Data from YouTube Data API

Using the code line shown below will help you fetch the data of any Youtuber, including his/her subscribers count, view count, total videos, etc. Two things which are variable are “dlcUrl[i]” and “API_Key”. “dlcUrl[i]” is the channel ID of any particular Youtuber. “API_Key” is the key which we get from Google YouTube Data API Console. Without it you cannot request the data. Daily total 10,000 quota points can be used. One request takes 3 quota points. Once 10,000 points (i.e. 3333 requests) are used up, you cannot request data from YouTube anymore. 10,000 points are refilled next day. Different types of requests take different values of points. Refer: https://developers.google.com/youtube/v3/determine_quota_cost

UnityWebRequest www = UnityWebRequest.Get(“https://www.googleapis.com/youtube/v3/channels?part=statistics&id=”+ dlcUrls[i] + “&key=” + API_Key);

yield return www.SendWebRequest(); //returns data in text format

  • Request Data from Twitter API

Request Twitter Data has no limits. Data can be number of twitter Followers of particular person, their twitter Post ID (get recent tweets/retweets they have done)

//Authorization set-up
public static string GetTwitterAccessToken (string consumerKey, string consumerSecret)

{

//Convert the consumer key and secret strings into a format to be added to our header string URL_ENCODED_KEY_AND_SECRET = Convert.ToBase64String(Encoding.UTF8.GetBytes(consumerKey + “:” + consumerSecret));

byte[] body;
body = Encoding.UTF8.GetBytes(“grant_type=client_credentials”);

Dictionary<string, string> headers = new Dictionary<string, string> ();
headers [“Authorization”] = “Basic ” + URL_ENCODED_KEY_AND_SECRET;

//Send a request to the Twitter API for an access token
WWW web = new WWW (“https://api.twitter.com/oauth2/token”, body, headers);

while (!web.isDone) { }
if (web.error != null)
{
//Debug.Log(web.error);
}
else
{

Debug.Log(“Access token retrieved”);

//Formatting string
string output = web.text.Replace(“{\”token_type\”:\”bearer\”,\”access_token\”:\””,””);

output = output.Replace(“\”}”,””);
return output;
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *