PART 2: How To Get A Token On S3zipper Api
To use the S3zipper API, the first thing we will need to get is a user token. Tokens carry user-specific information and make it simple for users to access certain resources securely. Instead of needing to provide a user-name and password, a user only needs to provide a server generated token to perform transactions.
For this, we will need to create a user account. Next step is to get userkey
and usersecret
under /developer
section. Follow the link below.
Create account or login
Once you have the user key and user secret, you can now go ahead and get a token.
The example below uses Go(Golang) and Echo web framework.
To see other examples in other languages visit API documentation.
Also available on GitHub
CODE BELOW
package main
import (
"github.com/labstack/echo"
"net/http"
"net/url"
"io/ioutil"
"encoding/json"
"log"
"strings"
"time"
)
func getJWTToken2(c echo.Context) (err error) {
// initiate the client
client := &http.Client{}
// set the form
form := url.Values{}
form.Add("userKey", "userkey")
form.Add("userSecret", "usersecret")
// create a new request
req2 , err2:= http.NewRequest("POST", "https://api.s3zipper.com/gentoken", strings.NewReader(form.Encode()))
if err2 != nil {
log.Fatal("NewRequest: ", err2)
return err2
}
// set form in request
req2.PostForm = form
req2.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// get response
resp3 ,err3 := client.Do(req2)
if err3 != nil{
log.Fatal("NewRequest: ", err3)
return err3
}
defer resp3.Body.Close()
// read the body
body, err4:= ioutil.ReadAll(resp3.Body)
if err4 != nil{
return err4
}
/*******************************************************************/
// Get the token from the body
var p GetToken
err = json.Unmarshal(body, &p)
if err != nil {
log.Fatal("NewRequest: ", err)
return err
}
token := p.Token
//save token in cookie
if len(token) > 1{
//save token
cookie := new(http.Cookie)
cookie.Name = "newJwtToken"
cookie.Value = token
cookie.Path = "/"
cookie.Expires = time.Now().Add(24 * time.Hour)
c.SetCookie(cookie)
}else{
return c.JSON(http.StatusOK, "No token was saved in cookie")
}
return c.JSON(http.StatusOK, map[string]string{
"token": token,
})
}