接入企业需要联系北极星客服申请OpenAPI的key和secret,之后通过JWT加密算法生成AccessToken,以下为详细生成过程:
Golang版 #
package main
import (
"fmt"
"github.com/dgrijalva/jwt-go"
"time"
)
func main(){
key := "org key"
secret := "org secret"
token := jwt.New(jwt.SigningMethodHS256)
claims := make(jwt.MapClaims)
claims["exp"] = time.Now().Add(time.Hour * time.Duration(1)).Unix()
claims["iat"] = time.Now().Unix()
claims["_key"] = key
token.Claims = claims
accessToken, _ := token.SignedString([]byte(secret))
fmt.Println(accessToken)
}
Java版 #
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
public static final Long EXPIRES_IN = 1 * 3600 * 1000L;
public static final String TOKEN_APPID = "_key";
public static String genAppToken(String appId, String appSecret) {
if (StringUtils.isEmpty(appId) || StringUtils.isEmpty(appSecret)) {
return null;
}
Algorithm algorithm = Algorithm.HMAC256(appSecret);
long timestamp = System.currentTimeMillis();
Date issuedAt = new Date(timestamp);
Date expiresAt = new Date(timestamp + EXPIRES_IN);
return JWT.create()
.withClaim(TOKEN_APPID, appId)
.withIssuedAt(issuedAt)
.withExpiresAt(expiresAt)
.sign(algorithm);
}
API #
OpenAPI所有接口需要在请求头中携带认证参数:
Name | Value | 说明 |
Authorization | Bearer <AccessToken> | AccessToken |
X-Tenant-Id | 1004 | 企业 ID |