douzhong1907 2013-11-24 18:57
浏览 29
已采纳

与Martini一起使用的Oauth2-Reddit的ResponseWriter语法

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

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.)
  • 写回答

2条回答 默认 最新

  • doushenken2833 2013-11-25 22:25
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元