All Projects → michimani → gotwi

michimani / gotwi

Licence: MIT license
A library for using the Twitter API v2 in the Go language. (It is still under development).

Programming Languages

go
31211 projects - #10 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to gotwi

twspace-crawler
Script to monitor & download Twitter Spaces 24/7
Stars: ✭ 78 (+143.75%)
Mutual labels:  twitter-api
Tweet-2-RSS
Convert your Twitter API requests to RSS Feeds
Stars: ✭ 14 (-56.25%)
Mutual labels:  twitter-api
2017-year-in-review
Year in Review with R Rmd Template
Stars: ✭ 34 (+6.25%)
Mutual labels:  twitter-api
spotatweet
A Spotify & Twitter API mashup showing what people are listening to now. Written in Node.js.
Stars: ✭ 26 (-18.75%)
Mutual labels:  twitter-api
Twitter-Stream-API-Dataset
Twitter Dynamic Dataset Api. Create any dataset YOU want.
Stars: ✭ 20 (-37.5%)
Mutual labels:  twitter-api
twitter-ruby-ads-sdk
A Twitter supported and maintained Ads API SDK for Ruby.
Stars: ✭ 63 (+96.88%)
Mutual labels:  twitter-api
github-readme-twitter
Add Twitter to your github readme
Stars: ✭ 73 (+128.13%)
Mutual labels:  twitter-api
twauth-web
A simple Python Flask web app that demonstrates the flow of obtaining a Twitter user OAuth access token
Stars: ✭ 65 (+103.13%)
Mutual labels:  twitter-api
SocialAuth
No description or website provided.
Stars: ✭ 35 (+9.38%)
Mutual labels:  twitter-api
Tweetstorm
🐦 Twitter UserStream APIの簡単な代替実装 / A simple substitute implementation for the Twitter UserStream
Stars: ✭ 55 (+71.88%)
Mutual labels:  twitter-api
twitter-crypto-bot
This is a Twitter bot that tweets about cryptocurrencies prices every certain amount of minutes
Stars: ✭ 21 (-34.37%)
Mutual labels:  twitter-api
go-t
A blazing-fast, simple and easy to use command-line client for Twitter written in Go. 🚀📨
Stars: ✭ 70 (+118.75%)
Mutual labels:  twitter-api
search-tweets-ruby
Ruby client for the Twitter search endpoints (v2/Labs/premium/enterprise). Now supports Twitter API v2 /recent and /all search endpoints.
Stars: ✭ 45 (+40.63%)
Mutual labels:  twitter-api
TwitterClone
TwitterClient app represents a real world twitter client app: it provides a fairly complex set of functionalities, it's a suitable showcase for all the advantages that architecture components bring, has all features that would make it a modular, scalable, testable and maintainable app.
Stars: ✭ 26 (-18.75%)
Mutual labels:  twitter-api
twitter-digest
✉️ A netlify lambda function that emails you tweets from a twitter list.
Stars: ✭ 14 (-56.25%)
Mutual labels:  twitter-api
ml-flashcards
Extracting data from Twitter for #machinelearningflashcards 🤖
Stars: ✭ 35 (+9.38%)
Mutual labels:  twitter-api
twitivity
🐍 Twitter Accounts Activity API Client Library for Python
Stars: ✭ 49 (+53.13%)
Mutual labels:  twitter-api
bitparrot
An app I created so developers can track the info they need: now includes Twitter API feeds and Machine Learning, Python, and Ruby on Rails sections.
Stars: ✭ 22 (-31.25%)
Mutual labels:  twitter-api
twittered
Twitter API client for Java developers
Stars: ✭ 170 (+431.25%)
Mutual labels:  twitter-api
larry
Larry 🐦 is a really simple Twitter bot generator that tweets random repositories from Github built in Go
Stars: ✭ 64 (+100%)
Mutual labels:  twitter-api

gotwi

Go Reference Twitter API v2 codecov

This is a library for using the Twitter API v2 in the Go language. (It is still under development).

Supported APIs

Twitter API Documentation | Docs | Twitter Developer Platform

