How to use OAuth to connect to Twitter in PowerShell - Updated

Update 2010-07-10: added support for PIN and link for library download, because there is no new release at DevDefined home page.

When I was working on a Microblog reader for Twitter and, I was thinking about using OAuth for authorization purposes. Recently I have started again. Now I can show you full working example from how to register an application up to how to get the data.

Register your application

If you have an application that will request some data from a service and want to use OAuth for authorization, you have to register it first, so that the service knows about the application. In my case service will be Twitter and the application (consumer in OAuth terminology) will be PowerShell.

First go to Twitter and log in using your standard credentials and browse to Settings–>Connections. In the right column go to the Developers section and click on the link that points to On this page you can see all applications you have registered so far. To create new application (probably your first one), click on Register a new application and fill the info.

First part of registration Second part of registration

Registered After you submit the form, you will receive your applications key and secret (consumer key / consumer secret). This two hashes are used by your application when trying to get authorization key from the service (Twitter). Ok, it's time to play with them.

Authorize and request data

You can implement OAuth protocol on your own or you can use existing implementations. First time I tried an implementation by Shannon Whitley. It didn't work as expected (some problems with token expiration). Then I downloaded from Google code / DevDefined, that worked without problems.

Update 2010-07-10: there is no new release available, although the code is still alive. You have to either download the project and compile it or download the library I compiled for you. The old version worked fine, but some time later Twitter introduced PIN. The source code reflects new changes, but no release has been issued so far..

[3] Add-Type -Path C:\OAuthDevDefined\DevDefined.OAuth.dll
[4] $cons = New-Object devdefined.oauth.consumer.oauthconsumercontext

Use the keys provided by Twitter and set the signature method.

[5] $cons.ConsumerKey = '6NoGCtBEDdZGZtKe7JWdw'
[6] $cons.ConsumerSecret = 'lPkVk1PUCdNe7yXrGBI5fGO1UNjyU4rXOUzHt2SdvE'
[7] $cons.SignatureMethod = [devdefined.oauth.framework.signaturemethod]::HmacSha1

Create an OAuth session and request for authorization. PowerShell has to redirect you to Twitter page where you will allow the access for our application.

[8] $session = new-object DevDefined.OAuth.Consumer.OAuthSession `
 $cons,"", `
[9] $rtoken = $session.GetRequestToken()  #unique token just for authorization
[10] $authLink = $session.GetUserAuthorizationUrlForToken($rtoken, 'anything'); $authLink
[11] [diagnostics.process]::start($authLink)  #redirection to Twitter

Prompt to allow access Access granted, PIN issued. A browser window should appear and you are requested to allow the access. After that you will see a PIN that you have to pass as the request parameter. We will request for access token that will identify our powershell client and then we will download last 5 statuses and parse user names from them.

[12] $pin = read-host -prompt 'Enter PIN that you have seen at Twitter page'
[13] $accessToken = $session.ExchangeRequestTokenForAccessToken($rtoken, $pin)
[14] $accessToken | Export-CliXml c:\temp\myTwitterAccessToken.clixml

Now you got your access token that contains keys for later use. The token is stored in a xml file. Currently the Twitter's access token doesn't expire, so you just need to store it and use later.
Ok, "use", but how? It is very similar to what you have seen so far:

[1] Add-Type -Path C:\OAuthDevDefined\DevDefined.OAuth.dll
# create context (provide correct keys)
[2] $cons = New-Object devdefined.oauth.consumer.oauthconsumercontext
[3] $cons.ConsumerKey = '6NoGCtBEDdZGZtKe7JWdw'
[4] $cons.ConsumerSecret = 'lPkVk1PUCdNe7yXrGBI5fGO1UNjyU4rXOUzHt2SdvE'
[5] $cons.SignatureMethod = [devdefined.oauth.framework.signaturemethod]::HmacSha1
# create session; I pass $null and not full urls. It looks weird, maybe 
# there is a more elegant way how to create the session
[6] $session = new-object DevDefined.OAuth.Consumer.OAuthSession $cons, $null, $null, $null
# create access token and fill its data
[7] $accessToken = new-object DevDefined.OAuth.Framework.TokenBase
[8] $at = import-cliXml C:\temp\myTwitterAccessToken.clixml
[9] $accessToken.ConsumerKey, $accessToken.Realm, $accessToken.Token, $accessToken.TokenSecret = `
  $at.ConsumerKey, $at.Realm, $at.Token, $at.TokenSecret
# finally, create request and read response
[10] $req = $session.Request($accessToken)
[11] $req.Context.RequestMethod = 'GET'
[12] $req.Context.RawUri = [Uri]''
[13] $res = [xml][DevDefined.OAuth.Consumer.ConsumerRequestExtensions]::ReadBody($req)
[14] $res.statuses.status | % { $_.user.Name }
Michal Těhník
Jeffery Hicks
Jon Skeet
Martin Hassman
David Grudl


More info

Meta: 2010-07-10, Pepa