api - ItunesConnectApi JWT

标签 api go jwt app-store-connect jwt-go

我正在尝试使用 App Store Connect API。 根据docs ,首先我尝试生成 JWT token 。 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 - 来自 https://appstoreconnect.apple.com/access/api 的 p8 私钥

似乎 jwt lib 无法在符号键上使用此 p8,所以我收到错误: key 类型无效

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

UPD: After this suggestin我已将代码更新为:

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 token ,但当我尝试使用它时,从 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"
        }]
}

最佳答案

这个问题似乎来自 issue来自 jwt-go 库。

作者说:

The library will not automatically parse your key from a byte slice. For ES256, I believe you need to provide a key of type *ecdsa.PrivateKey. As of v4, this will also accept a crypto.Signer so long as it produces a valid signature for that signing method.

你可以尝试this code example .

关于api - ItunesConnectApi JWT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58257445/

相关文章:

json - 如何在带有 json 键的 GO 中使用 switch?

python - 使用 API 进行排序或算法?

json - 如何在 Golang 中解析嵌套 JSON 对象中的嵌套数组?

go - 如何在 *tls.Conn 上设置 SetKeepAlivePeriod

c# - ASP.Net Core JWT 登录未设置 HttpContext.User.Claims

cookies - 如何在内存中存储 JWT 不容易受到 XSS 的攻击?

iphone - 在 iPhone 上通过 Web 服务使用 CMS 内容

python - 在 Raspi 上使用带有 Python 的 C 库

javascript - Node Js 和 Ebay api

google-cloud-platform - 如何为使用 gRPC 运行的 google cloud 设置 jwcrypto token 发行者?