示例程序使用 mrjones/oauth 库演示:

  • Twitter 账号授权.
  • Fetching mentions for that Twitter account.
  • Tweeting on behalf of that Twitter account.

程序内容如下:

twitter-oauth/app/
	models
		user.go   # User struct and in-memory data store
	controllers
		app.go    # All code

浏览代码

OAuth 概要

The OAuth process is governed by this configuration:

var TWITTER = oauth.NewConsumer(
	"VgRjky4dTA1U2Ck16MmZw",
	"l8lOLyIF3peCFEvrEoTc8h4oFwieAFgPM6eeTRo30I",
	oauth.ServiceProvider{
		AuthorizeTokenUrl: "https://api.twitter.com/oauth/authorize",
		RequestTokenUrl:   "https://api.twitter.com/oauth/request_token",
		AccessTokenUrl:    "https://api.twitter.com/oauth/access_token",
	},
)

处理过程如下:

  1. 程序生成用户令牌,发送到 Twitter.
  2. 用户授权应用程序
  3. 推特将用户重定向到提供的url, 包含验证参数
  4. 程序使用请求令牌和验证参数构造一个请求到推特,推特返回一个AccessToken
  5. 以后就可以使用AccessToken访问推特了