引用
引文以google为例子,这里使用github为例子(访问google有问题T_T)。
获取 oauth2
$ go get golang.org/x/oauth2
如果出现错误(被墙?),请参直接使用github下载即可。
$ mkdir -p $GOPATH/src/golang.org/x
$ cd $GOPATH/src/golang.org/x
$ git clone https://github.com/golang/oauth2.git
创建client id
访问这里: https://github.com/settings/developers
, 点击“New OAuth App”,示例配置如下:
Homepage URL : http://localhost:8000/
Authorization callback URL:http://localhost:8000/GithubCallback
创建成功后,会生产Client ID
和Client Secret
源程序
package main
import (
"fmt"
"io/ioutil"
"net/http"
"golang.org/x/oauth2"
)
const htmlIndex = `<html><body>
<a href="/GithubLogin">Log in with Github</a>
</body></html>
`
// Endpoint is Github's OAuth 2.0 endpoint.
var endpoint = oauth2.Endpoint{
AuthURL: "https://github.com/login/oauth/authorize",
TokenURL: "https://github.com/login/oauth/access_token",
}
var githubOauthConfig = &oauth2.Config{
ClientID: "YourClientID",
ClientSecret: "YourClientSecret",
RedirectURL: "http://localhost:8000/GithubCallback",
Scopes: []string{"user", "repo"},
Endpoint: endpoint,
}
const oauthStateString = "random"
func main() {
http.HandleFunc("/", handleMain)
http.HandleFunc("/GithubLogin", handleGithubLogin)
http.HandleFunc("/GithubCallback", handleGithubCallback)
fmt.Println(http.ListenAndServe(":8000", nil))
}
func handleMain(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, htmlIndex)
}
func handleGithubLogin(w http.ResponseWriter, r *http.Request) {
url := githubOauthConfig.AuthCodeURL(oauthStateString)
fmt.Println(url)
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}
func handleGithubCallback(w http.ResponseWriter, r *http.Request) {
state := r.FormValue("state")
if state != oauthStateString {
fmt.Printf("invalid oauth state, expected '%s', got '%s'\n", oauthStateString, state)
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
return
}
fmt.Println(state)
code := r.FormValue("code")
fmt.Println(code)
token, err := githubOauthConfig.Exchange(oauth2.NoContext, code)
fmt.Println(token)
if err != nil {
fmt.Printf("Code exchange failed with '%s'\n", err)
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
return
}
response, err := http.Get("https://api.github.com/user?access_token=" + token.AccessToken)
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
fmt.Fprintf(w, "%s\n", contents)
}
修改上面的 ClientID
和ClientSecret
即可。