0

msdn の例のコード例に基づいて画像をツイートしようとしていますが、うまくいきません。TwitterがAPIポリシーを変更するのですか?ここに私のコードがあります

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

msdn の例のように photochooser タスクから写真を撮る代わりに、isolatedstorage に保存された writeablebitmap から写真を撮るように、コードに少し調整を加えましたが、msdn のものとまったく同じものを既に試して、エラー TWEET_POST_ERR_FAILED を取得しました。

4

1 に答える 1

0

5 番目のパラメータとして文字列 "form-data" を restRequest.AddFile に追加してみてください。

restRequest.AddFile("media[]", image.jpg, ms, "image/jpg","form-data");
于 2014-08-11T21:07:56.463 に答える