LoginSignup
15
14

More than 5 years have passed since last update.

Go言語のgoauth2を準公式のoauth2に変更

Posted at

Go言語でOAuth2を使うためのライブラリ goauth2 が deprecated になっていて、準公式の oauth2 に変更したときのメモ。

JWTを使った認証を例にしています。

import

変更前

import "code.google.com/p/goauth2/oauth/jwt"

変更後

import "golang.org/x/net/context"
import "golang.org/x/oauth2/jwt"

context.Context が必要になる。

http.Client 取得

変更前

token := jwt.NewToken(email, scope, pem)
transport, err := jwt.NewTransport(token)
if err != nil {
    return err
}
client := transport.Client()

変更後

cfg := jwt.Config{
    Email:      email,
    PrivateKey: pem,
    Scopes:     []string{scope},
    TokenURL:   tokenURL,
}
ctx := context.Background()
client := cfg.Client(ctx)

TokenURLは、Googleならhttps://accounts.google.com/o/oauth2/token

15
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
14