Patreon Go API client

Build Status GoDoc Go Report Card codecov MIT license Patreon

patreon-go

patreon-go is a Go client library for accessing the Patreon API.

How to import

The patreon-go package may be installed by running:

go get gopkg.in/mxpv/patreon-go.v1

or

import "gopkg.in/mxpv/patreon-go.v1"

Basic example

import "gopkg.in/mxpv/patreon-go.v1"

func main() {
	client := patreon.NewClient(nil)
  
	user, err := client.FetchUser()
	if err != nil {
		// ...
	}

	print(user.Data.Id)
}

Authentication

The patreon-go library does not directly handle authentication. Instead, when creating a new client, pass an http.Client that can handle authentication for you, most likely you will need oauth2 package.

Here is an example with static token:

import (
	"gopkg.in/mxpv/patreon-go.v1"
	"golang.org/x/oauth2"
)

func NewPatreonClient(ctx context.Context, token string) *patreon.Client {
	ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
	tc := oauth2.NewClient(ctx, ts)
	
	client := patreon.NewClient(tc)
	return client
}

Automatically refresh token:

func NewPatreonClient() (*patreon.Client, error) {
	config := oauth2.Config{
		ClientID:     "<client_id>",
		ClientSecret: "<client_secret>",
		Endpoint: oauth2.Endpoint{
			AuthURL:  AuthorizationURL,
			TokenURL: AccessTokenURL,
		},
		Scopes: []string{"users", "pledges-to-me", "my-campaign"},
	}

	token := oauth2.Token{
		AccessToken:  "<current_access_token>",
		RefreshToken: "<current_refresh_token>",
		// Must be non-nil, otherwise token will not be expired
		Expiry: time.Now().Add(-24 * time.Hour),
	}

	tc := config.Client(context.Background(), &token)

	client := NewClient(tc)
	_, err := client.FetchUser()
	if err != nil {
		panic(err)
	}

	print("OK")
}

Look & Feel

func Example_fetchPatronsAndPledges() {
	ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: testAccessToken})
	tc := oauth2.NewClient(oauth2.NoContext, ts)

	// Create client with static access token
	client := NewClient(tc)

	// Get your campaign data
	campaignResponse, err := client.FetchCampaign()
	if err != nil {
		panic(err)
	}

	campaignId := campaignResponse.Data[0].Id

	cursor := ""
	page := 1

	for {
		pledgesResponse, err := client.FetchPledges(campaignId,
			WithPageSize(25),
			WithCursor(cursor))

		if err != nil {
			panic(err)
		}

		// Get all the users in an easy-to-lookup way
		users := make(map[string]*User)
		for _, item := range pledgesResponse.Included.Items {
			u, ok := item.(*User)
			if !ok {
				continue
			}

			users[u.Id] = u
		}

		fmt.Printf("Page %d\r\n", page)

		// Loop over the pledges to get e.g. their amount and user name
		for _, pledge := range pledgesResponse.Data {
			amount := pledge.Attributes.AmountCents
			patronId := pledge.Relationships.Patron.Data.Id
			patronFullName := users[patronId].Attributes.FullName

			fmt.Printf("%s is pledging %d cents\r\n", patronFullName, amount)
		}

		// Get the link to the next page of pledges
		nextLink := pledgesResponse.Links.Next
		if nextLink == "" {
			break
		}

		cursor = nextLink
		page++
	}

	fmt.Print("Done!")
}
Owner
Maksym Pavlenko
containers, microVMs, Kubernetes. @containerd maintainer.
Maksym Pavlenko
Similar Resources

Balabola-go-client - GO client for Yandex balabola service

Balabola GO Client GO client for Yandex balabola service Yandex warning The neur

Jan 29, 2022

Client-server-golang-sqs - Client Server with SQS and golang

Client Server with SQS and golang Multi-threaded client-server demo with Go What

Feb 14, 2022

Go Client Library for Amazon Product Advertising API

go-amazon-product-advertising-api Go Client Library for Amazon Product Advertising API How to Use go get -u github.com/ngs/go-amazon-product-advertisi

Sep 27, 2022

A Go client library for the Twitter 1.1 API

Anaconda Anaconda is a simple, transparent Go package for accessing version 1.1 of the Twitter API. Successful API queries return native Go structs th

Jan 1, 2023

Go client library for interacting with Coinpaprika's API

Coinpaprika API Go Client Usage This library provides convenient way to use coinpaprika.com API in Go. Coinpaprika delivers full market data to the wo

Dec 8, 2022

Golang client for ethereum json rpc api

Ethrpc Golang client for ethereum JSON RPC API. web3_clientVersion web3_sha3 net_version net_peerCount net_listening eth_protocolVersion eth_syncing e

Jan 7, 2023

