Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oauth2 in Go with Martini - ResponseWriter syntax for Reddit

Tags:

go

oauth

I have been banging my head for two days on this and am clearly missing something. I am a bit of a doofus on backend/server development and hoping someone can point me in the right direction.

  • I have a desktop application (not Go) which makes an OAuth2 request from Reddit.
  • The OAuth2 in my application is working just fine however the flow fails when Reddit hit the redirect URI on my own server.
  • I am guessing it is waiting for the proper ResponseWriter result and none of my dozen incompetent attempts have worked.
  • The redirect URI hits my server and callback function (below) then does nothing.

Questions

  • Where am I going wrong?
  • Is variable "t" my auth code and am I done (aka, you are a buffoon!)?
  • Can I just write t's value to my non-Go app and be done?
  • Or am I missing a step?
  • Note: code slightly simplified.

Thanks!

package main

import (
    "code.google.com/p/goauth2/oauth"
    "fmt"
    "github.com/codegangsta/martini"
    "io"
    "net/http"
)

var config = &oauth.Config{
    ClientId:     CLIENT_ID,
    ClientSecret: CLIENT_SECRET,
    Scope:        "identify",
    AuthURL:      "https://ssl.reddit.com/api/v1/authorize",
    TokenURL:     "https://ssl.reddit.com/api/v1/access_token",
    RedirectURL:  "http://localhost:3000/reddit_oauth",
}

func main() {
    m := martini.Classic()
    m.Get("/reddit_oauth", handleCallback)
    m.Run()
}

func handleCallback(w http.ResponseWriter, r *http.Request) {
    //Get the code from the response
    code := r.FormValue("code")

    // Exchange the received code for a token
    t := &oauth.Transport{Config: config}
    t.Exchange(code)

    // Am I done?
}

Points of reference

  • Reddit API
    • https://github.com/reddit/reddit/wiki/API
  • Reddit specific PHP example
    • https://github.com/reddit/reddit/wiki/OAuth2-PHP-Example
  • Reddit specific Python example
    • https://github.com/reddit/reddit/wiki/OAuth2-Python-Example
  • Martini
    • https://github.com/codegangsta/martini
  • Go OAuth2
    • https://code.google.com/p/goauth2/
    • https://code.google.com/p/goauth2/source/browse/oauth/oauth.go

Misc

  • Why Martini? It looks bloody great.
  • Why not just Oauth2? Because I am ignorant.
  • Why not PHP/Python? Because, c'mon! I am trying to learn Go. (I am loving it and getting some great results which enhances my UI work.)
like image 384
Geoffrey Avatar asked Nov 24 '13 18:11

Geoffrey


2 Answers

Okay, the answer mostly sat in my client application--again, not Go--which had a few missing aspects in its OAuth2 request. (It also took a little effort to get the headers correct for the different requests.)

The best info for Reddit's OAuth2 process I found was here: http://www.reddit.com/r/redditdev/comments/197x36/using_oauth_to_send_valid_requests/

The response from Reddit still pings me asking for the ClientID and ClientSecret, which I am sure could be served via a proper ResponseWriter, though for the moment I am simply copy/pasting into a popup just so I can focus on something else!

When I get that squared away I will add to this answer.

If anyone is interested in any more information, please do not hesitate to ask.

Thanks again, TomWilde and Elithrar!

like image 186
Geoffrey Avatar answered Oct 24 '22 03:10

Geoffrey


Checkout the martini-contrib page for an OAuth2 implementation.

like image 32
aminjam Avatar answered Oct 24 '22 03:10

aminjam