【发布时间】:2016-09-09 14:10:57
【问题描述】:
我可以登录并发送短信,但无法在 Twitter 上分享图片。
当我上传图片时,它会给出响应,但是当我没有在 Twitter 上发布图片时,我得到了以下响应。
这是我的代码 :-
private const string UploadMediaURL = "https://upload.twitter.com/1.1/media/upload.json";
public static IEnumerator UploadMedia(string consumerKey, string consumerSecret, AccessTokenResponse response)
{
Dictionary<string, string> parameters = new Dictionary<string,string>();
Texture2D tex= Resources.Load("imag") as Texture2D;
byte[] dataToSave = tex.EncodeToPNG ();
string encoded64ImageData = System.Convert.ToBase64String( dataToSave );
parameters.Add("media_data",encoded64ImageData);
WWWForm form = new WWWForm(); // Add data to the form to post.
form.AddField("media_data", encoded64ImageData );
Dictionary<string, string> headers = new Dictionary<string, string>();
string auth = GetHeaderWithAccessToken("POST", UploadMediaURL, consumerKey, consumerSecret, response, parameters);
Debug.Log ("Auth " + auth);
headers.Add( "Authorization", auth);
headers.Add( "Content-Transfer-Encoding","base64");
WWW web = new WWW(UploadMediaURL,form.data, headers);
yield return web;
Debug.Log ("Response " + web.text);
}
回应:-
{
"media_id":700577726298599424,
"media_id_string":"700577726298599424",
"size":9734,
"expires_after_secs":86400,
"image":
{
"image_type":"image\/png",
"w":435,
"h":159
}
}
这是我使用 Media_Id 发布的源代码。当我在没有 media_id 的情况下使用它时,它会成功发布。但是当我给它一个 media_id 参数时,它给了我一个错误
失败。 401 Unauthorized{"errors":[{"code":32,"message":"Could not authenticate you."}]}
源代码:-
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)
{
if (string.IsNullOrEmpty(text) || text.Length > 140)
{
Debug.Log(string.Format("PostTweet - text[{0}] is empty or too long.", text));
callback(false);
}
else
{
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("status", text);
WWWForm form = new WWWForm();
form.AddField("status", text);
form.AddField ("media_id", "732498072731713536");
// 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);
}
}
}
}
【问题讨论】:
-
您用来执行此操作的代码在哪里?
-
我在我的代码中进行了编辑。请参考它并给出一个可能的解决方案。提前致谢。