直接上传图片到Twitter [英] Upload image directly to twitter
问题描述
我需要在Windows Phone 7的直接上传图片到Twitter的帮助。
I need help in uploading images directly to twitter in Windows Phone 7.
我与Twitter的OAuth的流程进行,也可以可以更新微博,但我没有之所以能上传图片使用WP7来Twitter?
I am done with oauth flow of twitter and can also could update tweets but I have not been able to upload image to twitter using wp7?
推荐答案
我已经制定了一个解决方案,通过使用Hammock.WindowsPhone .Mango库。
(TweetSharp内部使用吊床库的OAuth和其他功能,但我从来没有使用TweetSharp或Twitterizer)
I have worked out a solution for this, by using the Hammock.WindowsPhone.Mango library. (TweetSharp internally uses Hammock library for oAuth and other functionalities, but I have never used TweetSharp or Twitterizer)
我从安装了最新版本的吊床< A HREF =http://nuget.org/packages/Hammock>的NuGet
I have installed the latest version of Hammock from Nuget
和则用于照片上传到Twitter下面的代码:
And then the following code is used for photo upload to Twitter:
public void uploadPhoto(Stream photoStream, string photoName)
{
var credentials = new OAuthCredentials
{
Type = OAuthType.ProtectedResource,
SignatureMethod = OAuthSignatureMethod.HmacSha1,
ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
ConsumerKey = TwitterSettings.consumerKey,
ConsumerSecret = TwitterSettings.consumerKeySecret,
Token = TwitterSettings.accessToken,
TokenSecret = TwitterSettings.accessTokenSecret,
Version = "1.0a"
};
RestClient restClient = new RestClient
{
Authority = "https://upload.twitter.com",
HasElevatedPermissions = true,
Credentials = credentials,
Method = WebMethod.Post
};
RestRequest restRequest = new RestRequest
{
Path = "1/statuses/update_with_media.json"
};
restRequest.AddParameter("status", tbxNewTweet.Text);
restRequest.AddFile("media[]", photoName, photoStream, "image/jpg");
}
restClient.BeginRequest(restRequest, new RestCallback(PostTweetRequestCallback));
}
private void PostTweetRequestCallback(RestRequest request, Hammock.RestResponse response, object obj)
{
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
//Success code
}
}
下面,
photoName距离PhotoChooserTask
Here, photoName is the name of the image selected ( "e.OriginalFileName") photoStream is the "e.ChosenPhoto" from the PhotoChooserTask
和.AddFile(第4个参数)应采取照顾(我还没有考虑其他格式,而这样做的样品,你要照顾好你的应用程序)
and the 4th parameter for .AddFile() should be taken care (I have not considered other formats while doing this sample, you have to take care in your apps)
我希望这有助于!
这篇关于直接上传图片到Twitter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!