Progress of supporting APIs:

Category Sub Category Endpoint Supported
Tweets Tweet lookup GET /2/tweets
GET /2/tweets/:id
Manage Tweet POST /2/tweets
DELETE /2/tweets/:id
Timelines GET /2/users/:id/tweets
GET /2/users/:id/mentions
Search Tweets GET /2/tweets/search/recent
GET /2/tweets/search/all
Tweet counts GET /2/tweets/counts/recent
GET /2/tweets/counts/all
Filtered stream POST /2/tweets/search/stream/rules
GET /2/tweets/search/stream/rules
GET /2/tweets/search/stream
Volume streams GET /2/tweets/sample/stream
Retweets GET /2/users/:id/retweeted_by
POST /2/users/:id/retweets
DELETE /2/users/:id/retweets/:source_tweet_id
Likes GET /2/tweets/:id/liking_users
GET /2/tweets/:id/liked_tweets
POST /2/users/:id/likes
DELETE /2/users/:id/likes/:tweet_id
Hide replies PUT /2/tweets/:id/hidden
Quote Tweets GET /2/tweets/:id/quote_tweets
Bookmarks GET /2/users/:id/bookmarks
POST /2/users/:id/bookmarks
DELETE /2/users/:id/bookmarks/:tweet_id
Users User lookup GET /2/users
GET /2/users/:id
GET /2/users/by
GET /2/users/by/username
GET /2/users/by/me
Follows GET /2/users/:id/following
GET /2/users/:id/followers
POST /2/users/:id/following
DELETE /2/users/:source_user_id/following/:target_user_id
Blocks GET /2/users/:id/blocking
POST /2/users/:id/blocking
DELETE /2/users/:source_user_id/blocking/:target_user_id
Mutes GET /2/users/:id/muting
POST /2/users/:id/muting
DELETE /2/users/:source_user_id/muting/:target_user_id
Lists List lookup GET /2/lists/:id
GET /2/users/:id/owned_lists
Manage Lists POST /2/lists
DELETE /2/lists/:id
PUT /2/lists/:id
List Tweets lookup GET /2/lists/:id/tweets
List members GET /2/users/:id/list_memberships
GET /2/lists/:id/members
POST /2/lists/:id/members
DELETE /2/lists/:id/members/:user_id
List follows GET /2/lists/:id/followers
GET /2/users/:id/followed_lists
POST /2/users/:id/followed_lists
DELETE /2/users/:id/followed_lists/:list_id
Pinned Lists GET /2/users/:id/pinned_lists
POST /2/users/:id/pinned_lists
DELETE /2/users/:id/pinned_lists/:list_id
Spaces Spaces Lookup GET /2/spaces/:id
GET /2/spaces
GET /2/spaces/by/creator_ids
GET /2/spaces/:id/buyers
GET /2/spaces/:id/tweets
Search Spaces GET /2/spaces/search
Compliance Batch compliance GET /2/compliance/jobs/:id
GET /2/compliance/jobs
POST /2/compliance/jobs

Sample

Prepare

Set the API key and API key secret to environment variables.

export GOTWI_API_KEY=your-api-key
export GOTWI_API_KEY_SECRET=your-api-key-secret

Request with OAuth 2.0 Bearer Token

This authentication method allows only read-only access to public information.

Example: Get a user by user name.

package main

import (
	"context"
	"fmt"

	"github.com/michimani/gotwi"
	"github.com/michimani/gotwi/fields"
	"github.com/michimani/gotwi/user/userlookup"
	"github.com/michimani/gotwi/user/userlookup/types"
)

