ItunesConnectApi JWT

ItunesConnectApi JWT,api,go,jwt,app-store-connect,jwt-go,Api,Go,Jwt,App Store Connect,Jwt Go,我正在尝试使用应用商店连接API。 首先,我尝试生成JWT令牌。 以下是golang的代码: package main import ( "fmt" "io/ioutil" "log" "time" "github.com/dgrijalva/jwt-go" ) var iss = "xxxxxxxxxxxxxxxxxxxxx" var kid = "xxxxx" func main()

我正在尝试使用应用商店连接API。 首先,我尝试生成JWT令牌。 以下是golang的代码:

    package main

    import (
        "fmt"
        "io/ioutil"
        "log"
        "time"
        "github.com/dgrijalva/jwt-go"
    )
var iss = "xxxxxxxxxxxxxxxxxxxxx"
var kid = "xxxxx"

func main() {

        bytes, err := ioutil.ReadFile("AuthKey.p8")
        if err!=nil {
            fmt.Println(err)
        }

        token := jwt.NewWithClaims(jwt.SigningMethodES256, jwt.MapClaims{
            "iss": iss,
            "exp": time.Now().Unix()+6000,
            "aud": "appstoreconnect-v1",
        })

        token.Header["kid"] = kid

        tokenString, err := token.SignedString(bytes)
        if err != nil {
            log.Fatal(err)
        }
        fmt.Println(tokenString)

    }
AuthKey.p8-来自的p8私钥

jwt lib似乎无法在符号键处使用此p8,因此我收到错误消息:
键的类型无效

也许有人已经有同样的问题了?或者用其他语言做例子

UPD: 我已将代码更新为:

func main() {

    bytes, err := ioutil.ReadFile("AuthKey.p8")
    if err!=nil {
        fmt.Println(err)
    }

    block, _ := pem.Decode(bytes)
    key, err := x509.ParsePKCS8PrivateKey(block.Bytes)
    if err != nil {
        log.Fatal(err)
    }

    token := jwt.NewWithClaims(jwt.SigningMethodES256, jwt.MapClaims{
        "iss": iss,
        "exp": time.Now().Unix()+6000,
        "aud": "appstoreconnect-v1",
    })

    token.Header["kid"] = kid

    tokenString, err := token.SignedString(key)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(tokenString)

}
获得JWT令牌,但当我尝试使用它时,从apple api获得了401

 {
        "errors": [{
                "status": "401",
                "code": "NOT_AUTHORIZED",
                "title": "Authentication credentials are missing or invalid.",
                "detail": "Provide a properly configured and signed bearer token, and make sure that it has not expired. Learn more about Generating Tokens for API Requests https://developer.apple.com/go/?id=api-generating-tokens"
        }]
}

这个问题似乎来自jwt go库

作者说,

库不会自动从字节片解析密钥。对于ES256,我认为您需要提供*ecdsa.PrivateKey类型的密钥。从v4开始,它还将接受crypto.Signer,只要它为该签名方法生成有效的签名


您可以尝试。

发现问题,将
的“exp”:time.Now().Unix()+6000,
替换为
“exp”:time.Now().Add(time.Minute*20).Unix(),