go-twitter
go-twitter copied to clipboard
OAuth 2 Request Example
Hi, I was trying to post a tweet.
It seems like I would need to make use of the OAuth 2 authorization flow. Is there a known code example so I could use it?
Cheers.
Hi,
i have the same problem. I receive an:
create tweet error: twitter callout status 403 Unsupported Authentication:Authenticating with OAuth 2.0 Application-Only is forbidden for this endpoint. Supported authentication types are [OAuth 1.0a User Context, OAuth 2.0 User Context].
Has anyone an idea, how it needs to be used?
Thank you!
The POST /2/tweets
endpoint should work with OAuth 1.0a, so I think it would work to pass an httpClient
generated with the oauth1 package as shown below.
Sorry if this is not what the issue is about.
// dummy
type authorizer struct{}
func (a *authorizer) Add(req *http.Request) {}
config := oauth1.NewConfig(consumerToken, consumerSecret)
httpClient := config.Client(oauth1.NoContext, &oauth1.token{
Token: userToken,
TokenSecret: userTokenSecret,
})
client := &twitter.Client{
Authorizer: &authorizer{},
Client: httpClient,
Host: "https://api.twitter.com",
}
The
POST /2/tweets
endpoint should work with OAuth 1.0a, so I think it would work to pass anhttpClient
generated with the oauth1 package as shown below.Sorry if this is not what the issue is about.
// dummy type authorizer struct{} func (a *authorizer) Add(req *http.Request) {} config := oauth1.NewConfig(consumerToken, consumerSecret) httpClient := config.Client(oauth1.NoContext, &oauth1.token{ Token: userToken, TokenSecret: userTokenSecret, }) client := &twitter.Client{ Authorizer: &authorizer{}, Client: httpClient, Host: "https://api.twitter.com", }
Also dont forget about write permission
Go to your Twitter Developer Portal (https://developer.twitter.com/en). Select your project and app. In the settings tab scroll down and hit the edit/set up button of User authentication settings. Change App permissions to Read and write and Direct message. Set to web app (2nd option). Set Callback URI & Website just to https://twitter.com/ since it doesn't matter. Save and regenerate your Keys and Tokens in the Keys and Tokens Tab. (ignore the newly created client ID/secret generated after setting up auth, those are not needed)
Here's the updated code that worked for me. Need to capitalize Token
instead of using token
and make sure you use API_KEY and ACCESS_TOKEN in the right places - I got tripped up because I was using ACCESS_TOKEN for both the NewConfig() call and Client() call which is not right 🤦♂️
Here's the full code to save other people time:
type authorizer struct{}
func (a *authorizer) Add(req *http.Request) {}
twitterAPIKey := os.Getenv("TWITTER_API_KEY")
twitterAPIKeySecret := os.Getenv("TWITTER_API_KEY_SECRET")
twitterAccessToken := os.Getenv("TWITTER_ACCESS_TOKEN")
twitterAccessTokenSecret := os.Getenv("TWITTER_ACCESS_TOKEN_SECRET")
oauth1Config := oauth1.NewConfig(twitterAPIKey, twitterAPIKeySecret)
twitterHttpClient := oauth1Config.Client(oauth1.NoContext, &oauth1.Token{
Token: twitterAccessToken,
TokenSecret: twitterAccessTokenSecret,
})
twitterClient := &twitter.Client{
Authorizer: authorize{},
Client: twitterHttpClient,
Host: "https://api.twitter.com",
}
Hi, I actually tried this and getting this error: twitter callout status 403 Forbidden:Your client app is not configured with the appropriate oauth1 app permissions for this endpoint.