authentication - Publishing tweets from C# Windows service using Tweetinvi or similar -
i looking publishing service status updates on twitter using tweetinvi, seems library doing sort of thing, starting out looking using not set in stone.
however, 1 thing research has not yielded yet, obvious way handle twitter authentication in headless service. have created app twitter, have consumer key , secret, , can "app only" auth request user info, followers etc., of course have no right publish tweets.
so ambition (once out of beta) create proper twitter account, somehow have service authenticate towards account, , publish status updates general service @ defined intervals. simple idea.
of course, can pin based authentication mentioned here:
https://github.com/linvi/tweetinvi/wiki/authentication
i can run manually, pin code, , proceed workflow. require reauthentication @ regular intervals, or valid "forever"? looking way make automatic possible, , having redo auth every x hours huge dent in dream, if not showstopper.
of course have password twitter account used publish statuses, don't see way old fashioned login without manual user intervention - options have?
this behavior design. twitter uses oauth, protocol purpose of allowing user authorize application. user because otherwise, or else can perform actions on behalf without them knowing.
with in mind, way have user explicitly authorize app. here's example of how linq twitter, wrote, using asp.net mvc. when user visit's page, can have button re-directs them oauthcontroller
below beginasync
action.
using system; using system.configuration; using system.linq; using system.threading.tasks; using system.web.mvc; using linqtotwitter; namespace mvcdemo.controllers { public class oauthcontroller : asynccontroller { public actionresult index() { return view(); } public async task<actionresult> beginasync() { //var auth = new mvcsigninauthorizer var auth = new mvcauthorizer { credentialstore = new sessionstatecredentialstore { consumerkey = configurationmanager.appsettings["consumerkey"], consumersecret = configurationmanager.appsettings["consumersecret"] } }; string twittercallbackurl = request.url.tostring().replace("begin", "complete"); return await auth.beginauthorizationasync(new uri(twittercallbackurl)); } public async task<actionresult> completeasync() { var auth = new mvcauthorizer { credentialstore = new sessionstatecredentialstore() }; await auth.completeauthorizeasync(request.url); // how access credentials after authorization. // oauthtoken , oauthtokensecret not expire. // can use userid associate credentials user. // can save credentials way want - database, // isolated storage, etc. - it's you. // can retrieve , load 4 credentials on subsequent // queries avoid need re-authorize. // when you've loaded 4 credentials, linq twitter let // make queries without re-authorizing. // //var credentials = auth.credentialstore; //string oauthtoken = credentials.oauthtoken; //string oauthtokensecret = credentials.oauthtokensecret; //string screenname = credentials.screenname; //ulong userid = credentials.userid; // return redirecttoaction("index", "home"); } } }
after user authorizes application, twitter redirects them completeasync
method. notice comments on how extract values auth.credentialstore
. save in db , retrieve them in service make calls on user's behalf.
those credentials don't change, user can possibly de-authorize application @ time in future - @ time you'll need them authorize again. can entire sample code @ linq twitter asp.net samples page.
Comments
Post a Comment