Unity3d将图像上传到Twitter

时间:2016-05-13 11:32:09

标签: c# twitter unity3d

我可以登录和发送短信,但无法在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);
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:2)

这在Twitter上没有出现是完全合理的。您当前提供的代码仅用于将图像上传到Twitter服务,然后可以在Twitter状态更新中附加。

  

例如,media_id值可用于使用状态/更新端点创建带有附加照片的推文。 source

上传图片后,接收可在update

中使用的media_id
  

更新身份验证用户的当前状态,也称为推特。

修改

  

这是我使用Media_Id发布源代码。当我在没有media_id的情况下使用它时它会成功发布。但是当我给它一个media_id参数时,它会给我一个错误

     

失败。 401未经授权{“错误”:[{“代码”:32,“消息”:“无法对您进行身份验证。”}}}

您收到此错误是因为您未经过验证。正如错误本身也是如此。正如update文档中提到的那样,在uploading media时,您必须记住一些事项。如此处所述,计算OAuth签名时不使用POST和查询参数,这与一般推文不同。

  

由于该方法使用多部分POST ,因此OAuth的处理方式略有不同。计算OAuth签名基本字符串或签名时,不使用POST或查询字符串参数。仅使用oauth_ *参数。

Why no tweet在此身份验证方面有一些非常好的答案。