Documentation ¶
Overview ¶
Package ghoauth provides acces_token creation for your github application
Two steps:
- redirect user to github auth page
- create an access_token
Example:
package main import ( "github.com/vvo/go-ghoauth" "net/http" ) var ghflow = ghoauth.New(&ghoauth.Config{ ClientId: "82c041166d565cf2f149", ClientSecret: "6458b989c06d8106f468fd47106d8654e3887d5b", RedirectUri: "http://localhost:8080/callback", Scope: "user:public", }) func main() { // step 1: redirect users to the right github oauth page // https://developer.github.com/v3/oauth/#redirect-users-to-request-github-access http.HandleFunc("/login", ghflow.Login) http.HandleFunc("/callback", callback) http.HandleFunc("/", root) panic(http.ListenAndServe(":8080", nil)) } func callback(w http.ResponseWriter, r *http.Request) { // step 2: github calls you using redirect_uri // we generate a token using github oauth access_token POST url // callback does not responds to client for you, it only generates a token // https://developer.github.com/v3/oauth/#github-redirects-back-to-your-site token, err := ghflow.Callback(r) if err != nil { w.Write([]byte("UhOh!")) } // token is now available and usable w.Write([]byte("Here's your access_token budy: " + token)) } func root(w http.ResponseWriter, r *http.Request) { w.Write([]byte("<a href=/login>login</a>")) }
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrStateNotFound = errors.New("Provided `state` was not found")
refers to https://developer.github.com/v3/oauth/#github-redirects-back-to-your-site "If the states don’t match, the request has been created by a third party and the process should be aborted."
Functions ¶
Types ¶
type Config ¶
type Config struct { ClientId string ClientSecret string RedirectUri string // Comma separated list of scopes https://developer.github.com/v3/oauth/#scopes Scope string // Defaults to defaultBaseUrl BaseUrl string }
See https://developer.github.com/v3/oauth/#web-application-flow to understand how to fill the properties
type GithubError ¶
type GithubError struct { RawError string `json:"error"` ErrorDescription string `json:"error_description"` ErrorUri string `json:"error_uri"` }
func (*GithubError) Error ¶
func (e *GithubError) Error() string
Click to show internal directories.
Click to hide internal directories.