简体   繁体   English

推特与图片不起作用

[英]tweet with picture not working

I trying to tweet with picture based on code example from msdn example but it's not working, any idea why? 我试图根据msdn示例中的代码示例发布图片,但它不起作用,为什么? is it twitter changing api policy? 是Twitter更改了api政策吗? here is my code 这是我的代码

public void postToTwitter()
{
    try
    {
        var image = new WriteableBitmap(430, 400);
        image.Render(locationPict, null);
        image.Invalidate();
        MemoryStream ms = new MemoryStream();
        image.SaveJpeg(ms, 430, 400, 0, 100);

        const string filename = "/Shared/ShellContent/image.jpg";

        using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (!isf.DirectoryExists("/post"))
            {
                isf.CreateDirectory("/post");
            }
            using (var stream = isf.OpenFile(filename, System.IO.FileMode.OpenOrCreate))
            {
                image.SaveJpeg(stream, 430, 400, 0, 100);
            }
        }

        var credentials = new OAuthCredentials
        {
            Type = OAuthType.ProtectedResource,
            SignatureMethod = OAuthSignatureMethod.HmacSha1,
            ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
            ConsumerKey = twitterAppSettings.consumerKey,
            ConsumerSecret = twitterAppSettings.consumerKeySecret,
            Token = MainUtil.GetKeyValue<string>("twitterAccessToken"),
            TokenSecret = MainUtil.GetKeyValue<string>("twitterAccessTokenSecret"),
            Version = "1.0"
        };

        var restClient = new RestClient
        {
            Authority = "https://api.twitter.com",
            HasElevatedPermissions = true 
        };

        var restRequest = new RestRequest
        {
            Credentials = credentials,
            Path = "1.1/statuses/update_with_media.json",
            Method = WebMethod.Post 
        };

        restRequest.AddField("status", textpost.Text);
        restRequest.AddFile("media[]", image.jpg, ms, "image/jpg");
        restClient.BeginRequest(restRequest, new RestCallback(PostTweetRequestCallback));
    }
    catch(Exception ex)
    {
        string message = "Tweet failed! Exception details: " + ex.ToString();
        MessageBox.Show(message);
    }
}

private void PostTweetRequestCallback(RestRequest request, RestResponse response, object obj)
{
    Deployment.Current.Dispatcher.BeginInvoke(() =>
    {
        if (response.StatusCode == HttpStatusCode.OK)
        {
            MessageBox.Show("TWEET_POSTED_SUCCESSFULLY");
        }
        else if (response.StatusCode == HttpStatusCode.Forbidden)
        {
            MessageBox.Show("TWEET_POST_ERR_UPDATE_LIMIT");
        }
        else
        {
            MessageBox.Show("TWEET_POST_ERR_FAILED");
        }
    });
} 

I have add a little tweak into the code so that instead taking picture from photochooser task like in msdn example, I'm taking it from writeablebitmap that saved into isolatedstorage, but I also already tried the exactly like the one in msdn and getting error TWEET_POST_ERR_FAILED 我在代码中做了一些调整,以便代替像msdn示例中那样从photochooser任务拍照,而是从保存到隔离存储中的writeablebitmap中获取它,但是我也已经尝试过与msdn中的完全一样,并得到错误TWEET_POST_ERR_FAILED

尝试将字符串“ form-data”作为第五个参数添加到restRequest.AddFile:

restRequest.AddFile("media[]", image.jpg, ms, "image/jpg","form-data");

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

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