登录
首页 >  Golang >  Go问答

ItunesConnectApi JWT

来源:stackoverflow

时间:2024-04-21 22:45:41 230浏览 收藏

“纵有疾风来,人生不言弃”,这句话送给正在学习Golang的朋友们,也希望在阅读本文《ItunesConnectApi JWT》后,能够真的帮助到大家。我也会在后续的文章中,陆续更新Golang相关的技术文章,有好的建议欢迎大家在评论留言,非常感谢!

问题内容

我正在尝试使用 app store connect 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 - 来自 https://appstoreconnect.apple.com/access/api 的 p8 私钥

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

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

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"
        }]
}

解决方案


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

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

作者说:

您可以尝试拨打this code example

以上就是《ItunesConnectApi JWT》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>