Twitter API Client for Unity C#. (ex-name. twitter-for-unity)
Inspired by Let's Tweet In Unity.
- Unity 2017.1.0f3
- Unity 2017.2.0f3
- Unity 2017.4.2f2
- Unity 2018.1.0f2
(On Unity 2017.3, Streaming API doesn't work)
- GET : Available
- POST : Available -- Chunked POST "media/upload" (INIT/APPEND/FINALIZE) is not available yet (now in progress)
- POST statuses/filter : Available(beta)
- GET statuses/sample : Available(beta)
- UserStreams : Available(beta)
If you have access_token and access_token_secret,
public class EventHandler : MonoBehaviour {
void Start () {
Twity.Oauth.consumerKey = "...";
Twity.Oauth.consumerSecret = "...";
Twity.Oauth.accessToken = "...";
Twity.Oauth.accessTokenSecret = "...";
}
}
You can use application-only authentication. https://developer.twitter.com/en/docs/basics/authentication/overview/application-only
public class EventHandler : MonoBehaviour {
void Start () {
Twity.Oauth.consumerKey = "...";
Twity.Oauth.consumerSecret = "...";
StartCoroutine(Twity.Client.GetOauth2BearerToken(Callback));
}
void Callback(bool success) {
if (!success) return;
// you write some request with application-only authentication
}
}
You can use PIN-Based Oauth. https://developer.twitter.com/en/docs/basics/authentication/overview/pin-based-oauth
public class EventHandler : MonoBehaviour {
void Start () {
Twity.Oauth.consumerKey = "...";
Twity.Oauth.consumerSecret = "...";
StartCoroutine(Twity.Client.GenerateRequestToken(RequestTokenCallback));
}
void RequestCallback(bool success) {
if (!success) return;
// When request successes, you can display `Twity.Oauth.authorizeURL` to user so that they may use a web browser to access Twitter.
}
void GenerateAccessToken(string pin) {
// pin is numbers displayed on web browser when user complete authorization.
StartCoroutine(Twity.Client.GenerateAccessToken(pin, AccessTokenCallback));
}
void AccessTokenCallback(bool success) {
if (!success) return;
// When success, authorization is completed. You can make request to other endpoint.
// User's screen_name is in '`Twity.Client.screenName`.
}
}
using Twity.DataModels.Responses;
void Start() {
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters ["q"] = "search word";
parameters ["count"] = 30.ToString();;
StartCoroutine (Twity.Client.Get ("search/tweets", parameters, Callback));
}
void Callback(bool success, string response) {
if (success) {
SearchTweetsResponse Response = JsonUtility.FromJson<SearchTweetsResponse> (response);
} else {
Debug.Log (response);
}
}
using Twity.DataModels.Responses;
void Start() {
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters ["count"] = 30.ToString();;
StartCoroutine (Twity.Client.Get ("statuses/home_timeline", parameters, Callback));
}
void Callback(bool success, string response) {
if (success) {
StatusesHomeTimelineResponse Response = JsonUtility.FromJson<StatusesHomeTimelineResponse> (response);
} else {
Debug.Log (response);
}
}
using Twity.DataModels.Core;
void Start() {
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters ["status"] = "Tweet from Unity";
StartCoroutine (Twity.Client.Post ("statuses/update", parameters, Callback));
}
void Callback(bool success, string response) {
if (success) {
Tweet tweet = JsonUtility.FromJson<Tweet> (response);
} else {
Debug.Log (response);
}
}
ex. search tweets with the word "Unity", and retweet 5 tweets.
using Twity.DataModels.Core;
using Twity.DataModels.Responses;
void start() {
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters ["q"] = "Unity"; // Search keywords
parameters ["count"] = 5.ToString(); // Number of Tweets
StartCoroutine (Twity.Client.Get ("search/tweets", parameters, Callback));
}
void Callback(bool success, string response) {
if (success) {
SearchTweetsResponse Response = JsonUtility.FromJson<SearchTweetsResponse> (response);
foreach (Tweet tweet in Response.statuses) { Retweet (tweet); }
} else {
Debug.Log (response);
}
}
void Retweet(Tweet tweet) {
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters ["id"] = tweet.id_str;
StartCoroutine (Twity.Client.Post ("statuses/retweet/" + tweet.id_str, parameters, RetweetCallback));
}
void RetweetCallback(bool success, string response) {
if (success) {
Debug.Log ("Retweet Done");
} else {
Debug.Log (response);
}
}
using Twity.DataModels.Core;
void start() {
byte[] imgBinary = File.ReadAllBytes(path/to/the/file);
string imgbase64 = System.Convert.ToBase64String(imgBinary);
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters["media_data"] = imgbase64;
parameters["additional_owners"] = "additional owner if you have";
StartCoroutine (Twity.Client.Post ("media/upload", parameters, MediaUploadCallback));
}
void MediaUploadCallback(bool success, string response) {
if (success) {
UploadMedia media = JsonUtility.FromJson<UploadMedia>(response);
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters["media_ids"] = media.media_id.ToString();
parameters["status"] = "Tweet text with image";
StartCoroutine (Twity.Client.Post ("statuses/update", parameters, StatusesUpdateCallback));
} else {
Debug.Log (response);
}
}
void StatusesUpdateCallback(bool success, string response) {
if (success) {
Tweet tweet = JsonUtility.FromJson<Tweet> (response);
} else {
Debug.Log (response);
}
}
See https://dev.twitter.com/rest/reference for more Methods.
using Twity;
using Twity.DataModels.Core;
Stream stream;
void Start() {
stream = new Stream(StreamType.PublicFilter);
Dictionary<string, string> streamParameters = new Dictionary<string, string>();
List<string> tracks = new List<string>();
tracks.Add("Unity");
tracks.Add("Twitter");
Twity.FilterTrack filterTrack = new Twity.FilterTrack(tracks);
streamParameters.Add(filterTrack.GetKey(), filterTrack.GetValue());
StartCoroutine(stream.On(streamParameters, OnStream));
}
void OnStream(string response, StreamMessageType messageType) {
try
{
if(messageType == StreamMessageType.Tweet)
{
Tweet tweet = JsonUtility.FromJson<Tweet>(response);
}
}
catch (System.Exception e)
{
Debug.Log(e);
}
}
User Stream is retired from August 23rd, 2018 https://developer.twitter.com/en/docs/accounts-and-users/subscribe-account-activity/api-reference/user-stream.html
using Twity;
using Twity.DataModels.Core;
using Twity.DataModels.StreamMessages;
Stream stream;
void Start() {
stream = new Stream(StreamType.User);
Dictionary<string, string> streamParameters = new Dictionary<string, string>();
StartCoroutine(stream.On(streamParameters, OnStream));
}
void OnStream(string response, StreamMessageType messageType) {
try
{
if(messageType == StreamMessageType.Tweet)
{
Twity.Tweet tweet = JsonUtility.FromJson<Tweet>(response);
}
else if(messageType == StreamMessageType.StreamEvent)
{
StreamEvent streamEvent = JsonUtility.FromJson<StreamEvent>(response);
Debug.Log(streamEvent.event_name); // Response Key 'event' is replaced 'event_name' in this library.
}
else if(messageType == StreamMessageType.FriendsList)
{
FriendsList friendsList = JsonUtility.FromJson<FriendsList>(response);
}
}
catch (System.Exception e)
{
Debug.Log(e);
}
}
See StreamType
and StreamMessageType
at TwitterStreamType.cs
. and https://dev.twitter.com/streaming/overview/messages-types .
See https://dev.twitter.com/streaming/reference for more Methods.
See DataModels/Responses/
, and https://dev.twitter.com/overview/api/tweets , https://dev.twitter.com/overview/api/users , https://dev.twitter.com/overview/api/entities , https://dev.twitter.com/overview/api/entities-in-twitter-objects .
You can modify DataModels/Responses/
to get a response item.
MIT