2016-05-13 125 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。 –