我正在尝试使用 Vapor 4 实现 Sign In with Apple 我关注了 Vapor Docs 但是当我使用 api 并发送用户的令牌(即使作为请求标头或在正文中)时,它会返回以下错误
{
"error": true,
"reason": "Data corrupted at path ''. The given data was not valid JSON. Underlying error: Error Domain=NSCocoaErrorDomain Code=3840 \"Invalid value around line 1, column 0.\" UserInfo={NSDebugDescription=Invalid value around line 1, column 0., NSJSONSerializationErrorIndex=0}."
}
正如我提到的,我使用了两种不同的方法。 第一个,在正文中发送用户令牌:
func SignInWithApple(req: Request)async throws -> Token{
//Decode request body -> succeeded
let data = try req.content.decode(SignInWithAppleToken.self)
//The bundle id of the iOS app
guard let appIdentifier = Environment.get("ios_app_id") else{
throw Abort(.internalServerError)
}
//The error happens at this line
let siwaToken = try await req.jwt.apple.verify(data.token, applicationIdentifier: appIdentifier)
print(siwaToken)
//Rest of logic
}
第二个,在请求头(Bearer)中发送用户令牌
func routes(_ app: Application) throws {
app.jwt.apple.applicationIdentifier = "iOS_app_bundle_Id"
app.get("apple") { req async throws -> HTTPStatus in
let token = try await req.jwt.apple.verify()
print(token) // AppleIdentityToken
return .ok
}
}
这是我在邮递员中发送请求的方式: 1- 在请求正文中发送数据(对于方法 1):
有什么建议吗?
这很可能是因为您按原样发送
identityToken
,即 Data
,但您需要先将其转换为 String
,然后再将其发送到您的后端,如下所示:
func handleAppleSignIn(_ auth: ASAuthorization) {
guard let credential = auth.credential as? ASAuthorizationAppleIDCredential else {
print("Invalid Apple ID credential")
return
}
guard let idToken = credential.identityToken, let idTokenString = String(data: idToken, encoding: .utf8) else {
print("Unable to convert identityToken to String")
return
}
print(idTokenString)
// rest of the logic
}