twitter: Twitter API with oauth in Golang

GitHub license GoDoc Build Status

Installation and Usage


go get


Use Desktop Mode OAuth in Twitter

Use Desktop oauth mode to sign in Twitter

package main

import (

const (
	//Get consumer key and secret from
	ConsumerKey string = ""
	ConsumerSecret string = ""
func main() {
	twitterClient = NewDesktopClient(ConsumerKey, ConsumerSecret)
	//Show a UI to display URL.
	//Please go to this URL to get code to continue
	//Get timeline only latest one
	timeline, byteData, err :=twitterClient.QueryTimeLine(1)
	if err == nil {
		fmt.Println("timeline struct=", timeline, " byteData=", string(byteData) )

Use Server Mode OAuth in Twitter (Three-legged Authentication)

Refer to twitter document, use server oauth mode to sign in Twitter

Please note, you might get error if you don’t set Callback_URL in twitter setting. You need input valid URL:

  1. Could not be localhost, use other setting in hosts if you want to test it locally.
  2. Must need input any URL otherwise your app will treat as desktop app.

package main

import (

	. ""

var ConsumerKey string
var ConsumerSecret string
var twitterClient *ServerClient

func init() {
	ConsumerKey = os.Getenv("ConsumerKey")
	ConsumerSecret = os.Getenv("ConsumerSecret")

const (
	//This URL need note as follow:
	// 1. Could not be localhost, change your hosts to a specific domain name
	// 2. This setting must be identical with your app setting on twitter Dev
	CallbackURL string = ""

func main() {

	if ConsumerKey == "" && ConsumerSecret == "" {
		fmt.Println("Please setup ConsumerKey and ConsumerSecret.")

	var port *int = flag.Int(
		"Port to listen on.")


	fmt.Println("[app] Init server key=", ConsumerKey, " secret=", ConsumerSecret)
	twitterClient = NewServerClient(ConsumerKey, ConsumerSecret)
	http.HandleFunc("/maketoken", GetTwitterToken)
	http.HandleFunc("/request", RedirectUserToTwitter)
	http.HandleFunc("/follow", GetFollower)
	http.HandleFunc("/followids", GetFollowerIDs)
	http.HandleFunc("/time", GetTimeLine)
	http.HandleFunc("/user", GetUserDetail)
	http.HandleFunc("/", MainProcess)

	u := fmt.Sprintf(":%d", *port)
	fmt.Printf("Listening on '%s'\n", u)
	http.ListenAndServe(u, nil)

func MainProcess(w http.ResponseWriter, r *http.Request) {

	if !twitterClient.HasAuth() {
		fmt.Fprintf(w, "<BODY><CENTER><A HREF='/request'><IMG SRC=''></A></CENTER></BODY>")
	} else {
		//Logon, redirect to display time line
		timelineURL := fmt.Sprintf("http://%s/time", r.Host)
		http.Redirect(w, r, timelineURL, http.StatusTemporaryRedirect)

func RedirectUserToTwitter(w http.ResponseWriter, r *http.Request) {
	fmt.Println("Enter redirect to twitter")
	fmt.Println("Token URL=", CallbackURL)
	requestUrl := twitterClient.GetAuthURL(CallbackURL)

	http.Redirect(w, r, requestUrl, http.StatusTemporaryRedirect)
	fmt.Println("Leave redirtect")

func GetTimeLine(w http.ResponseWriter, r *http.Request) {
	timeline, bits, _ := twitterClient.QueryTimeLine(1)
	fmt.Println("TimeLine=", timeline)
	fmt.Fprintf(w, "The item is: "+string(bits))

func GetTwitterToken(w http.ResponseWriter, r *http.Request) {
	fmt.Println("Enter Get twitter token")
	values := r.URL.Query()
	verificationCode := values.Get("oauth_verifier")
	tokenKey := values.Get("oauth_token")

	twitterClient.CompleteAuth(tokenKey, verificationCode)
	timelineURL := fmt.Sprintf("http://%s/time", r.Host)

	http.Redirect(w, r, timelineURL, http.StatusTemporaryRedirect)

func GetFollower(w http.ResponseWriter, r *http.Request) {
	followers, bits, _ := twitterClient.QueryFollower(10)
	fmt.Println("Followers=", followers)
	fmt.Fprintf(w, "The item is: "+string(bits))

func GetFollowerIDs(w http.ResponseWriter, r *http.Request) {
	followers, bits, _ := twitterClient.QueryFollowerIDs(10)
	fmt.Println("Follower IDs=", followers)
	fmt.Fprintf(w, "The item is: "+string(bits))
func GetUserDetail(w http.ResponseWriter, r *http.Request) {
	followers, bits, _ := twitterClient.QueryFollowerById(2244994945)
	fmt.Println("Follower Detail of =", followers)
	fmt.Fprintf(w, "The item is: "+string(bits))

Inspired By


It is one of my project 52.


This package is licensed under MIT license. See LICENSE for details.


View Github