按照文档中关于token获取的响应
参数 | 类型 | 说明 |
---|---|---|
Token | token对象 | 包含了具体的token值和有效期时间戳 |
Id | String | 本次请求分配的token值 |
ExpireTime | Long | token的有效期时间戳(单位:秒,例如1553825814换算为北京时间为:2019/3/29 10:16:54,即token在该时间之前有效。) |
Id为获取的token
响应Body JSON字符串格式解析:
{
"NlsRequestId": "dd05a301b40441c99a2671905325fb1f",
"RequestId": "E11F2DC2-0163-4D97-A704-0BD28045608A",
"ErrMsg": "",
"Token": {
"ExpireTime": 1553592564,
"Id": "889******166",
"UserId": "150**********151"
}
}
因此
需要把token的内容单独切出来:
package main
import (
"fmt"
"strings"
)
func main() {
str := "\"NlsRequestId\" \"Id\": \"889******166\",\"UserId\": \"150**********151\""
token1 := strings.Index(str, "\"Id\":")
token2 := strings.Index(str, "\"UserId\"")
token := str[token1+7:token2-2]
fmt.Println(token)
}
输出:
有关请求参数中的uuid
名称 | 类型 | 是否必需 | 说明 |
---|---|---|---|
SignatureNonce | String | 是 | 唯一随机数uuid,用于请求的防重放攻击,每次请求唯一,不能重复使用。格式为xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx(8-4-4-4-12),例如8d1e6a7a-f44e-40d5-aedb-fe4a1c80f434 |
其中:SignatureNonce
参考:https://blog.csdn.net/oguro/article/details/112195662
是随机生成的字符串:
package main
import (
"fmt"
"math/rand"
"time"
)
func GetRandomString(l int) string {
str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
bytes := []byte(str)
result := []byte{}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < l; i++ {
result = append(result, bytes[r.Intn(len(bytes))])
}
return string(result)
}
func main() {
r1 := GetRandomString(8)
fmt.Println(r1)
r2 := GetRandomString(4)
r3 := GetRandomString(4)
r4 := GetRandomString(4)
r5 := GetRandomString(12)
fmt.Println(r1+"-"+r2+"-"+r3+"-"+r4+"-"+r5)
}
输出:
有关请求参数中的Timestamp
名称 | 类型 | 是否必需 | 说明 |
---|---|---|---|
Timestamp | String | 是 | 请求的时间戳。日期格式按照ISO 8601标准表示,并需要使用UTC时间,时区为:+0(请勿使用本地时间,如北京时间)。格式为YYYY-MM-DDThh:mm:ssZ。例如2019-04-03T06:15:03Z 为UTC时间2019年4月3日6点15分03秒。 |
参考:https://studygolang.com/articles/21743
package main
import (
"fmt"
"time"
)
func main() {
Timestamp := string(time.Now().Format("2006-01-02T15:04:05Z"))
fmt.Println(Timestamp)
}
输出:
——————有点问题,要求转化成UTC时间
参考:https://www.jianshu.com/p/cc3ffdaa0b8c
package main
import (
"fmt"
"reflect"
"time"
)
func main() {
Timestamp := time.Now()
//Timestamp = Timestamp.UTC()//.Format("2006-01-02T15:04:05Z")
fmt.Println(Timestamp)
fmt.Println("Timestamp 的数据类型是:",reflect.TypeOf(Timestamp))
Timestamp = Timestamp.UTC()
fmt.Println(Timestamp)
fmt.Println("Timestamp 的数据类型是:",reflect.TypeOf(Timestamp))
Timestamps := Timestamp.Format("2006-01-02T15:04:05Z")
fmt.Println(Timestamps)
fmt.Println("Timestamps 的数据类型是:",reflect.TypeOf(Timestamps))
}
输出:
在使用时可简化:
Timestamp := time.Now().UTC().Format("2006-01-02T15:04:05Z")
即可获得当下时刻都UTC时间并进行格式化,总体代码如下:
package main
import (
"fmt"
"reflect"
"time"
)
func main() {
Timestamp := time.Now().UTC().Format("2006-01-02T15:04:05Z")
fmt.Println(Timestamp)
fmt.Println("Timestamp 的数据类型是:",reflect.TypeOf(Timestamp))
}
输出:
好像再往后测试不了了,阿里和腾讯老是憋着让我实名认证,一个要绑定支付宝,一个要微信和QQ绑银行卡。我缓缓打出两个问号?绑银行卡是不可能的!绑支付宝也绝不可能,穷人总共就没多少生活费!
拓展:有关时间戳
package main
import (
"fmt"
"reflect"
"strconv"
"time"
)
func main() {
Timestamp := time.Now().Unix()
//Timestamp = Timestamp.Format("2006-01-02T15:04:05Z")
fmt.Println(Timestamp)
fmt.Println("Timestamp 的数据类型是:",reflect.TypeOf(Timestamp))
//十六进制
Timestamps := strconv.FormatInt(Timestamp,16)
fmt.Println(Timestamps)
fmt.Println("Timestamp 的数据类型是:",reflect.TypeOf(Timestamps))
//十进制
Timestamps = strconv.FormatInt(Timestamp,10)
fmt.Println(Timestamps)
fmt.Println("Timestamp 的数据类型是:",reflect.TypeOf(Timestamps))
//二进制
Timestamps = strconv.FormatInt(Timestamp,2)
fmt.Println(Timestamps)
fmt.Println("Timestamp 的数据类型是:",reflect.TypeOf(Timestamps))
}
输出:
D:\SOFTware\GoLand\GoProject\go_build_GoProject_.exe #gosetup
1615969823
Timestamp 的数据类型是: int64
6051be1f
Timestamp 的数据类型是: string
1615969823
Timestamp 的数据类型是: string
1100000010100011011111000011111
Timestamp 的数据类型是: string