【发布时间】:2015-05-15 10:51:03
【问题描述】:
目前我正在与团结合作。在这个项目中,我需要在 Twitter 上发布一些带有图像的状态。但是,我在将图片上传到 Twitter 时遇到了问题。
这是我用来上传图片的代码:
private const string UploadMediaURL = "https://upload.twitter.com/1.1/media/upload.json";
public static IEnumerator UploadMedia(string consumerKey, string consumerSecret, string filePath, AccessTokenResponse response){
Dictionary<string, string> mediaParameters = new Dictionary<string, string>();
mediaParameters.Add("media", System.Convert.ToBase64String(File.ReadAllBytes(filePath)));
// Add data to the form to post.
WWWForm mediaForm = new WWWForm();
mediaForm.AddField("media", System.Convert.ToBase64String(File.ReadAllBytes(filePath)));
print (System.Convert.ToBase64String (File.ReadAllBytes (filePath)));
// HTTP header
var mediaHeaders = new Hashtable();
mediaHeaders["Authorization"] = GetHeaderWithAccessToken("POST", UploadMediaURL, consumerKey, consumerSecret, response, mediaParameters);
WWW mw = new WWW(UploadMediaURL, mediaForm.data, mediaHeaders);
yield return mw;
if (!string.IsNullOrEmpty (mw.error)) {
Debug.Log(string.Format("PostTweet - failed. {0}"));
} else {
SetMediaWeb(mw);
}
}
当我调用上述方法时,出现以下错误:
UriFormatException:Uri 超过了最大 {0} 个字符。 System.Uri.EscapeDataString(System.String stringToEscape)
仅供参考,图片不大,只有23 kb,图片类型为png。
我的代码有什么问题。感谢您的帮助。
【问题讨论】:
-
异常是否来自对 new WWW(...) 的调用?什么是堆栈跟踪?
标签: c# twitter unity3d upload media