要为苹果appstoreconnectapi生成jwt令牌,请使用此golang代码片段

问题描述 投票:0回答:1

为appstoreconnectapi生成jwt令牌的函数?深入研究该过程后得到了这段代码。

App Store Connect API 需要 JSON Web 令牌 (JWT) 来授权您向 API 发出的每个请求。 因此,此代码有助于生成 jwt 令牌

go firebase-authentication jwt apple-push-notifications app-store-connect-api
1个回答
0
投票

func GetJwtToken() { 标头 := 地图[字符串]接口{}{ "alg": "ES256", // 算法 "kid": os.Getenv("KEYID"), // 密钥 ID(替换为您的实际密钥 ID) “类型”:“智威汤逊”, }

// Define custom payload
payload := jwt.MapClaims{
    "iss": os.Getenv("ISSUER_ID"),      // Issuer (replace with your actual Team ID)
    "iat": time.Now().Unix(),           // Issued At Time
    "exp": time.Now().Add(1800).Unix(), // Expiration Time (within 30 minutes)
    "aud": "appstoreconnect-v1",        // Audience
    "bid": os.Getenv("BUNDLE_ID"),      // Subject (replace with your actual Client ID)
}

    // Read the private key from file
    privateKeyBytes, err :=
    os.ReadFile("privateKeys/SubscriptionKey_ISSUER_ID.p8")

if err != nil {
    log.Fatalf("Error reading private token file: %v", err)
}

block, _ := pem.Decode(privateKeyBytes)
if block == nil {
    fmt.Println("Error decoding private key PEM")
    return
}

privateKey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
    fmt.Println("Error parsing private key:", err)
    return
}

ecKey, ok := privateKey.(*ecdsa.PrivateKey)
if !ok {
    fmt.Println("Error casting private key to ECDSA")
    return
}

// Sign JWT token
token := jwt.New(jwt.SigningMethodES256)
token.Header = header
token.Claims = payload

tokenString, err := token.SignedString(ecKey)
if err != nil {
    log.Fatalf("Error signing token: %v", err)
    return
}
fmt.Println("JWT Token:", tokenString)

}

© www.soinside.com 2019 - 2024. All rights reserved.