【问题标题】:Unity3d Upload a Image to TwitterUnity3d 将图像上传到 Twitter
【发布时间】:2016-09-09 14:10:57
【问题描述】:

我可以登录并发送短信,但无法在 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 Unauthorized{"errors":[{"code":32,"message":"Could not authenticate you."}]}

源代码:-

    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);
                }
            }
        }
    }

【问题讨论】:

  • 您用来执行此操作的代码在哪里?
  • 我在我的代码中进行了编辑。请参考它并给出一个可能的解决方案。提前致谢。

标签: c# twitter unity3d


【解决方案1】:

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

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

所以在上传你的图片后,收到media_id,可以在update中使用

更新验证用户的当前状态,也称为 Tweeting。

编辑

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

失败。 401 Unauthorized{"errors":[{"code":32,"message":"Could not authenticate you."}]}

您收到此错误是因为您没有得到验证。正如错误本身所说的那样。正如update 文档中所述,当uploading media 时,您必须牢记一些事情。正如这里提到的,与一般推文不同,计算 OAuth 签名时不使用 POST 和查询参数。

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

Why no tweet 对此身份验证有一些很好的答案。

【讨论】:

  • 我需要更多信息。您能否提供我的源代码,因为我无法完成它。提前致谢。
  • @Flexsin_Unity 你到底有什么问题?您提到您已经可以成功发布文本。添加图像只是对此的轻微补充。
  • 我需要知道的那一点点补充是什么。我在这件事上挣扎了很多。但我没有完成它。请帮助我将图像文件发布到 Twitter。
  • @Flexsin_Unity 请参阅update。您需要将media_id 作为参数包含在tweet 中。如果您在这方面需要更多帮助,我会要求您在问题中包含您当前用于在推文中发送文本的代码
  • :- 我在我的第一个问题 Source Code 中添加了带有媒体 ID 的定位代码。你可以请参考它。它给了我错误行:-失败。 401 Unauthorized {"errors":[{"code":32,"message":"Could not authenticate you."}]} 当我给出 media_id 时。非常感谢。
猜你喜欢
  • 2016-04-13
  • 2014-07-03
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 2012-06-28
  • 2015-01-10
  • 1970-01-01
  • 2016-05-31
相关资源
最近更新 更多