Reputation: 201
I want to login and sharing on twitter through unity3d. I search a lot there is no official plugins for unity on twitter developer site.I know there is api is available for login and sharing on twitter.I have download a plugin from assets store for twitter integration. In that plugin i am able to login and tweet text message but not able to share image on twitter.Below is link where i got some reference how to upload image on twitter.
C#: Upload Photo To Twitter From Unity
I follow the same thing but when i upload image it gives the response,but when i it did not post image on twitter.following response i am getting.
{"media_id":700577726298599424,"media_id_string":"700577726298599424","size":9734,"expires_after_secs":86400,"image":{"image_type":"image\/png","w":435,"h":159}}
below is code for uploading image on twitter.
private const string UploadMediaURL = "https://upload.twitter.com/1.1/media/upload.json";
public static IEnumerator UploadImageOnTwitter(string ImagePath, string consumerKey, string consumerSecret, AccessTokenResponse response, PostTweetCallback callback)
{
Dictionary<string, string> parameters = new Dictionary<string, string>();
var imageData = File.ReadAllBytes(Application.persistentDataPath +"/Image.png");
string encoded64ImageData = Convert.ToBase64String(imageData);
parameters.Add("media_data", encoded64ImageData );
// Add data to the form to post.
WWWForm form = new WWWForm();
form.AddField( "media_data", encoded64ImageData );
// HTTP header
Dictionary<string, string> headers = new Dictionary<string, string>();
string url = UploadMediaURL;
string auth = GetHeaderWithAccessToken("POST", url, consumerKey, consumerSecret, response, parameters);
headers.Add( "Authorization", auth );
headers.Add( "Content-Transfer-Encoding", "base64" );
Debug.Log ("response is "+response);
WWW web = new WWW(url, form.data, headers);
yield return web;
Debug.Log (web.text);
}
Upvotes: 1
Views: 2389
Reputation: 11
The above code uploads the media but does not post it. You need to tweet the result as well.
private const string UploadMediaURL = "https://upload.twitter.com/1.1/media/upload.json";
private const string PostTweetURL = "https://api.twitter.com/1.1/statuses/update.json";
public static IEnumerator PostTweet(string text, string consumerKey, string consumerSecret, AccessTokenResponse response, PostTweetCallback callback)
{
Dictionary<string, string> mediaParameters = new Dictionary<string, string>();
var imageData = File.ReadAllBytes(Application.persistentDataPath +"/Image.png");
string encoded64ImageData = Convert.ToBase64String(imageData);
mediaParameters.Add("media_data", encoded64ImageData );
// Add data to the form to post.
WWWForm mediaForm = new WWWForm();
mediaForm.AddField( "media_data", encoded64ImageData );
// HTTP header
Dictionary<string, string> mediaHeaders = new Dictionary<string, string>();
string auth = GetHeaderWithAccessToken("POST", UploadMediaURL, consumerKey, consumerSecret, response, mediaParameters);
mediaHeaders.Add( "Authorization", auth );
mediaHeaders.Add( "Content-Transfer-Encoding", "base64" );
WWW mw = new WWW(UploadMediaURL, mediaForm.data, mediaHeaders);
yield return mw;
string mID = Regex.Match(mw.text, @"(\Dmedia_id\D\W)(\d*)").Groups[2].Value;
Debug.Log ("response from media request : " + mw.text);
Debug.Log ("mID = " + mID);
if (!string.IsNullOrEmpty (mw.error)) {
Debug.Log (string.Format ("PostTweet - failed. {0}\n{1}", mw.error, mw.text));
callback (false);
} else {
string error = Regex.Match (mw.text, @"<error>([^&]+)</error>").Groups [1].Value;
if (!string.IsNullOrEmpty (error)) {
Debug.Log (string.Format ("PostTweet - failed. {0}", error));
callback (false);
} else {
callback (true);
}
}
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("status", text);
parameters.Add ("media_ids", mID);
// Add data to the form to post.
WWWForm form = new WWWForm();
form.AddField("status", text);
form.AddField ("media_ids", mID);
// HTTP header
var headers = new Dictionary<string, string>();
headers["Authorization"] = GetHeaderWithAccessToken("POST", PostTweetURL, consumerKey, consumerSecret, response, parameters);
WWW web = new WWW(PostTweetURL, form.data, headers);
yield return web;
if (!string.IsNullOrEmpty(web.error))
{
Debug.Log(string.Format("PostTweet - failed. {0}\n{1}", web.error, web.text));
callback(false);
}
else
{
string error = Regex.Match(web.text, @"<error>([^&]+)</error>").Groups[1].Value;
if (!string.IsNullOrEmpty(error))
{
Debug.Log(string.Format("PostTweet - failed. {0}", error));
callback(false);
}
else
{
callback(true);
}
}
}
Upvotes: 1