Skip to content

Instantly share code, notes, and snippets.

@ilovelili
Last active June 13, 2021 07:52
Show Gist options
  • Save ilovelili/1904d7b7412c89a50e4645ee232fc0e2 to your computer and use it in GitHub Desktop.
Save ilovelili/1904d7b7412c89a50e4645ee232fc0e2 to your computer and use it in GitHub Desktop.
Z様 OAuth1 integration
package handler
import (
"fmt"
"net/http"
"github.com/labstack/echo"
"github.com/woodstock-tokyo/woodstock-zaim/server/util"
)
var (
config = util.LoadConfig()
consts = util.LoadConst()
oauthClient = &oauth.Client{
TemporaryCredentialRequestURI: config.RequestTokenURL, // https://api.zaim.net/v2/auth/request
ResourceOwnerAuthorizationURI: config.AuthorizeURL, // https://auth.zaim.net/users/auth
TokenRequestURI: config.AccessTokenURL, // https://api.zaim.net/v2/auth/access
Credentials: oauth.Credentials{
Token: config.ConsumerKey,
Secret: config.ConsumerSecret,
},
}
)
const (
tempCredKey = "tempCred"
tokenCredKey = "tokenCred"
)
// Authorize GET /auth
func Authorize(c echo.Context) error {
tempCred, err := oauthClient.RequestTemporaryCredentials(nil, config.CallbackURL, nil)
if err != nil {
fmt.Println(err)
return util.ResponseError(c, consts.GetError("500-001"), err)
}
s := util.GetSession(c.Request())
s[tempCredKey] = tempCred
if err := util.SaveSession(c.Response(), c.Request(), s); err != nil {
return util.ResponseError(c, consts.GetError("500-002"), err)
}
return c.Redirect(http.StatusFound, oauthClient.AuthorizationURL(tempCred, nil))
}
// Callback GET /auth/callback
// todo
func Callback(c echo.Context) error {
fmt.Println(c.Request())
return c.NoContent(http.StatusOK)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment