Sunday, 15 August 2010

api - Tweeting using Powershell -



api - Tweeting using Powershell -

i'm trying utilize powershell tweet can't work fully. i'm able create 1 or 2 tweets , yes wait few minutes in between each , dont repeat previous message.

then of sudden stops working, getrequeststream function hangs, i'm not sure whats happening, can shed lite on issue?

thanks in advance!

[reflection.assembly]::loadwithpartialname("system.net") $status = [system.uri]::escapedatastring("tweet tweet"); $oauth_consumer_key = "<key"; $oauth_consumer_secret = "<secret>"; $oauth_token = "<token>"; $oauth_token_secret = "<secret>"; $oauth_nonce = [system.convert]::tobase64string([system.text.encoding]::ascii.getbytes([system.datetime]::now.ticks.tostring())); $ts = [system.datetime]::utcnow - [system.datetime]::parseexact("01/01/1970", "dd/mm/yyyy", $null).touniversaltime(); $oauth_timestamp = [system.convert]::toint64($ts.totalseconds).tostring(); $signature = "post&"; $signature += [system.uri]::escapedatastring("https://api.twitter.com/1.1/statuses/update.json") + "&"; $signature += [system.uri]::escapedatastring("oauth_consumer_key=" + $oauth_consumer_key + "&"); $signature += [system.uri]::escapedatastring("oauth_nonce=" + $oauth_nonce + "&"); $signature += [system.uri]::escapedatastring("oauth_signature_method=hmac-sha1&"); $signature += [system.uri]::escapedatastring("oauth_timestamp=" + $oauth_timestamp + "&"); $signature += [system.uri]::escapedatastring("oauth_token=" + $oauth_token + "&"); $signature += [system.uri]::escapedatastring("oauth_version=1.0&"); $signature += [system.uri]::escapedatastring("status=" + $status); $signature_key = [system.uri]::escapedatastring($oauth_consumer_secret) + "&" + [system.uri]::escapedatastring($oauth_token_secret); $hmacsha1 = new-object system.security.cryptography.hmacsha1; $hmacsha1.key = [system.text.encoding]::ascii.getbytes($signature_key); $oauth_signature = [system.convert]::tobase64string($hmacsha1.computehash([system.text.encoding]::ascii.getbytes($signature))); $oauth_authorization = 'oauth '; $oauth_authorization += 'oauth_consumer_key="' + [system.uri]::escapedatastring($oauth_consumer_key) + '",'; $oauth_authorization += 'oauth_nonce="' + [system.uri]::escapedatastring($oauth_nonce) + '",'; $oauth_authorization += 'oauth_signature="' + [system.uri]::escapedatastring($oauth_signature) + '",'; $oauth_authorization += 'oauth_signature_method="hmac-sha1",' $oauth_authorization += 'oauth_timestamp="' + [system.uri]::escapedatastring($oauth_timestamp) + '",' $oauth_authorization += 'oauth_token="' + [system.uri]::escapedatastring($oauth_token) + '",'; $oauth_authorization += 'oauth_version="1.0"'; $post_body = [system.text.encoding]::ascii.getbytes("status=" + $status); [system.net.httpwebrequest] $request = [system.net.webrequest]::create("https://api.twitter.com/1.1/statuses/update.json"); $request.method = "post"; $request.headers.add("authorization", $oauth_authorization); $request.contenttype = "application/x-www-form-urlencoded"; $body = $request.getrequeststream(); $body.write($post_body, 0, $post_body.length); $body.flush(); $body.close(); $response = $request.getresponse(); $response.close()

this total error sequence when times out:

exception calling "getrequeststream" "0" argument(s): "the operation has timed out" @ line:46 char:5 + $body = $request.getrequeststream(); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + categoryinfo : notspecified: (:) [], methodinvocationexception + fullyqualifiederrorid : webexception exception calling "write" "3" argument(s): "the request aborted: connection closed unexpectedly." @ line:47 char:5 + $body.write($post_body, 0, $post_body.length); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + categoryinfo : notspecified: (:) [], methodinvocationexception + fullyqualifiederrorid : webexception exception calling "getresponse" "0" argument(s): "you must provide request body if set contentlength>0 or sendchunked==true. calling [begin]getrequeststream before [begin]getresponse." @ line:50 char:5 + $response = $request.getresponse(); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + categoryinfo : notspecified: (:) [], methodinvocationexception + fullyqualifiederrorid : protocolviolationexception

are closing response-stream? keep-alive connections default, if open many of them may blocked because of 2 many concurrent sessions. seek using:

$response.close()

api http powershell twitter

No comments:

Post a Comment