如何使用unity3d在twitter上分享图像

时间:2016-02-19 07:22:58

标签: c# twitter unity3d

我想通过unity3d在Twitter上登录和分享。我在Twitter开发者网站上搜索了很多没有官方插件用于统一。我知道有可用于在Twitter上登录和共享的api。我从资产商店下载了一个插件用于twitter集成。在该插件中,我能够登录和发送短信,但无法在twitter上分享图像。下面是我获得一些参考如何在Twitter上传图像的链接。

C#: Upload Photo To Twitter From Unity

我遵循同样的事情,但是当我上传图片时,它会给出回复,但是当我没有在twitter上发布图片时,我会得到回应。

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

下面是在twitter上上传图片的代码。

private const string UploadMediaURL = "https://upload.twitter.com/1.1/media/upload.json";

public static IEnumerator UploadImageOnTwitter(string ImagePath, string consumerKey, string consumerSecret, AccessTokenResponse response, PostTweetCallback callback)
    {
        Dictionary<string, string> parameters = new Dictionary<string, string>();

        var imageData = File.ReadAllBytes(Application.persistentDataPath +"/Image.png");

        string encoded64ImageData = Convert.ToBase64String(imageData);

        parameters.Add("media_data", encoded64ImageData );

        // Add data to the form to post.
        WWWForm form = new WWWForm();
        form.AddField( "media_data", encoded64ImageData );

        // HTTP header
        Dictionary<string, string> headers = new Dictionary<string, string>();
        string url = UploadMediaURL;
        string auth = GetHeaderWithAccessToken("POST", url, consumerKey, consumerSecret, response, parameters);

        headers.Add( "Authorization", auth );
        headers.Add( "Content-Transfer-Encoding", "base64" );

        Debug.Log ("response is "+response);

        WWW web = new WWW(url, form.data, headers);
        yield return web;

        Debug.Log (web.text);
    }

1 个答案:

答案 0 :(得分:1)

以上代码上传媒体但不发布。你也需要发布结果。

    private const string UploadMediaURL = "https://upload.twitter.com/1.1/media/upload.json";
   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)
    {
        Dictionary<string, string> mediaParameters = new Dictionary<string, string>();

        var imageData = File.ReadAllBytes(Application.persistentDataPath +"/Image.png");

        string encoded64ImageData = Convert.ToBase64String(imageData);

        mediaParameters.Add("media_data", encoded64ImageData );

    // Add data to the form to post.
        WWWForm mediaForm = new WWWForm();
        mediaForm.AddField( "media_data", encoded64ImageData );

    // HTTP header
        Dictionary<string, string> mediaHeaders = new Dictionary<string, string>();
        string auth = GetHeaderWithAccessToken("POST", UploadMediaURL, consumerKey, consumerSecret, response, mediaParameters);

        mediaHeaders.Add( "Authorization", auth );
        mediaHeaders.Add( "Content-Transfer-Encoding", "base64" );

        WWW mw = new WWW(UploadMediaURL, mediaForm.data, mediaHeaders);
        yield return mw;

        string mID = Regex.Match(mw.text, @"(\Dmedia_id\D\W)(\d*)").Groups[2].Value;

        Debug.Log ("response from media request : " + mw.text);

        Debug.Log ("mID = " + mID);

        if (!string.IsNullOrEmpty (mw.error)) {
            Debug.Log (string.Format ("PostTweet - failed. {0}\n{1}", mw.error, mw.text));
            callback (false);
        } else {
            string error = Regex.Match (mw.text, @"<error>([^&]+)</error>").Groups [1].Value;

            if (!string.IsNullOrEmpty (error)) {
                Debug.Log (string.Format ("PostTweet - failed. {0}", error));
                callback (false);
            } else {
                callback (true);
            }
        }

        Dictionary<string, string> parameters = new Dictionary<string, string>();
        parameters.Add("status", text);
        parameters.Add ("media_ids", mID);

        // Add data to the form to post.
        WWWForm form = new WWWForm();
        form.AddField("status", text);
        form.AddField ("media_ids", mID);

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