9

到目前为止,我关注了这篇文章,它对我帮助很大,但是,我现在得到了一个“invalid_grant”。以下:https://developer.apple.com/documentation/signinwithapplerestapi/errorresponse我了解我遇到问题是因为授权授予或刷新令牌无效。

尽管我进行了搜索和尝试(和重试),但我仍然卡住了,我不知道它来自哪里。我使用了https://developer.apple.com/documentation/authenticationservices/adding_the_sign_in_with_apple_flow_to_your_app提供的应用程序

现在我从上面的应用程序中获取了我的令牌,我尝试从 C# 后端验证它,但我得到了 400 响应代码invalid_grant

我可以从帖子中注意到的唯一区别是,与下图相比,我没有任何[Verify]按钮(选项)或[Download]门户中的按钮。我不知道这是否相关,但我试图提供尽可能多的细节:

在此处输入图像描述


希望有人可以提供帮助,感谢您的帮助:)如果需要,请随时询问更多详细信息

最大限度

4

2 回答 2

3

我也遇到了同样的问题,我在这里找到了解决方案:

https://forums.developer.apple.com/thread/118135

如链接中所述,当您使用从应用程序获得的代码时,您应该使用应用程序 ID 而不是服务 ID。

于 2020-05-15T12:34:58.600 回答
1

您能分享一下您是如何尝试创建 JWT 的吗?我已经尝试了一些我知道的东西(这也不起作用,如果我找到真正的解决方案,我会更新)

const string iss = "7#######G"; //  team ID 
            const string aud = "https://appleid.apple.com";
            const string sub = "com.######.weblogin"; // serviceid
            const string privateKey = "MIGTA#######"; // contents of .p8 file     

            var d = DateTime.UtcNow.AddDays(-5);

            var cngKey = CngKey.Import(
              Convert.FromBase64String(privateKey),
              CngKeyBlobFormat.Pkcs8PrivateBlob);


            var handler = new JwtSecurityTokenHandler();


            var securityKey = new ECDsaSecurityKey(new ECDsaCng(cngKey) { KeySize = 256 , HashAlgorithm = CngAlgorithm.ECDsaP256});


            securityKey.KeyId = "G#######W";
            var signingCredentials = new SigningCredentials(securityKey, SecurityAlgorithms.EcdsaSha256);

            return  handler.CreateEncodedJwt(iss, aud, new ClaimsIdentity(new List<Claim> { new Claim("sub", sub) }),d, expires: d.AddMonths(3),d, signingCredentials: signingCredentials);

标头在 jwt 中看起来像这样,从我收集的内容来看,可能有许多实施中不存在的“典型”标头,也许我应该摆脱它:

{
  "alg": "ES256",
  "kid": "G#######W",
  "typ": "JWT"
}

身体:

{
  "sub": "com.#####.weblogin",
  "nbf": 1583088895,
  "exp": 1591037695,
  "iat": 1583088895,
  "iss": "7######G",//teamid
  "aud": "https://appleid.apple.com"
}
于 2020-03-08T13:45:07.640 回答