简体   繁体   中英

Unity3d Upload a Image to Twitter

I am able to login and tweet text message but not able to share an image on twitter.

When I upload the image it gives the response, but when I did not post image on twitter, I got the following response.

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. When I use it without media_id it posts successfully. But when I give a media_id parameter with it, it gives me an error

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

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. 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.

For example, a media_id value can be used to create a Tweet with an attached photo using the statuses/update endpoint. source

So after uploading your image, and receiving the media_id which can be used in the update

Updates the authenticating user's current status, also known as Tweeting.

Edit

This is my Posting Source Code With Media_Id. When I use it without media_id it posts successfully. But when I give a media_id parameter with it, it gives me an error

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

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.

Because the method uses multipart POST , OAuth is handled a little differently. POST or query string parameters are not used when calculating an OAuth signature basestring or signature. Only the oauth_* parameters are used.

Why no tweet has some pretty good answers in regards to this authentication.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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