vartanbeno / go-reddit

Go library for accessing the Reddit API.
Other
306 stars 84 forks source link

Add support for Application Only OAuth #21

Open and3rson opened 3 years ago

and3rson commented 3 years ago

This PR implements Application Only OAuth (also known as "two-legged oauth").

This is NOT a breaking change and is fully backwards-compatible. Default behavior is unaffected.

This flow is activated with WithApplicationOnlyOAuth(o bool) option:

See #18 for discussion: https://github.com/vartanbeno/go-reddit/issues/18#issuecomment-890094080

Usage example:

package main

import (
    "context"
    "fmt"

    "github.com/and3rson/go-reddit/v2/reddit"
)

func main() {
    client, err := reddit.NewClient(reddit.Credentials{
        ID:     "your_client_id",
        Secret: "your_client_secret",
    }, reddit.WithUserAgent("my-application"), reddit.WithApplicationOnlyOAuth(true))
    if err != nil {
        panic(err)
    }
    posts, err := client.Subreddit.TopPosts(context.Background(), "funny", &reddit.ListPostOptions{})
    if err != nil {
        panic(err)
    }
    fmt.Println(posts)
}

Unit test included.