diff --git a/README.md b/README.md index d9cb718..d868fdc 100644 --- a/README.md +++ b/README.md @@ -91,16 +91,16 @@ Progress of supporting APIs: 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 +```bash +export GOTWI_API_KEY='your-api-key' +export GOTWI_API_KEY_SECRET='your-api-key-secret' ``` -## Request with OAuth 2.0 Bearer Token +## Request with OAuth 1.0a User Context -This authentication method allows only read-only access to public information. +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: Get a user by user name. +### Example: Get your own information. ```go package main @@ -116,16 +116,19 @@ import ( ) func main() { - c, err := gotwi.NewClient(&gotwi.NewClientInput{ - AuthenticationMethod: gotwi.AuthenMethodOAuth2BearerToken, - }) + in := &gotwi.NewClientInput{ + AuthenticationMethod: gotwi.AuthenMethodOAuth1UserContext, + OAuthToken: "your-access-token", + OAuthTokenSecret: "your-access-token-secret", + } + + c, err := gotwi.NewClient(in) if err != nil { fmt.Println(err) return } - p := &types.GetByUsernameInput{ - Username: "michimani210", + p := &types.GetMeInput{ Expansions: fields.ExpansionList{ fields.ExpansionPinnedTweetID, }, @@ -137,7 +140,7 @@ func main() { }, } - u, err := userlookup.GetByUsername(context.Background(), c, p) + u, err := userlookup.GetMe(context.Background(), c, p) if err != nil { fmt.Println(err) return @@ -163,31 +166,12 @@ You will get the output like following. ``` ID: 581780917 -Name: michimani Lv.861 +Name: michimani Lv.873 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. - -```go -in := &gotwi.NewClientWithAccessTokenInput{ - AccessToken: "your-access-token", -} - -c, err := gotwi.NewClientWithAccessToken(in) -if err != nil { - // error handling -} +PinnedTweet: OpenAI API の Function Calling を使って自然言語で AWS リソースを作成してみる ``` -## 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. ```go @@ -205,8 +189,8 @@ import ( func main() { in := &gotwi.NewClientInput{ AuthenticationMethod: gotwi.AuthenMethodOAuth1UserContext, - OAuthToken: "your-twitter-acount-oauth-token", - OAuthTokenSecret: "your-twitter-acount-oauth-token-secret", + OAuthToken: "your-access-token", + OAuthTokenSecret: "your-access-token-secret", } c, err := gotwi.NewClient(in) @@ -248,6 +232,100 @@ You will get the output like following. [1462813519607263236] This is a test tweet with poll. ``` +## 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. + +⚠ This example only works with Twitter API v2 Basic or Pro plan. see details: [Developers Portal](https://developer.twitter.com/en/portal/products) + +```go +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: 真偽をハッキリしたい西城秀樹「ブーリアン、ブーリアン」 +``` + +## Request with OAuth 2.0 Authorization Code with PKCE + +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. + +```go +in := &gotwi.NewClientWithAccessTokenInput{ + AccessToken: "your-access-token", +} + +c, err := gotwi.NewClientWithAccessToken(in) +if err != nil { + // error handling +} +``` + +See below for information on which authentication methods are available for which endpoints. + +[Twitter API v2 authentication mapping | Docs | Twitter Developer Platform ](https://developer.twitter.com/en/docs/authentication/guides/v2-authentication-mapping) + ## Error handling Each function that calls the Twitter API (e.g. `retweet.ListUsers()`) may return an error for some reason.