Skip to content
Golang Reddit API Wrapper
Branch: master
Clone or download
cameronstanley Merge pull request #3 from rmichela/feature/user-agent
Set User-Agent when calling Exchange()
Latest commit 4bfac7e Apr 23, 2017
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
test_data Implement GetMyPreferences Oct 31, 2016
.gitignore Initial commit Oct 12, 2016
.travis.yml Add .travis.yml config file Oct 14, 2016
LICENSE Initial commit Oct 12, 2016
README.md Documentation Apr 15, 2017
account.go Add User-Agent to client; fixes for GetAccount to send User-Agent Oct 29, 2016
account_test.go WIP Implement message Oct 27, 2016
authenticator.go Documentation Apr 15, 2017
authenticator_test.go
award.go
award_test.go Implement GetMyPreferences Oct 31, 2016
comment.go Documentation fixes Nov 4, 2016
comment_test.go Implement HideLink; run gofmt Nov 4, 2016
link.go Implement HideLink; run gofmt Nov 4, 2016
link_test.go
media.go Fix golint issues; refactor article to link Oct 26, 2016
message.go Implement HideLink; run gofmt Nov 4, 2016
message_test.go
preferences.go Implement DeleteComment Nov 2, 2016
preferences_test.go
reddit.go
reddit_test.go Refactor tests to use testify Oct 18, 2016
subreddit.go Implement CommentOnLink Nov 3, 2016
subreddit_test.go Fix golint issues; refactor article to link Oct 26, 2016
user.go Implement CommentOnLink Nov 3, 2016
user_test.go Fix golint issues; refactor article to link Oct 26, 2016

README.md

go-reddit

Build Status GoDoc Go Report Card

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() {
  // Create a new authenticator with your app's client ID, secret, and redirect URI
  // A random string representing state and a list of requested OAuth scopes are required
  authenticator := reddit.NewAuthenticator("<client_id>", "<client_secret>", "<redirect_uri>", 
     "<platform>:<app ID>:<version string> (by /u/<reddit username>)", "<random_string>", reddit.ScopeIdentity)
  
  // Instruct your user to visit the URL retrieved from GetAuthenticationURL in their web browser
  url := authenticator.GetAuthenticationURL()
  fmt.Printf("Please proceed to %s\n", url)

  // After the user grants permission for your client, they will be redirected to the supplied redirect_uri with a code and state as URL parameters
  // Gather these values from the user on the console
  // Note: this can be automated by having a web server listen on the redirect_uri and parsing the state and code params
  fmt.Print("Enter state: ")
  var state string
  fmt.Scanln(&state)

  fmt.Print("Enter code: ")
  var code string
  fmt.Scanln(&code)

  // Exchange the code for an access token
  token, err := authenticator.GetToken(state, code)
  
  // Create a new client using the access token and a user agent string to identify your application
  client := authenticator.GetAuthClient(token)
}

Examples

// Returns a new unauthenticated client for invoking the API
client := reddit.NoAuthClient

// Retrives a listing of default subreddits
client.GetDefaultSubreddits()

// Retrives a listing of hot links for the "news" subreddit
client.GetHotLinks("news")
You can’t perform that action at this time.