c# - TweetSharp upload image with PhotoChooserTask and SendTweetWithMedia -
i'm trying upload picture on twitter of photochoosertask catching him. problem have not know how pass bitmapimage, format when choose image , pass stream.
below sample of code:
namespace twitter { public partial class twitteruploadimage : phoneapplicationpage { // constructor twitterservice service; private const string consumerkey = ""; private const string consumersecret = ""; private oauthrequesttoken requesttoken; private oauthaccesstoken accestoken; private bool userauthenticated = false; //uri uri = new uri("/background.png", urikind.relative); //stream imagestream = application.getresourcestream(uri); stream imagestream; string namephoto; public twitteruploadimage() { initializecomponent(); service = new twitterservice(consumerkey,consumersecret); //chek if have autehntification data var token = getaccesstoken(); if (token != null) { service.authenticatewith(token.token, token.tokensecret); userauthenticated = true; } } //protected override void onnavigatedto(navigationeventargs e) //{ // // dictionary of query string keys , values. // idictionary<string, string> querystrings = this.navigationcontext.querystring; // // ensure there @ least 1 key in query string, , check whether "fileid" key present. // if (querystrings.containskey("fileid")) // { // // retrieve photo media library using fileid passed app. // medialibrary library = new medialibrary(); // picture photofromlibrary = library.getpicturefromtoken(querystrings["fileid"]); // // create bitmapimage object , add set image control source. // // retrieve full-resolution image, use getimage() method instead. // bitmapimage bitmapfromphoto = new bitmapimage(); // bitmapfromphoto.setsource(photofromlibrary.getpreviewimage()); // image1.source = bitmapfromphoto; // } //} private void tweetclick(object sender, routedeventargs e) { if (userauthenticated) tweet(message.text, imagestream); else service.getrequesttoken(processrequesttoken); } private void processrequesttoken(oauthrequesttoken token, twitterresponse response) { if (token == null) dispatcher.begininvoke(() => { messagebox.show("error obtaining request token"); }); else { requesttoken = token; dispatcher.begininvoke(() => { browser.visibility = system.windows.visibility.visible; browser.navigate(service.getauthorizationuri(requesttoken)); }); } } private void processaccesstoken(oauthaccesstoken token, twitterresponse response) { if (token == null) dispatcher.begininvoke(() => { messagebox.show("error obtaining access token"); }); else { accestoken = token; service.authenticatewith(token.token, token.tokensecret); saveaccesstoken(token); userauthenticated = true; dispatcher.begininvoke(() => { tweet(message.text, imagestream); }); } } private void tweet(string message, stream imagestream) { //sendtweetoptions msg = new sendtweetoptions(); //msg.status = message; //service.sendtweet(msg, tweetresponse); //medialibrary library = new medialibrary(); //picture picture = library.getpicturefromtoken(namephoto); ////stream stream = picture.tostring; //var stream = library.getpicturefromtoken(namephoto); photochoosertask photochoosertask = new photochoosertask(); photochoosertask.completed += new eventhandler<photoresult>(photochoosertask_completed); photochoosertask.show(); sendtweetwithmediaoptions msgmedia = new sendtweetwithmediaoptions(); msgmedia.status = message; dictionary<string, stream> imagedict = new dictionary<string, stream> { { "test", imagestream } }; //imagedict.add(imagepath, imagestream); msgmedia.images = imagedict; //status = service.sendtweetwithmedia(new sendtweetwithmediaoptions() { status = readermsg.message, images = imagedict }); service.sendtweetwithmedia(msgmedia, tweetresponse); ////var service = getauthenticatedservice(); //using (var stream = new filestream("daniel_8bit.png", filemode.open)) //{ // var tweet = service.sendtweetwithmedia(new sendtweetwithmediaoptions // { // status = "can_tweet_with_image:tweet", // images = new dictionary<string, stream> {{"test", stream}} // }); // //assert.isnotnull(tweet); // //assert.arenotequal(0, tweet.id); //} //service.sendtweetwithmedia(new sendtweetwithmediaoptions //{ // status = "can_tweet_with_image:tweet", // images = new dictionary<string, stream> { { "test", imagestream } } //}); } private void tweetresponse(twitterstatus tweet, twitterresponse response) { if (response.statuscode == httpstatuscode.ok) { dispatcher.begininvoke(() => { messagebox.show("tweet posted successfully"); }); } else { if (response.statuscode == httpstatuscode.unauthorized) { saveaccesstoken(null); userauthenticated = false; dispatcher.begininvoke(() => { messagebox.show("authentication error"); }); } else dispatcher.begininvoke(() => { messagebox.show("error, please try again later"); }); } } private void browsernavitaged(object sender, system.windows.navigation.navigationeventargs e) { if (e.uri.absoluteuri.contains("oauth_verifier")) { var values = parsequerystring(e.uri.absoluteuri); string verifier = values["oauth_verifier"]; service.getaccesstoken(requesttoken, verifier, processaccesstoken); dispatcher.begininvoke(() => { browser.visibility = system.windows.visibility.collapsed; }); } } private void saveaccesstoken(oauthaccesstoken token) { if (isolatedstoragesettings.applicationsettings.contains("accesstoken")) isolatedstoragesettings.applicationsettings["accesstoken"] = token; else isolatedstoragesettings.applicationsettings.add("accesstoken", token); isolatedstoragesettings.applicationsettings.save(); } private oauthaccesstoken getaccesstoken() { if (isolatedstoragesettings.applicationsettings.contains("accesstoken")) return isolatedstoragesettings.applicationsettings["accesstoken"] oauthaccesstoken; else return null; } protected override void onbackkeypress(system.componentmodel.canceleventargs e) { if (browser.visibility == system.windows.visibility.visible) { browser.visibility = system.windows.visibility.collapsed; e.cancel = true; } base.onbackkeypress(e); } // hammock.extensions.stringextensions.cs public static idictionary<string, string> parsequerystring(string query) { // [dc]: method not url decode, , cannot handle decoded input if (query.startswith("?")) query = query.substring(1); if (query.equals(string.empty)) { return new dictionary<string, string>(); } var parts = query.split(new[] { '&' }); return parts.select( part => part.split(new[] { '=' })).todictionary( pair => pair[0], pair => pair[1] ); } private void takepicture(object sender, routedeventargs e) { //photochoosertask photochoosertask = new photochoosertask(); //photochoosertask.completed += new eventhandler<photoresult>(photochoosertask_completed); //photochoosertask.show(); } void photochoosertask_completed(object sender, photoresult e) { if (e.taskresult == taskresult.ok) { var img = new bitmapimage(); img.setsource(e.chosenphoto); imagestream = img; //namephoto = e.originalfilename; //bitmapimage image = new bitmapimage(); //image.setsource(e.chosenphoto); //this.img.source = image; } } } }
i had similar problem, solution same put question , after time found solution self, here solution. cannot use sendtweetwithmediaoptions in windows phone
Comments
Post a Comment