As the title says, when I try to post a tweet with Go (I have the free X/Twitter Developer account), I get this error message:
2023/12/06 21:56:24 twitter: 453 You currently have access to a subset of Twitter API v2 endpoints and limited v1.1 endpoints (e.g. media post, oauth) only. If you need access to this endpoint, you may need a different access level. You can learn more here:
https://developer.twitter.com/en/portal/product
2023/12/06/21:56:24 &{Status:403 Forbidden StatusCode:403 ... [truncated for brevity]}
This is my current code:
package main
import (
// other imports
"fmt"
"github.com/joho/godotenv"
"log"
"os"
"github.com/dghubble/go-twitter/twitter"
"github.com/dghubble/oauth1"
)
type Credentials struct {
ConsumerKey string
ConsumerSecret string
AccessToken string
AccessTokenSecret string
}
func getClient(creds *Credentials) (*twitter.Client, error) {
config := oauth1.NewConfig(creds.ConsumerKey, creds.ConsumerSecret)
token := oauth1.NewToken(creds.AccessToken, creds.AccessTokenSecret)
httpClient := config.Client(oauth1.NoContext, token)
client := twitter.NewClient(httpClient)
verifyParams := &twitter.AccountVerifyParams{
SkipStatus: twitter.Bool(true),
IncludeEmail: twitter.Bool(true),
}
user, _, err := client.Accounts.VerifyCredentials(verifyParams)
if err != nil {
return nil, err
}
log.Printf("User's ACCOUNT:\n%+v\n", user)
return client, nil
}
func init() {
if err := godotenv.Load(); err != nil {
log.Fatal("Error loading .env file")
}
}
func main() {
creds := Credentials{
AccessToken: os.Getenv("ACCESS_TOKEN"),
AccessTokenSecret: os.Getenv("ACCESS_TOKEN_SECRET"),
ConsumerKey: os.Getenv("CONSUMER_KEY"),
ConsumerSecret: os.Getenv("CONSUMER_SECRET"),
}
{
// some code here...
client, err := getClient(&creds)
if err != nil {
log.Println("Error getting Twitter Client, this is expected if you did not supply your Twitter API tokens")
log.Println(err)
}
message := "Hello World"
tweet, resp, err := client.Statuses.Update(message, nil)
if err != nil {
log.Println(err)
}
log.Printf("%+v\n", resp)
log.Printf("%+v\n", tweet)
}
}
I've visited some online resources, but it seems focused on Python examples, and I'm looking for assistance specific to Go. Any insights or suggestions on resolving this issue would be greatly appreciated.
The library you're using, go-twitter
, uses the v1.1 twitter API. This API can not be accessed with the free plan, which is exactly what the error message you're getting says.
... You currently have access to a subset of Twitter API v2 endpoints and limited v1.1 endpoints (e.g. media post, oauth) only. ...
For libraries that work with v2, check out the documentation: https://developer.twitter.com/en/docs/twitter-api/tools-and-libraries/v2#go