Skip to content

Instantly share code, notes, and snippets.

@apeyroux
Created July 29, 2014 16:36
Show Gist options
  • Save apeyroux/4eeb24c49d57b308a800 to your computer and use it in GitHub Desktop.
Save apeyroux/4eeb24c49d57b308a800 to your computer and use it in GitHub Desktop.
Exemple auth online.net with golang
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"time"
)
const (
CLIENT_ID = "20270_1zord46r78u888g84wco4c84040ckgcgkwocokswskk8s000wo"
GRANT_TYPE = "http://oauth.net/grant_type/device/1.0"
URL_AUTH = "https://console.online.net/oauth/v2/device/code?client_id=" + CLIENT_ID + "&new_credentials=yes"
URL_IDENTIFIANTS = "https://console.online.net/oauth/v2/device/credentials"
URL_TOCKEN = "https://console.online.net/oauth/v2/token"
)
type Authentification struct {
Device_code string
User_code string
Verification_url string
Expires_in int
Interval int
}
type Identifiant struct {
Client_id string
Client_secret string
}
type Tocken struct {
Access_token string
Token_type string
Expires_in int
}
func servers(token Tocken) {
// passer le tocken en param à la func puis l'ajouter dans le header Authorization: Bearer your_api_token
http_client := &http.Client{}
req, err := http.NewRequest("GET", "https://api.online.net/api/v1/server/", nil)
if err != nil {
fmt.Println("ya comme une err %s", err)
} else {
fmt.Println("req ...")
}
req.Header.Add("Authorization:", "Bearer "+token.Access_token)
resp, err := http_client.Do(req)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("ya comme une err %s", err)
} else {
fmt.Println(string(body[:]))
}
}
func getTocken(auth Authentification, ident Identifiant) Tocken {
var tocken Tocken
resp, err := http.PostForm(URL_TOCKEN,
url.Values{"client_id": {ident.Client_id},
"client_secret": {ident.Client_secret},
"code": {auth.Device_code},
"grant_type": {GRANT_TYPE}})
if err != nil {
fmt.Errorf("Ya comme un blem (%s) ...", err)
os.Exit(0)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Errorf("Ya comme un blem (%s) ...", err)
os.Exit(0)
}
jsdec := json.NewDecoder(bytes.NewReader(body))
err2 := jsdec.Decode(&tocken)
if err2 != nil {
fmt.Errorf("Ya comme un blem (%s) ...", err2)
os.Exit(0)
}
return tocken
}
func checkClientId(auth Authentification) Identifiant {
var identifiant Identifiant
urlws := fmt.Sprintf("%s?client_id=%s&code=%s", URL_IDENTIFIANTS, CLIENT_ID, auth.Device_code)
resp, err := http.Get(urlws)
if err != nil {
fmt.Errorf("Ya comme un blem (%s) ...", err)
os.Exit(0)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Errorf("Ya comme un blem (%s) ...", err)
os.Exit(0)
}
jsdec := json.NewDecoder(bytes.NewReader(body))
err2 := jsdec.Decode(&identifiant)
if err2 != nil {
fmt.Errorf("Ya comme un blem (%s) ...", err2)
os.Exit(0)
}
return identifiant
}
func setTocken(tocken *Tocken) {
var auth Authentification
var ident Identifiant
resp, err := http.Get(URL_AUTH)
if err != nil {
fmt.Errorf("Ya comme un blem (%s) ...", err)
os.Exit(0)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Errorf("Ya comme un blem (%s) ...", err)
os.Exit(0)
}
jsdec := json.NewDecoder(bytes.NewReader(body))
err2 := jsdec.Decode(&auth)
if err2 != nil {
fmt.Errorf("Ya comme un blem (%s) ...", err2)
os.Exit(0)
}
fmt.Printf("Please, go to %s and enter %s code.\n", auth.Verification_url, auth.User_code)
for {
time.Sleep(5 * time.Second)
ident = checkClientId(auth)
if ident.Client_secret != "" {
break
}
}
*tocken = getTocken(auth, ident)
}
func main() {
var tocken Tocken
setTocken(&tocken)
fmt.Printf("t:%s\n", tocken.Access_token)
servers(tocken)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment