go-mastodon supports multiple authentication methods to access the Mastodon API: application credentials for bot/service access, user credentials for individual user access, and OAuth for user-authorized applications.
Before authenticating, you must register your application with a Mastodon instance.
func RegisterApp(ctx context.Context, appConfig *AppConfig) (*Application, error)Registers a new application with a Mastodon instance and returns application credentials.
Parameters:
ctx: Context for cancellation and timeoutsappConfig: Application configuration including server, name, redirect URIs, and scopesReturns:
*Application: Application credentials including ClientID, ClientSecret, and AuthURIerror: Error if registration failsExample:
app, err := mastodon.RegisterApp(context.Background(), &mastodon.AppConfig{
Server: "https://mastodon.social",
ClientName: "My Mastodon App",
RedirectURIs: "urn:ietf:wg:oauth:2.0:oob",
Scopes: "read write follow",
Website: "https://example.com",
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("Client ID: %s\n", app.ClientID)
fmt.Printf("Client Secret: %s\n", app.ClientSecret)
fmt.Printf("Auth URI: %s\n", app.AuthURI)Use application credentials to access public data without user authorization.
func (c *Client) GetAppAccessToken(ctx context.Context, redirectURI string) errorExchanges API credentials for an application access token. This token provides limited access to public data only.
Parameters:
ctx: Context for cancellation and timeoutsredirectURI: Must match the redirect URI used during application registrationReturns:
error: Error if token exchange failsExample:
config := &mastodon.Config{
Server: "https://mastodon.social",
ClientID: app.ClientID,
ClientSecret: app.ClientSecret,
}
client := mastodon.NewClient(config)
err := client.GetAppAccessToken(context.Background(), "urn:ietf:wg:oauth:2.0:oob")
if err != nil {
log.Fatal(err)
}
// Client now has access token for public data
instance, err := client.GetInstance(context.Background())Use OAuth flow to obtain user authorization and access token.
func (c *Client) GetUserAccessToken(ctx context.Context, authCode string, redirectURI string) errorExchanges a user authorization code for a user access token. The authorization code is obtained by directing the user to the AuthURI provided by RegisterApp.
Parameters:
ctx: Context for cancellation and timeoutsauthCode: Authorization code obtained from user authorizationredirectURI: Must match the redirect URI used during application registrationReturns:
error: Error if token exchange failsExample:
// 1. Register application and get AuthURI
app, err := mastodon.RegisterApp(context.Background(), &mastodon.AppConfig{
Server: "https://mastodon.social",
ClientName: "My Mastodon App",
RedirectURIs: "urn:ietf:wg:oauth:2.0:oob",
Scopes: "read write follow",
})
// 2. Direct user to app.AuthURI to authorize
fmt.Printf("Please visit: %s\n", app.AuthURI)
fmt.Print("Enter authorization code: ")
var authCode string
fmt.Scanln(&authCode)
// 3. Exchange authorization code for access token
config := &mastodon.Config{
Server: "https://mastodon.social",
ClientID: app.ClientID,
ClientSecret: app.ClientSecret,
}
client := mastodon.NewClient(config)
err = client.GetUserAccessToken(context.Background(), authCode, "urn:ietf:wg:oauth:2.0:oob")
if err != nil {
log.Fatal(err)
}
// Client now has full user access
account, err := client.GetAccountCurrentUser(context.Background())If you already have an access token, configure the client directly.
Example:
config := &mastodon.Config{
Server: "https://mastodon.social",
AccessToken: "your-existing-access-token",
}
client := mastodon.NewClient(config)
// Client is ready to use
account, err := client.GetAccountCurrentUser(context.Background())func (c *Client) VerifyAppCredentials(ctx context.Context) (*ApplicationVerification, error)Verifies the application credentials and returns application information.
Parameters:
ctx: Context for cancellation and timeoutsReturns:
*ApplicationVerification: Application verification data including name, website, and VAPID keyerror: Error if verification failsExample:
verification, err := client.VerifyAppCredentials(context.Background())
if err != nil {
log.Fatal(err)
}
fmt.Printf("App Name: %s\n", verification.Name)
fmt.Printf("Website: %s\n", verification.Website)type AppConfig struct {
http.Client
Server string // Mastodon instance URL (e.g., "https://mastodon.social")
ClientName string // Your application name
RedirectURIs string // Where user is redirected after authorization (use "urn:ietf:wg:oauth:2.0:oob" for no redirect)
Scopes string // Space-separated list of scopes (e.g., "read write follow")
Website string // Optional application website
}Configuration for registering applications.
type Application struct {
ID ID // Application ID
RedirectURI string // Redirect URI for OAuth
ClientID string // Client ID for API access
ClientSecret string // Client secret for API access
AuthURI string // URI for user authorization (not part of Mastodon API, generated by go-mastodon)
}Application credentials returned by registration.
type ApplicationVerification struct {
Name string // Application name
Website string // Application website
VapidKey string // VAPID key for push notifications
}Application verification information.
type Config struct {
Server string // Mastodon instance URL
ClientID string // Application client ID
ClientSecret string // Application client secret
AccessToken string // User or application access token
}Client configuration for API access.
type Client struct {
http.Client
Config *Config // Client configuration
UserAgent string // User agent string for HTTP requests
JSONWriter io.Writer // Writer for JSON response logging (optional)
}The main API client for interacting with Mastodon. All API methods are called on Client instances.
func NewClient(config *Config) *ClientCreates a new Mastodon API client with the provided configuration.
Parameters:
config: Client configuration including server URL and credentialsReturns:
*Client: New client instance ready for API callsExample:
config := &mastodon.Config{
Server: "https://mastodon.social",
ClientID: "your-client-id",
ClientSecret: "your-client-secret",
AccessToken: "your-access-token",
}
client := mastodon.NewClient(config)The following authentication methods are deprecated and should not be used:
Authenticate(ctx, username, password) - Use GetAppAccessToken() or GetUserAccessToken() insteadAuthenticateApp(ctx) - Use GetAppAccessToken() insteadAuthenticateToken(ctx, authCode, redirectURI) - Use GetUserAccessToken() instead