我可以登錄併發送短信,但無法在Twitter上分享圖像。Unity3d將圖像上傳到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未授權{ 「錯誤」:[{ 「代碼」:32, 「消息」: 「無法驗證您的身份。」}]}
源代碼: -
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);
}
}
}
}
您使用的代碼在哪裏? –
我在我的代碼中編輯過。請參考它並提供一個可能的解決方案。謝謝提前。 –