2016-05-13 127 views
0

我可以登錄併發送短信,但無法在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); 
       } 
      } 
     } 
    } 
+0

您使用的代碼在哪裏? –

+0

我在我的代碼中編輯過。請參考它並提供一個可能的解決方案。謝謝提前。 –

回答

1

它的意義這不會出現在Twitter上。您目前提供的代碼僅用於將圖片上傳到twitter服務,然後可以將其添加到twitter狀態更新中。

例如,media_id值可用於使用狀態/更新端點創建帶有附加照片的Tweet。 source

所以上傳您的圖片,並接收可在update

更新所使用的身份驗證用戶的當前狀態,也被稱爲啁啾media_id後。

編輯

這是我發佈的源代碼Media_Id。當我沒有使用media_id時,它會成功發佈。但是,當我給一個media_id參數它,它給了我一個錯誤

失敗。 401未授權{「errors」:[{「code」:32,「message」:「無法驗證您的身份。」}}}

您收到此錯誤是因爲您未驗證。正如錯誤本身所述。正如在update文檔中提到的,在uploading media時,您必須記住一些事項。如上所述,計算OAuth簽名時不會使用POST和查詢參數,這與常規推文不同。

因爲該方法使用多部分POST,所以OAuth處理方式稍有不同。計算OAuth簽名基礎字符串或簽名時,不會使用POST或查詢字符串參數。僅使用oauth_ *參數。

Why no tweet對於此驗證有一些相當不錯的答案。

+0

我需要一些更多的信息。你能否提供我的源代碼,因爲我無法完成它。提前致謝。 –

+0

@Flexsin_Unity究竟是什麼問題?你提到你已經可以成功地發表文章了。添加圖像只是對此的補充。 –

+0

什麼是我需要知道的輕微增加。我在那件事上掙扎很多。但我沒有把它完成。請幫助我將圖像文件發佈到Twitter。 –