{"id":19156602,"url":"https://github.com/kkdai/twitter","last_synced_at":"2025-05-07T07:45:17.555Z","repository":{"id":2028882,"uuid":"45591855","full_name":"kkdai/twitter","owner":"kkdai","description":"Twitter API SDK with OAuth in Golang","archived":false,"fork":false,"pushed_at":"2022-10-30T16:39:12.000Z","size":32,"stargazers_count":21,"open_issues_count":2,"forks_count":13,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-19T20:17:15.917Z","etag":null,"topics":["go","twitter","twitter-api","twitter-sdk"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kkdai.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-11-05T06:16:37.000Z","updated_at":"2025-01-09T13:33:38.000Z","dependencies_parsed_at":"2022-09-21T03:00:50.008Z","dependency_job_id":null,"html_url":"https://github.com/kkdai/twitter","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kkdai%2Ftwitter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kkdai%2Ftwitter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kkdai%2Ftwitter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kkdai%2Ftwitter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kkdai","download_url":"https://codeload.github.com/kkdai/twitter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252838598,"owners_count":21812079,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","twitter","twitter-api","twitter-sdk"],"created_at":"2024-11-09T08:35:09.074Z","updated_at":"2025-05-07T07:45:17.535Z","avatar_url":"https://github.com/kkdai.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"twitter: Twitter API with oauth in Golang\n==============\n\n[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/kkdai/twitter/master/LICENSE)  [![GoDoc](https://godoc.org/github.com/kkdai/cyk?status.svg)](https://godoc.org/github.com/kkdai/twitter)   ![Go](https://github.com/kkdai/twitter/workflows/Go/badge.svg) [![goreportcard.com](https://goreportcard.com/badge/github.com/kkdai/twitter)](https://goreportcard.com/report/github.com/kkdai/twitter)\n\nInstallation and Usage\n=============\n\nInstall\n---------------\n\n    go get github.com/kkdai/twitter\n\nUsage\n---------------\n\n#### Use Desktop Mode OAuth in Twitter\n\nUse Desktop oauth mode to sign in Twitter\n\n```go\npackage main\n\nimport (\n    \"github.com/kkdai/twitter\"\n)\n\nconst (\n //Get consumer key and secret from https://dev.twitter.com/apps/new\n ConsumerKey string = \"\"\n ConsumerSecret string = \"\"\n)\nfunc main() {\n twitterClient = NewDesktopClient(ConsumerKey, ConsumerSecret)\n \n //Show a UI to display URL.\n //Please go to this URL to get code to continue\n twitterClient.DoAuth()\n \n //Get timeline only latest one\n timeline, byteData, err :=twitterClient.QueryTimeLine(1)\n \n if err == nil {\n  fmt.Println(\"timeline struct=\", timeline, \" byteData=\", string(byteData) )\n }\n}\n\n```\n\n#### Use Server Mode OAuth in Twitter (Three-legged Authentication)\n\n![](https://g.twimg.com/dev/sites/default/files/images_documentation/sign-in-oauth-1_0.png)\n\nRefer to [twitter document](https://dev.twitter.com/web/sign-in/implementing), use server oauth mode to sign in Twitter\n\nPlease note, you might get error if you don't set `Callback_URL` in twitter setting.  You need input valid URL:\n\n1. Could not be `localhost`, use other setting in `hosts` if you want to test it locally.\n2. Must need input any URL otherwise your app will treat as `desktop app`.\n\n```go\n\npackage main\n\nimport (\n \"flag\"\n \"fmt\"\n \"net/http\"\n \"os\"\n\n . \"github.com/kkdai/twitter\"\n)\n\nvar ConsumerKey string\nvar ConsumerSecret string\nvar twitterClient *ServerClient\n\nfunc init() {\n ConsumerKey = os.Getenv(\"ConsumerKey\")\n ConsumerSecret = os.Getenv(\"ConsumerSecret\")\n}\n\nconst (\n //This URL need note as follow:\n // 1. Could not be localhost, change your hosts to a specific domain name\n // 2. This setting must be identical with your app setting on twitter Dev\n CallbackURL string = \"http://YOURDOMAIN.com/maketoken\"\n)\n\nfunc main() {\n\n if ConsumerKey == \"\" \u0026\u0026 ConsumerSecret == \"\" {\n  fmt.Println(\"Please setup ConsumerKey and ConsumerSecret.\")\n  return\n }\n\n var port *int = flag.Int(\n  \"port\",\n  8888,\n  \"Port to listen on.\")\n\n flag.Parse()\n\n fmt.Println(\"[app] Init server key=\", ConsumerKey, \" secret=\", ConsumerSecret)\n twitterClient = NewServerClient(ConsumerKey, ConsumerSecret)\n http.HandleFunc(\"/maketoken\", GetTwitterToken)\n http.HandleFunc(\"/request\", RedirectUserToTwitter)\n http.HandleFunc(\"/follow\", GetFollower)\n http.HandleFunc(\"/followids\", GetFollowerIDs)\n http.HandleFunc(\"/time\", GetTimeLine)\n http.HandleFunc(\"/user\", GetUserDetail)\n http.HandleFunc(\"/\", MainProcess)\n\n u := fmt.Sprintf(\":%d\", *port)\n fmt.Printf(\"Listening on '%s'\\n\", u)\n http.ListenAndServe(u, nil)\n}\n\nfunc MainProcess(w http.ResponseWriter, r *http.Request) {\n\n if !twitterClient.HasAuth() {\n  fmt.Fprintf(w, \"\u003cBODY\u003e\u003cCENTER\u003e\u003cA HREF='/request'\u003e\u003cIMG SRC='https://g.twimg.com/dev/sites/default/files/images_documentation/sign-in-with-twitter-gray.png'\u003e\u003c/A\u003e\u003c/CENTER\u003e\u003c/BODY\u003e\")\n  return\n } else {\n  //Logon, redirect to display time line\n  timelineURL := fmt.Sprintf(\"http://%s/time\", r.Host)\n  http.Redirect(w, r, timelineURL, http.StatusTemporaryRedirect)\n }\n}\n\nfunc RedirectUserToTwitter(w http.ResponseWriter, r *http.Request) {\n fmt.Println(\"Enter redirect to twitter\")\n fmt.Println(\"Token URL=\", CallbackURL)\n requestUrl := twitterClient.GetAuthURL(CallbackURL)\n\n http.Redirect(w, r, requestUrl, http.StatusTemporaryRedirect)\n fmt.Println(\"Leave redirtect\")\n}\n\nfunc GetTimeLine(w http.ResponseWriter, r *http.Request) {\n timeline, bits, _ := twitterClient.QueryTimeLine(1)\n fmt.Println(\"TimeLine=\", timeline)\n fmt.Fprintf(w, \"The item is: \"+string(bits))\n\n}\nfunc GetTwitterToken(w http.ResponseWriter, r *http.Request) {\n fmt.Println(\"Enter Get twitter token\")\n values := r.URL.Query()\n verificationCode := values.Get(\"oauth_verifier\")\n tokenKey := values.Get(\"oauth_token\")\n\n twitterClient.CompleteAuth(tokenKey, verificationCode)\n timelineURL := fmt.Sprintf(\"http://%s/time\", r.Host)\n\n http.Redirect(w, r, timelineURL, http.StatusTemporaryRedirect)\n}\n\nfunc GetFollower(w http.ResponseWriter, r *http.Request) {\n followers, bits, _ := twitterClient.QueryFollower(10)\n fmt.Println(\"Followers=\", followers)\n fmt.Fprintf(w, \"The item is: \"+string(bits))\n}\n\nfunc GetFollowerIDs(w http.ResponseWriter, r *http.Request) {\n followers, bits, _ := twitterClient.QueryFollowerIDs(10)\n fmt.Println(\"Follower IDs=\", followers)\n fmt.Fprintf(w, \"The item is: \"+string(bits))\n}\nfunc GetUserDetail(w http.ResponseWriter, r *http.Request) {\n followers, bits, _ := twitterClient.QueryFollowerById(2244994945)\n fmt.Println(\"Follower Detail of =\", followers)\n fmt.Fprintf(w, \"The item is: \"+string(bits))\n}\n\n```\n\nInspired By\n=============\n\n- [Twitter API authentication in Go](http://venkat.io/posts/twitter-api-auth-golang/)  \n- [https://github.com/mrjones/oauth](https://github.com/mrjones/oauth)\n- [Twitter:sign-in Doc](https://dev.twitter.com/web/sign-in)\n- [Twitter: Browser sign in flow Overview](https://dev.twitter.com/web/sign-in/desktop-browser)\n\nProject52\n---------------\n\nIt is one of my [project 52](https://github.com/kkdai/project52).\n\nLicense\n---------------\n\nThis package is licensed under MIT license. See [LICENSE](/LICENSE) for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkkdai%2Ftwitter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkkdai%2Ftwitter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkkdai%2Ftwitter/lists"}