Package githubv4 is a client library for accessing GitHub GraphQL API v4 (https://developer.github.com/v4/).

githubv4 Package githubv4 is a client library for accessing GitHub GraphQL API v4 (https://docs.github.com/en/graphql). If you're looking for a client

Dec 26, 2022

📟 Tiny utility Go client for HackerNews API.

go-hacknews Tiny utility Go client for HackerNews API. Official Hackernews API Install go get github.com/PaulRosset/go-hacknews Usage Few examples a

Sep 27, 2022

A golang client for the Twitch v3 API - public APIs only (for now)

go-twitch Test CLIENT_ID="my client ID" go test -v -cover Usage Example File: package main import ( "log" "os" "github.com/knspriggs/go-twi

Sep 27, 2022
Comments
  • Webhook example

    Webhook example

    Hi, I'm trying/struggling to build a Patreon webhook using this library.

    Does anyone have a working example I could use as a start? I find the webhook data structures quite complex and I wouldn't want to spend time on this if anyone else did it already.

  • use token to get info failer, nothing to got.

    use token to get info failer, nothing to got.

    config := oauth2.Config{ ClientID: "myclient", ClientSecret: "mysecret", Endpoint: oauth2.Endpoint{ AuthURL: "https://www.patreon.com/api/oauth2/authorize", TokenURL: "https://www.patreon.com/api/oauth2/token", }, Scopes: []string{"identity","identity[email]","identity.memberships","campaigns", "campaigns.members", "campaigns.members[email]", "campaigns.members.address"},//"users", "pledges-to-me", "my-campaign"}, } //1.to get token token, err := config.PasswordCredentialsToken(ctx, username, password)

    tc := oauth2.NewClient(ctx, config.TokenSource(ctx, token)) client := patreon.NewClient(tc)

    //2.to get profiler info emial, username, image,others user, err := client.GetIdentity()

    //3..... campaign, err := client.GetCampaigns()

    both GetIdentity and GetCampaigns got null, image

    please help me , if i can not solve the problem,my boss will fire me. thank you very much,you can also use email:[email protected] or wechat guagual to contact me ,thank you again.

  • Invalid Grant

    Invalid Grant

    I'm attempting to call NewPatreonClient() for as defined in the README and receiving this error:

    panic: Get "https://api.patreon.com/oauth2/api/current_user": oauth2: cannot fetch token: 401 Unauthorized
    Response: {"error": "invalid_grant"}
    

    I did modify the NewPatreonClient() in the README slightly because it was throwing errors due to missing a return and NewClient method not referencing the package. Here's my code:

    func NewPatreonClient() (*patreon.Client, error) {
    	config := oauth2.Config{
    		ClientID:     os.Getenv("PATREON_CLIENT_ID"),
    		ClientSecret: os.Getenv("PATREON_CLIENT_SECRET"),
    		Endpoint: oauth2.Endpoint{
    			AuthURL:  AUTHORIZATION_URL,
    			TokenURL: ACCESS_TOKEN_URL,
    		},
    		Scopes: []string{"users", "pledges-to-me", "my-campaign"},
    	}
    
    	token := oauth2.Token{
    		AccessToken:  os.Getenv("PATREON_CREATOR_ACCESS_TOKEN"),
    		RefreshToken: os.Getenv("PATREON_CREATOR_REFRESH_TOKEN"),
    		// Must be non-nil, otherwise token will not be expired
    		Expiry: time.Now().Add(-24 * time.Hour),
    	}
    
    	tc := config.Client(context.Background(), &token)
    
    	client := patreon.NewClient(tc)
    	user, err := client.FetchUser()
    	if err != nil {
    		panic(err)
    	}
    
    	fmt.Printf("%v\n", user)
    
    	return client, nil
    }
    

    I'm using patreon-go.v1

Client-go - Clusterpedia-client supports the use of native client-go mode to call the clusterpedia API

clusterpedia-client supports the use of native client-go mode to call the cluste

Dec 5, 2022
A Go client implementing a client-side distributed consumer group client for Amazon Kinesis.
A Go client implementing a client-side distributed consumer group client for Amazon Kinesis.

Kinesumer is a Go client implementing a client-side distributed consumer group client for Amazon Kinesis.

Jan 5, 2023
Nutanix-client-go - Go client for the Nutanix Prism V3 API

nutanix-client-go This repository contains portions of the Nutanix API client code in nutanix/terraform-provider-nutanix. It has been extracted to red

Jan 6, 2022
Go client for the YNAB API. Unofficial. It covers 100% of the resources made available by the YNAB API.

YNAB API Go Library This is an UNOFFICIAL Go client for the YNAB API. It covers 100% of the resources made available by the YNAB API. Installation go

Oct 6, 2022
An API client for the Notion API implemented in Golang

An API client for the Notion API implemented in Golang

Dec 30, 2022
A Wrapper Client for Google Spreadsheet API (Sheets API)

Senmai A Wrapper Client for Google Spreadsheet API (Sheets API) PREPARATION Service Account and Key File Create a service account on Google Cloud Plat

Nov 5, 2021
Simple-Weather-API - Simple weather api app created using golang and Open Weather API key
Simple-Weather-API - Simple weather api app created using golang and Open Weather API key

Simple Weather API Simple weather api app created using golang and Open Weather

Feb 6, 2022
Client for the cloud-iso-client

cloud-iso-client Client for the cloud-iso-client. Register an API token Before using this client library, you need to register an API token under your

Dec 6, 2021
Go-http-client: An enhanced http client for Golang
Go-http-client: An enhanced http client for Golang

go-http-client An enhanced http client for Golang Documentation on go.dev ?? This package provides you a http client package for your http requests. Y

Jan 7, 2023
Aoe4-client - Client library for aoe4 leaderboards etc

AOE4 Client Overview This is a go client used to query AOE4 data from either the

Jan 18, 2022