Generate Video Access token (JWT) with Twillio Go SDK

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

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。也许有人会觉得这个解决方案很方便:

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/dvsekhvalnov/jose2go"
  6. "log"
  7. "time"
  8. )
  9. func main() {
  10. accountSid := "XXX"
  11. keySid := "YYY"
  12. keySecret := "ZZZ"
  13. username := "Simon"
  14. roomName := "SimonsRoom"
  15. now := time.Now()
  16. type JWTPayload struct {
  17. Jti string `json:"jti"`
  18. Issuer string `json:"iss"`
  19. Subject string `json:"sub"`
  20. CreationUnixTimestamp int64 `json:"iat"`
  21. NotBeforeUnixTimestamp int64 `json:"nbf"`
  22. ExpiresUnixTimestamp int64 `json:"exp"`
  23. Grants struct {
  24. Identity string `json:"identity"`
  25. Video struct {
  26. Room string `json:"room"`
  27. } `json:"video"`
  28. } `json:"grants"`
  29. }
  30. payload := JWTPayload{
  31. Jti: fmt.Sprintf("%s-%d", keySid, now.UnixNano()),
  32. Issuer: keySid,
  33. Subject: accountSid,
  34. CreationUnixTimestamp: now.Unix(),
  35. NotBeforeUnixTimestamp: now.Unix(),
  36. ExpiresUnixTimestamp: now.Add(23 * time.Hour).Unix(),
  37. Grants: struct {
  38. Identity string `json:"identity"`
  39. Video struct {
  40. Room string `json:"room"`
  41. } `json:"video"`
  42. }{
  43. Identity: username,
  44. Video: struct {
  45. Room string `json:"room"`
  46. }{
  47. Room: roomName,
  48. },
  49. },
  50. }
  51. payloadByte, err := json.Marshal(payload)
  52. if err != nil {
  53. log.Fatal(err)
  54. }
  55. token, err := jose.SignBytes(payloadByte, jose.HS256, []byte(keySecret),
  56. jose.Header("cty", "twilio-fpa;v=1"),
  57. jose.Header("typ", "JWT"),
  58. jose.Header("alg", "HS256"),
  59. )
  60. if err != nil {
  61. log.Fatal(err)
  62. }
  63. fmt.Println(token)
  64. }
英文:

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:

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/dvsekhvalnov/jose2go"
  6. "log"
  7. "time"
  8. )
  9. func main() {
  10. accountSid := "XXX"
  11. keySid := "YYY"
  12. keySecret := "ZZZ"
  13. username := "Simon"
  14. roomName := "SimonsRoom"
  15. now := time.Now()
  16. type JWTPayload struct {
  17. Jti string `json:"jti"`
  18. Issuer string `json:"iss"`
  19. Subject string `json:"sub"`
  20. CreationUnixTimestamp int64 `json:"iat"`
  21. NotBeforeUnixTimestamp int64 `json:"nbf"`
  22. ExpiresUnixTimestamp int64 `json:"exp"`
  23. Grants struct {
  24. Identity string `json:"identity"`
  25. Video struct {
  26. Room string `json:"room"`
  27. } `json:"video"`
  28. } `json:"grants"`
  29. }
  30. payload := JWTPayload{
  31. Jti: fmt.Sprintf("%s-%d",keySid,now.UnixNano()),
  32. Issuer: keySid,
  33. Subject: accountSid,
  34. CreationUnixTimestamp: now.Unix(),
  35. NotBeforeUnixTimestamp: now.Unix(),
  36. ExpiresUnixTimestamp: now.Add(23*time.Hour).Unix(),
  37. Grants: struct {
  38. Identity string `json:"identity"`
  39. Video struct {
  40. Room string `json:"room"`
  41. } `json:"video"`
  42. }{
  43. Identity: username,
  44. Video: struct {
  45. Room string `json:"room"`
  46. }{
  47. Room: roomName,
  48. },
  49. },
  50. }
  51. payloadByte, err := json.Marshal(payload)
  52. if err != nil {
  53. log.Fatal(err)
  54. }
  55. token, err := jose.SignBytes(payloadByte, jose.HS256, []byte(keySecret),
  56. jose.Header("cty", "twilio-fpa;v=1"),
  57. jose.Header("typ", "JWT"),
  58. jose.Header("alg", "HS256"),
  59. )
  60. if err != nil {
  61. log.Fatal(err)
  62. }
  63. fmt.Println(token)
  64. }

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:

确定