func main() {
	c, err := gotwi.NewClient(&gotwi.NewClientInput{
		AuthenticationMethod: gotwi.AuthenMethodOAuth2BearerToken,
	})
	if err != nil {
		fmt.Println(err)
		return
	}

	p := &types.GetByUsernameInput{
		Username: "michimani210",
		Expansions: fields.ExpansionList{
			fields.ExpansionPinnedTweetID,
		},
		UserFields: fields.UserFieldList{
			fields.UserFieldCreatedAt,
		},
		TweetFields: fields.TweetFieldList{
			fields.TweetFieldCreatedAt,
		},
	}

	u, err := userlookup.GetByUsername(context.Background(), c, p)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("ID:          ", gotwi.StringValue(u.Data.ID))
	fmt.Println("Name:        ", gotwi.StringValue(u.Data.Name))
	fmt.Println("Username:    ", gotwi.StringValue(u.Data.Username))
	fmt.Println("CreatedAt:   ", u.Data.CreatedAt)
	if u.Includes.Tweets != nil {
		for _, t := range u.Includes.Tweets {
			fmt.Println("PinnedTweet: ", gotwi.StringValue(t.Text))
		}
	}
}
go run main.go

You will get the output like following.

ID:           581780917
Name:         michimani Lv.861
Username:     michimani210
CreatedAt:    2012-05-16 12:07:04 +0000 UTC
PinnedTweet:  真偽をハッキリしたい西城秀樹「ブーリアン、ブーリアン」

new client with access token

If you already have a pre-generated access token (e.g. OAuth 2.0 Authorization Code with PKCE), you can use NewClientWithAccessToken() function to generate a Gotwi client.

in := &gotwi.NewClientWithAccessTokenInput{
	AccessToken: "your-access-token",
}

c, err := gotwi.NewClientWithAccessToken(in)
if err != nil {
	// error handling
}

Request with OAuth 1.0a User Context

With this authentication method, each operation will be performed as the authenticated Twitter account. For example, you can tweet as that account, or retrieve accounts that are blocked by that account.

Example: Tweet with poll.

package main

import (
	"context"
	"fmt"

	"github.com/michimani/gotwi"
	"github.com/michimani/gotwi/tweet/managetweet"
	"github.com/michimani/gotwi/tweet/managetweet/types"
)

func main() {
	in := &gotwi.NewClientInput{
		AuthenticationMethod: gotwi.AuthenMethodOAuth1UserContext,
		OAuthToken:           "your-twitter-acount-oauth-token",
		OAuthTokenSecret:     "your-twitter-acount-oauth-token-secret",
	}

	c, err := gotwi.NewClient(in)
	if err != nil {
		fmt.Println(err)
		return
	}

	p := &types.CreateInput{
		Text: gotwi.String("This is a test tweet with poll."),
		Poll: &types.CreateInputPoll{
			DurationMinutes: gotwi.Int(5),
			Options: []string{
				"Cyan",
				"Magenta",
				"Yellow",
				"Key plate",
			},
		},
	}

	res, err := managetweet.Create(context.Background(), c, p)
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	fmt.Printf("[%s] %s\n", gotwi.StringValue(res.Data.ID), gotwi.StringValue(res.Data.Text))
}
go run main.go

You will get the output like following.

[1462813519607263236] This is a test tweet with poll.

Error handling

Each function that calls the Twitter API (e.g. retweet.ListUsers()) may return an error for some reason. If the error is caused by the Twitter API returning a status other than 2XX, you can check the details by doing the following.

res, err := retweet.ListUsers(context.Background(), c, p)
if err != nil {
	fmt.Println(err)

	// more error information
	ge := err.(*gotwi.GotwiError)
	if ge.OnAPI {
		fmt.Println(ge.Title)
		fmt.Println(ge.Detail)
		fmt.Println(ge.Type)
		fmt.Println(ge.Status)
		fmt.Println(ge.StatusCode)

		for _, ae := range ge.APIErrors {
			fmt.Println(ae.Message)
			fmt.Println(ae.Label)
			fmt.Println(ae.Parameters)
			fmt.Println(ae.Code)
			fmt.Println(ae.Code.Detail())
		}

		if ge.RateLimitInfo != nil {
			fmt.Println(ge.RateLimitInfo.Limit)
			fmt.Println(ge.RateLimitInfo.Remaining)
			fmt.Println(ge.RateLimitInfo.ResetAt)
		}
	}
}

More examples

See _examples directory.

Licence

MIT

Author

michimani210

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].