Generate Video Access token (JWT) with Twillio Go SDK

huangapple go评论119阅读模式
英文:

Generate Video Access token (JWT) with Twillio Go SDK

问题

我无法为 Twilio 视频产品使用 golang SDK 创建访问令牌。在文档中,遗憾的是没有提到 golang(https://www.twilio.com/docs/video/tutorials/user-identity-access-tokens#generating-access-tokens)。

你们中是否有人知道我如何创建所需的访问令牌?

在其他语言的示例中提到的 JWT 包在 Go SDK 中找不到。

英文:

I am failing to create an Access Token for the Twillio Video Product with the golang SDK
In the docs there is golang sadly not mentioned

Does anyone of you know how I can create the required access token?

The JWT package mentioned in the examples of the other languages can not be found in the Go SDK.

答案1

得分: 2

我发现使用默认的Go SDK是不可能做到的。我按照https://www.twilio.com/docs/iam/access-tokens上的说明构建了JWT。也许有人会觉得这个解决方案很方便:

package main

import (
	"encoding/json"
	"fmt"
	"github.com/dvsekhvalnov/jose2go"
	"log"
	"time"
)

func main() {
	accountSid := "XXX"
	keySid := "YYY"
	keySecret := "ZZZ"

	username := "Simon"
	roomName := "SimonsRoom"

	now := time.Now()

	type JWTPayload struct {
		Jti                    string `json:"jti"`
		Issuer                 string `json:"iss"`
		Subject                string `json:"sub"`
		CreationUnixTimestamp  int64  `json:"iat"`
		NotBeforeUnixTimestamp int64  `json:"nbf"`
		ExpiresUnixTimestamp   int64  `json:"exp"`
		Grants                 struct {
			Identity string `json:"identity"`
			Video    struct {
				Room string `json:"room"`
			} `json:"video"`
		} `json:"grants"`
	}

	payload := JWTPayload{
		Jti:                    fmt.Sprintf("%s-%d", keySid, now.UnixNano()),
		Issuer:                 keySid,
		Subject:                accountSid,
		CreationUnixTimestamp:  now.Unix(),
		NotBeforeUnixTimestamp: now.Unix(),
		ExpiresUnixTimestamp:   now.Add(23 * time.Hour).Unix(),
		Grants: struct {
			Identity string `json:"identity"`
			Video    struct {
				Room string `json:"room"`
			} `json:"video"`
		}{
			Identity: username,
			Video: struct {
				Room string `json:"room"`
			}{
				Room: roomName,
			},
		},
	}

	payloadByte, err := json.Marshal(payload)
	if err != nil {
		log.Fatal(err)
	}
	token, err := jose.SignBytes(payloadByte, jose.HS256, []byte(keySecret),
		jose.Header("cty", "twilio-fpa;v=1"),
		jose.Header("typ", "JWT"),
		jose.Header("alg", "HS256"),
	)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(token)
}
英文:

I found that it is just not possible to do it with the default go SDK. I did follow the instructions on https://www.twilio.com/docs/iam/access-tokens and build the JWT myself. Maybe someone will find the solution handy:

package main
import (
"encoding/json"
"fmt"
"github.com/dvsekhvalnov/jose2go"
"log"
"time"
)
func main() {
accountSid := "XXX"
keySid := "YYY"
keySecret := "ZZZ"
username := "Simon"
roomName := "SimonsRoom"
now := time.Now()
type JWTPayload struct {
Jti                    string `json:"jti"`
Issuer                 string `json:"iss"`
Subject                string `json:"sub"`
CreationUnixTimestamp  int64  `json:"iat"`
NotBeforeUnixTimestamp int64  `json:"nbf"`
ExpiresUnixTimestamp   int64  `json:"exp"`
Grants                 struct {
Identity string `json:"identity"`
Video    struct {
Room string `json:"room"`
} `json:"video"`
} `json:"grants"`
}
payload := JWTPayload{
Jti:                    fmt.Sprintf("%s-%d",keySid,now.UnixNano()),
Issuer:                 keySid,
Subject:                accountSid,
CreationUnixTimestamp:  now.Unix(),
NotBeforeUnixTimestamp: now.Unix(),
ExpiresUnixTimestamp:   now.Add(23*time.Hour).Unix(),
Grants: struct {
Identity string `json:"identity"`
Video    struct {
Room string `json:"room"`
} `json:"video"`
}{
Identity: username,
Video: struct {
Room string `json:"room"`
}{
Room: roomName,
},
},
}
payloadByte, err := json.Marshal(payload)
if err != nil {
log.Fatal(err)
}
token, err := jose.SignBytes(payloadByte, jose.HS256, []byte(keySecret),
jose.Header("cty", "twilio-fpa;v=1"),
jose.Header("typ", "JWT"),
jose.Header("alg", "HS256"),
)
if err != nil {
log.Fatal(err)
}
fmt.Println(token)
}

huangapple
  • 本文由 发表于 2021年6月4日 15:34:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/67833205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定