Auth0 得到 meta_data 未通过身份验证
Auth0 get meta_data not authenticated
我正在使用 Auth0 对使用我的 ios 应用程序的用户进行身份验证,我一直在关注有关获取用户元数据的文档,但是当我尝试时它不起作用。按照文档,这是我写的方法:
AuthenticationViewController
@IBAction func showLogin(_ sender: UIButton) {
guard let clientInfo = plistValues(bundle: Bundle.main) else { return }
Auth0
.webAuth()
.scope("openid profile read:current_user")
.audience("https://" + clientInfo.domain + "/userinfo")
.start {
switch [=11=] {
case .failure(let error):
Loaf("Something went wrong, please try again!", state: .error, location: .bottom, presentingDirection: .vertical, dismissingDirection: .vertical, sender: self).show()
print("---WEBAUTH---", error)
case .success(let credentials):
if(!SessionManager.shared.store(credentials: credentials)) {
print("Failed to store credentials")
} else {
SessionManager.shared.retrieveProfile { error in
if let error = error {
print("---RETRIEVE PROFILE---", error)
} else {
SessionManager.shared.getMetaData { (error) in
if let error = error {
print("---GETMETADATA---", error)
} else {
DispatchQueue.main.async {
self.performSegue(withIdentifier: "authenticate", sender: self)
}
}
}
}
}
}
}
}
}
会话管理器
class SessionManager {
static let shared = SessionManager()
private let authentication = Auth0.authentication()
let credentialsManager: CredentialsManager!
var profile: UserInfo?
var credentials: Credentials?
var patchMode: Bool = false
private init () {
self.credentialsManager = CredentialsManager(authentication: Auth0.authentication())
_ = self.authentication.logging(enabled: true)
}
func retrieveProfile(_ callback: @escaping (Error?) -> ()) {
guard let accessToken = self.credentials?.accessToken
else { return callback(CredentialsManagerError.noCredentials) }
self.authentication
.userInfo(withAccessToken: accessToken)
.start { result in
switch(result) {
case .success(let profile):
self.profile = profile
callback(nil)
case .failure(let error):
callback(error)
}
}
}
func getMetaData(_ callback: @escaping (Error?) -> ()) {
guard let accessToken = self.credentials?.accessToken
else { return callback(CredentialsManagerError.noCredentials) }
Auth0
.users(token: accessToken)
.get(profile!.sub, fields: ["user_metadata"], include: true)
.start { (result) in
switch result {
case .success(let user):
print(user)
callback(nil)
case .failure(let error):
callback(error)
}
}
}
func store(credentials: Credentials) -> Bool {
self.credentials = credentials
// Store credentials in KeyChain
return self.credentialsManager.store(credentials: credentials)
}
}
// also contains standard plist func written by Auth0
这是我遇到的错误:
---GETMETADATA--- Failed with unknown error ["error": Bad Request, "statusCode": 400, "message": Bad HTTP authentication header format, "errorCode": Bearer]
我知道通常您会使用 Bearer + accessToken 进行身份验证,但我没有看到他们在文档或示例项目中使用它。
我发现了问题所在。就我而言,我需要使用 /api/v2
端点而不是 /userInfo
端点:.audience("https://" + clientInfo.domain + "/api/v2/")
我希望这对未来的搜索者有所帮助!
我正在使用 Auth0 对使用我的 ios 应用程序的用户进行身份验证,我一直在关注有关获取用户元数据的文档,但是当我尝试时它不起作用。按照文档,这是我写的方法:
AuthenticationViewController
@IBAction func showLogin(_ sender: UIButton) {
guard let clientInfo = plistValues(bundle: Bundle.main) else { return }
Auth0
.webAuth()
.scope("openid profile read:current_user")
.audience("https://" + clientInfo.domain + "/userinfo")
.start {
switch [=11=] {
case .failure(let error):
Loaf("Something went wrong, please try again!", state: .error, location: .bottom, presentingDirection: .vertical, dismissingDirection: .vertical, sender: self).show()
print("---WEBAUTH---", error)
case .success(let credentials):
if(!SessionManager.shared.store(credentials: credentials)) {
print("Failed to store credentials")
} else {
SessionManager.shared.retrieveProfile { error in
if let error = error {
print("---RETRIEVE PROFILE---", error)
} else {
SessionManager.shared.getMetaData { (error) in
if let error = error {
print("---GETMETADATA---", error)
} else {
DispatchQueue.main.async {
self.performSegue(withIdentifier: "authenticate", sender: self)
}
}
}
}
}
}
}
}
}
会话管理器
class SessionManager {
static let shared = SessionManager()
private let authentication = Auth0.authentication()
let credentialsManager: CredentialsManager!
var profile: UserInfo?
var credentials: Credentials?
var patchMode: Bool = false
private init () {
self.credentialsManager = CredentialsManager(authentication: Auth0.authentication())
_ = self.authentication.logging(enabled: true)
}
func retrieveProfile(_ callback: @escaping (Error?) -> ()) {
guard let accessToken = self.credentials?.accessToken
else { return callback(CredentialsManagerError.noCredentials) }
self.authentication
.userInfo(withAccessToken: accessToken)
.start { result in
switch(result) {
case .success(let profile):
self.profile = profile
callback(nil)
case .failure(let error):
callback(error)
}
}
}
func getMetaData(_ callback: @escaping (Error?) -> ()) {
guard let accessToken = self.credentials?.accessToken
else { return callback(CredentialsManagerError.noCredentials) }
Auth0
.users(token: accessToken)
.get(profile!.sub, fields: ["user_metadata"], include: true)
.start { (result) in
switch result {
case .success(let user):
print(user)
callback(nil)
case .failure(let error):
callback(error)
}
}
}
func store(credentials: Credentials) -> Bool {
self.credentials = credentials
// Store credentials in KeyChain
return self.credentialsManager.store(credentials: credentials)
}
}
// also contains standard plist func written by Auth0
这是我遇到的错误:
---GETMETADATA--- Failed with unknown error ["error": Bad Request, "statusCode": 400, "message": Bad HTTP authentication header format, "errorCode": Bearer]
我知道通常您会使用 Bearer + accessToken 进行身份验证,但我没有看到他们在文档或示例项目中使用它。
我发现了问题所在。就我而言,我需要使用 /api/v2
端点而不是 /userInfo
端点:.audience("https://" + clientInfo.domain + "/api/v2/")
我希望这对未来的搜索者有所帮助!