go-reddit
A Golang wrapper for the Reddit API. This package aims to implement every endpoint exposed according to the documentation in a user friendly, well tested and documented manner.
Installation
Install the package with
go get github.com/cameronstanley/go-reddit
Authentication
Many endpoints in the Reddit API require OAuth2 authentication to access. To get started, register an app at https://www.reddit.com/prefs/apps and be sure to note the ID, secret, and redirect URI. These values will be used to construct the Authenticator to generate a client with OAuth access. The following is an example of creating an authenticated client using a manual approach:
package main
import(
"fmt"
"github.com/cameronstanley/go-reddit"
)
func main() {
authenticator := reddit.NewAuthenticator("<client_id>", "<client_secret>", "<redirect_uri>", "<random_string>", reddit.ScopeIdentity)
url := authenticator.GetAuthenticationURL()
fmt.Printf("Please proceed to %s\n", url)
fmt.Print("Enter state: ")
var state string
fmt.Scanln(&state)
fmt.Print("Enter code: ")
var code string
fmt.Scanln(&code)
token, err := authenticator.GetToken(state, code)
client := authenticator.GetAuthClient(token, "<platform>:<app ID>:<version string> (by /u/<reddit username>)")
}
Examples
client := reddit.NoAuthClient
client.GetDefaultSubreddits()
client.GetHotLinks("news")