Skip to content

Instantly share code, notes, and snippets.

@suyash
Last active October 12, 2017 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suyash/988c8646d2eb858779b001d7a2884627 to your computer and use it in GitHub Desktop.
Save suyash/988c8646d2eb858779b001d7a2884627 to your computer and use it in GitHub Desktop.
Pocket API authentication demo
go run main.go -key ...

and go to http://localhost:4343/login

package main
import (
"bytes"
"encoding/json"
"errors"
"flag"
"io/ioutil"
"log"
"net/http"
"net/url"
)
var address *string
var key *string
func init() {
address = flag.String("address", ":4343", "address to run the server at")
key = flag.String("key", "", "application key for Pocket API access")
}
func main() {
flag.Parse()
if *key == "" {
log.Fatal("key is required")
}
http.HandleFunc("/login", handleLogin)
http.HandleFunc("/redirect", handleRedirect)
if err := http.ListenAndServe(*address, nil); err != nil {
log.Fatal(err)
}
}
type codeParameters struct {
ConsumerKey string `json:"consumer_key"`
RedirectURI string `json:"redirect_uri"`
}
type codeResponse struct {
Code string `json:"code"`
}
func handleLogin(w http.ResponseWriter, r *http.Request) {
data, err := json.Marshal(codeParameters{ConsumerKey: *key, RedirectURI: "http://localhost" + *address + "/redirect"})
if err != nil {
log.Fatal(err)
}
log.Println("data:", string(data))
d, err := request("https://getpocket.com/v3/oauth/request", data)
if err != nil {
log.Fatal(err)
}
response := &codeResponse{}
err = json.Unmarshal(d, response)
if err != nil {
log.Fatal(err)
}
log.Println("code:", response.Code)
url := "https://getpocket.com/auth/authorize?request_token=" + response.Code +
"&redirect_uri=http://localhost" + *address + "/" +
url.PathEscape("redirect?code="+response.Code)
http.Redirect(w, r, url, http.StatusFound)
}
type authorizeParameters struct {
ConsumerKey string `json:"consumer_key"`
Code string `json:"code"`
}
type authorizeResponse struct {
AccessToken string `json:"access_token"`
Username string `json:"username"`
}
func handleRedirect(w http.ResponseWriter, r *http.Request) {
code := r.URL.Query().Get("code")
data, err := json.Marshal(authorizeParameters{ConsumerKey: *key, Code: code})
if err != nil {
log.Fatal(err)
}
d, err := request("https://getpocket.com/v3/oauth/authorize", data)
if err != nil {
log.Fatal(err)
}
response := &authorizeResponse{}
err = json.Unmarshal(d, response)
if err != nil {
log.Fatal(err)
}
log.Println("access_token:", response.AccessToken)
w.Write([]byte("Hello " + response.Username))
}
func request(url string, data []byte) ([]byte, error) {
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(data))
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
req.Header.Set("X-Accept", "application/json")
if err != nil {
return nil, err
}
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, errors.New("request to " + url + " failed with status " + res.Status)
}
d, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
return d, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment