You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

token.go 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package apijson
  2. import (
  3. "bytes"
  4. "strconv"
  5. "encoding/base64"
  6. )
  7. func GenChannelPageToken(channelId string, page uint64) string {
  8. // Generate the inner token
  9. token := genInnerToken(page)
  10. // Build the inner object
  11. var inner bytes.Buffer
  12. // channelId
  13. inner.WriteByte(0x12) // type
  14. writeVarint(&inner, uint64(len(channelId))) // len
  15. inner.WriteString(channelId) // data
  16. // token
  17. inner.WriteByte(0x1a) // type
  18. writeVarint(&inner, uint64(len(token))) // len
  19. inner.WriteString(token) // data
  20. innerBytes := inner.Bytes()
  21. var root bytes.Buffer
  22. // innerBytes
  23. root.Write([]byte{0xe2, 0xa9, 0x85, 0xb2, 0x02}) // probably types
  24. writeVarint(&root, uint64(len(innerBytes)))
  25. root.Write(innerBytes)
  26. rootBytes := root.Bytes()
  27. return base64.URLEncoding.EncodeToString(rootBytes)
  28. }
  29. func genInnerToken(page uint64) string {
  30. var buf bytes.Buffer
  31. pageStr := strconv.FormatUint(page, 10)
  32. // Probably protobuf
  33. buf.Write([]byte{0x12, 0x06})
  34. buf.WriteString("videos")
  35. buf.Write([]byte{
  36. 0x20, 0x00, 0x30, 0x01, 0x38, 0x01, 0x60, 0x01,
  37. 0x6a, 0x00, 0x7a,
  38. })
  39. // Write size-prefixed page string
  40. writeVarint(&buf, uint64(len(pageStr)))
  41. buf.WriteString(pageStr)
  42. buf.Write([]byte{0xb8, 0x01, 0x00})
  43. return base64.URLEncoding.EncodeToString(buf.Bytes())
  44. }
  45. func writeVarint(buf *bytes.Buffer, n uint64) {
  46. var enc [10]byte
  47. i := uint(0)
  48. for {
  49. enc[i] = uint8(n & 0x7F)
  50. n >>= 7
  51. if n != 0 {
  52. enc[i] |= 0x80
  53. i++
  54. } else {
  55. i++
  56. break
  57. }
  58. }
  59. buf.Write(enc[:i])
  60. }