简体   繁体   English

Unity3d将图像上传到Twitter

[英]Unity3d Upload a Image to Twitter

I am able to login and tweet text message but not able to share an image on twitter. 我可以登录和发短信,但不能在Twitter上分享图片。

When I upload the image it gives the response, but when I did not post image on twitter, I got the following response. 当我上传图像时,它给出了响应,但是当我没有在Twitter上发布图像时,得到了以下响应。

This is my code :- 这是我的代码 :-

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

Response :- 回应 :-

{
"media_id":700577726298599424,
"media_id_string":"700577726298599424",
"size":9734,
"expires_after_secs":86400,
"image":
        {
         "image_type":"image\/png",
         "w":435,
         "h":159
         }
}

This is my Posting Source Code With Media_Id. 这是我的Media_Id发布源代码。 When I use it without media_id it posts successfully. 当我不带media_id使用它时,它会成功发布。 But when I give a media_id parameter with it, it gives me an error 但是当我给它一个media_id参数时,它给了我一个错误

failed. 失败了 401 Unauthorized{"errors":[{"code":32,"message":"Could not authenticate you."}]} 401未经授权{“错误”:[{“代码”:32,“消息”:“无法验证您的身份。”}]}

Source Code :- 源代码

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

It makes perfect sense that this does not appear on twitter. 完全没有出现在Twitter上是完全合理的。 The code you are currently provided is only meant to upload a image to the twitter service, which then can be attached in twitter status updates. 当前提供的代码仅用于将图像上传到twitter服务,然后可以将其附加到twitter状态更新中。

For example, a media_id value can be used to create a Tweet with an attached photo using the statuses/update endpoint. 例如,可以使用media_id值使用状态/更新端点来创建带有附带照片的Tweet。 source 资源

So after uploading your image, and receiving the media_id which can be used in the update 因此,在上传您的图片并收到可在更新中使用的media_id

Updates the authenticating user's current status, also known as Tweeting. 更新身份验证用户的当前状态,也称为Tweeting。

Edit 编辑

This is my Posting Source Code With Media_Id. 这是我的Media_Id发布源代码。 When I use it without media_id it posts successfully. 当我不带media_id使用它时,它会成功发布。 But when I give a media_id parameter with it, it gives me an error 但是当我给它一个media_id参数时,它给了我一个错误

failed. 失败了 401 Unauthorized{"errors":[{"code":32,"message":"Could not authenticate you."}]} 401未经授权{“错误”:[{“代码”:32,“消息”:“无法验证您的身份。”}]}

You are receiving this error because you are not being verified. 您收到此错误,因为您没有得到验证。 As the error itself states as well. 由于错误本身也是如此。 As mentioned in the update docs there are some things you have to keep in mind when uploading media . 更新文档中所述, 上传媒体时,您需要记住一些事情。 As mentioned here, POST and query parameters are not used when calculation an OAuth signature, unlike a general tweet. 如此处所述,与普通推文不同,在计算OAuth签名时不使用POST和查询参数。

Because the method uses multipart POST , OAuth is handled a little differently. 由于该方法使用多部分POST ,因此OAuth的处理方式略有不同。 POST or query string parameters are not used when calculating an OAuth signature basestring or signature. 计算OAuth签名基本字符串或签名时,不使用POST或查询字符串参数。 Only the oauth_* parameters are used. 仅使用oauth_ *参数。

Why no tweet has some pretty good answers in regards to this authentication. 为什么没有任何推文对此认证有一些很好的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM