gotwi

package module
v0.9.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2021 License: MIT Imports: 17 Imported by: 20

README

gotwi

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

Supported APIs

What's New with Twitter API v2 | Docs | Twitter Developer Platform

Progress of supporting APIs:

  • Tweets
    • Tweet lookup
      • GET /2/tweets
      • GET /2/tweets/:id
    • Manage Tweet
      • POST /2/tweets
      • DELETE /2/tweets/:id
    • Search Tweets
      • GET /2/tweets/search/recent
      • GET /2/tweets/search/all
    • Tweet counts
      • GET /2/tweets/counts/recent
      • GET /2/tweets/counts/all
    • Timelines
      • GET /2/users/:id/tweets
      • GET /2/users/:id/mentions
    • Filtered stream
      • POST /2/tweets/search/stream/rules
      • GET /2/tweets/search/stream/rules
      • GET /2/tweets/search/stream
    • Sampled stream
      • 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
  • Users
    • User lookup
      • GET /2/users
      • GET /2/users/:id
      • GET /2/users/by
      • GET /2/users/by/username
    • 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
    • Manage pinned Lists
      • GET /2/users/:id/pinned_lists
      • POST /2/users/:id/pinned_lists
      • DELETE /2/users/:id/pinned_lists/:list_id
  • Spaces
    • Lookup Spaces
      • GET /2/spaces/:id
      • GET /2/spaces
      • GET /2/spaces/by/creator_ids
    • 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

  • Get a user by user name.
package main

import (
	"context"
	"fmt"

	"github.com/michimani/gotwi"
	"github.com/michimani/gotwi/fields"
	"github.com/michimani/gotwi/users"
	"github.com/michimani/gotwi/users/types"
)

func main() {
	in := &gotwi.NewGotwiClientInput{
		AuthenticationMethod: gotwi.AuthenMethodOAuth2BearerToken,
	}

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

	p := &types.UserLookupByUsernameParams{
		Username: "michimani210",
		Expansions: fields.ExpansionList{
			fields.ExpansionPinnedTweetID,
		},
		UserFields: fields.UserFieldList{
			fields.UserFieldCreatedAt,
		},
		TweetFields: fields.TweetFieldList{
			fields.TweetFieldCreatedAt,
		},
	}
	res, err := users.UserLookupByUsername(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 following output.

ID:  581780917
Name:  michimani Lv.859
Username:  michimani210
CreatedAt:  2012-05-16 12:07:04 +0000 UTC
PinnedTweet:  pinned tweet

Request with OAuth 1.0a User Context

  • Get blocking users.
package main

import (
	"context"
	"fmt"

	"github.com/michimani/gotwi"
	"github.com/michimani/gotwi/fields"
	"github.com/michimani/gotwi/users"
	"github.com/michimani/gotwi/users/types"
)

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

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

	p := &types.BlocksBlockingGetParams{
		ID:        "your-twitter-acount-id",
		MaxResults: 5,
	}

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

	for _, b := range res.Data {
		fmt.Println(gotwi.StringValue(b.Name))
	}
}
go run main.go

You will get the output like following.

blockingUser1
blockingUser2
blockingUser3
blockingUser4
blockingUser5

Licence

MIT

Author

michimani210

Documentation

Index

Constants

View Source
const (
	APIKeyEnvName       = "GOTWI_API_KEY"
	APIKeySecretEnvName = "GOTWI_API_KEY_SECRET"
)
View Source
const (
	AuthenMethodOAuth1UserContext = "OAuth 1.0a User context"
	AuthenMethodOAuth2BearerToken = "OAuth 2.0 Bearer token"
)
View Source
const (
	OAuthVersion10               = "1.0"
	OAuthSignatureMethodHMACSHA1 = "HMAC-SHA1"
)
View Source
const OAuth2TokenEndpoint = "https://api.twitter.com/oauth2/token"

Variables

This section is empty.

Functions

func Bool added in v0.6.0

func Bool(b bool) *bool

func BoolValue added in v0.6.0

func BoolValue(b *bool) bool

func Float64 added in v0.9.0

func Float64(f float64) *float64

func Float64Value added in v0.9.0

func Float64Value(f *float64) float64

func GenerateBearerToken

func GenerateBearerToken(c *GotwiClient, apiKey, apiKeySecret string) (string, error)

func Int added in v0.6.0

func Int(i int) *int

func IntValue added in v0.6.0

func IntValue(i *int) int

func String added in v0.6.0

func String(s string) *string

func StringValue added in v0.6.0

func StringValue(s *string) string

func Time added in v0.9.0

func Time(t time.Time) *time.Time

func TimeValue added in v0.9.0

func TimeValue(t *time.Time) time.Time

Types

type AuthenticationMethod added in v0.5.0

type AuthenticationMethod string

func (AuthenticationMethod) Valid added in v0.5.0

func (a AuthenticationMethod) Valid() bool

type ClientResponse

type ClientResponse struct {
	StatusCode int
	Status     string
	Error      *resources.Non2XXError
	Body       []byte
	Response   util.Response
}

type CreateOAthSignatureInput added in v0.5.0

type CreateOAthSignatureInput struct {
	HTTPMethod       string
	RawEndpoint      string
	OAuthConsumerKey string
	OAuthToken       string
	SigningKey       string
	ParameterMap     map[string]string
}

type CreateOAthSignatureOutput added in v0.5.0

type CreateOAthSignatureOutput struct {
	OAuthNonce           string
	OAuthSignatureMethod string
	OAuthTimestamp       string
	OAuthVersion         string
	OAuthSignature       string
}

func CreateOAuthSignature added in v0.5.1

func CreateOAuthSignature(in *CreateOAthSignatureInput) (*CreateOAthSignatureOutput, error)

type Endpoint added in v0.5.0

type Endpoint string

func (Endpoint) Detail added in v0.5.0

func (e Endpoint) Detail() (*EndpointInfo, error)

func (Endpoint) String added in v0.5.0

func (e Endpoint) String() string

type EndpointInfo added in v0.5.0

type EndpointInfo struct {
	Raw                      string
	Base                     string
	EncodedQueryParameterMap map[string]string
}

type GotwiClient added in v0.5.1

type GotwiClient struct {
	Client               *http.Client
	AuthenticationMethod AuthenticationMethod
	AccessToken          string
	OAuthToken           string
	SigningKey           string
	OAuthConsumerKey     string
}

func NewGotwiClient added in v0.5.1

func NewGotwiClient(in *NewGotwiClientInput) (*GotwiClient, error)

func (*GotwiClient) CallAPI added in v0.5.1

func (c *GotwiClient) CallAPI(ctx context.Context, endpoint, method string, p util.Parameters, i util.Response) error

func (*GotwiClient) Exec added in v0.5.1

func (*GotwiClient) IsReady added in v0.5.1

func (c *GotwiClient) IsReady() bool

type NewGotwiClientInput added in v0.5.1

type NewGotwiClientInput struct {
	HTTPClient           *http.Client
	AuthenticationMethod AuthenticationMethod
	OAuthToken           string
	OAuthTokenSecret     string
}

type OAuth2TokenResponse

type OAuth2TokenResponse struct {
	TokenType   string `json:"token_type"`
	AccessToken string `json:"access_token"`
}

func (OAuth2TokenResponse) HasPartialError added in v0.8.0

func (o OAuth2TokenResponse) HasPartialError() bool

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL