go-reddit icon indicating copy to clipboard operation
go-reddit copied to clipboard

Add support for Application Only OAuth

Open and3rson opened this issue 3 years ago • 0 comments

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:

  • o == true: oauthTransport uses "golang.org/x/oauth2/clientcredentials"
  • o == false: oauthTransport uses "golang.org/x/oauth2" (default behavior)

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.

and3rson avatar Jul 30 '21 20:07 and3rson