Here’s a simple example of how to use GOAuth to authorize via an Out Of Band (OOB) mode.
OOB is used when the application requiring OAuth access doesn’t run a HTTP service. For example a CLI client.
In this example we’re going to connect to create a CLI application that Authorizes & updates a twitter status.
Install GOAuth from github
goinstall github.com/hokapoka/goauth
This example is included in the source & package that has just been installed via goinstall.
Firstly you need to gather the URLs that are used to get the access token.
- http://twitter.com/oauth/request_token
- This is the URL the Consumer obtains a Request Token from.
- http://twitter.com/oauth/authorize
- This is the URL the user is directed to access and Authorize the Request to connect, using the Request Token obtained previously.
- http://twitter.com/oauth/access_token
- This is the URL that we return to with Authorization Number that return the Access Token that can be used to access the API
- http://api.twitter.com/1/statuses/update.json
- Once we have the Access token we are able to use it with this URL to update the users twitter status.
Additionally you need to obtain your Consumer Key & Consumer Secret these are used by the consumer to Sign the Header Params sent to the OAuth Service.
Creating the Client
In a new .go program file import the GOAuth package & declare an OAuthConsumer & AccessToken for use later.
package main import ( oauth "github.com/hokapoka/goauth" "fmt" ) var goauthcon *oauth.OAuthConsumer var AT *oauth.AccessToken
Within the func main() create the Instance of the OAuthConcumer setting the params as shown below.
Note : the CallBackURL is set to “oob”, this tells the service is expected to present the user with a number rather than callback the service.
goauthcon = &oauth.OAuthConsumer{
Service:"twitter",
RequestTokenURL:"http://twitter.com/oauth/request_token",
AccessTokenURL:"http://twitter.com/oauth/access_token",
AuthorizationURL:"http://twitter.com/oauth/authorize",
ConsumerKey:"change me",
ConsumerSecret:"change me",
CallBackURL:"oob",
}
As soon as you have created the Consumer You can obtain a the Authoriation URL via GetRequestAuthorizationURL(). This method returns the Request Token from the Service and the URL that the user is to be directed to.
Note if you receive an error when obtaining the URL, double check your consumer key/secret.
s, rt, err := goauthcon.GetRequestAuthorizationURL()
if err != nil {
fmt.Println(err.String())
return
}
Once you have the URL to the user needs to visit you need to direct the user to the URL and wait for the to input the PIN number that has been presented to the user.
Using the fmt packages Scanln method, we pass the pointer to a string that is going to be populated with the value they type into the console.
We then pass the string that contains the pin number and the Request Token to the Consumer’s GetAccessToken.
This will return an AccessToken that can be used to interact with the Services EndPoints.
var pin string
fmt.Printf("Open %s In your browser.n Allow access and then enter the PIN numbern", s);
fmt.Printf("PIN Number: ")
fmt.Scanln(&pin)
at := goauthcon.GetAccessToken(rt.Token, pin)
Update twitter Status
Now we have the AccessTOken we can use it to update the Status.
Using the POST method to the relevant EndPoint, passing the Parameters & the access token. Only one parameter is sent (status=some_text), there are sent using GoLang’s Composite literal notation as shown below.
_, err = goauthcon.Post(
"http://api.twitter.com/1/statuses/update.json",
oauth.Params{
&oauth.Pair{
Key:"status",
Value:"Testing Status Update via GOAuth - OAuth consumer for #Golang"
},
},
at )
if err != nil {
fmt.Println(err.String())
return
}
fmt.Println("Twitter Status is updated")
That’s it, your twitter status will have been updated.