使用Microsoft进行SwiftUI Firebase身份验证
目前,我正在尝试使用SwiftUI与Microsoft一起使用Firebase身份验证构建应用程序。我的目标是登录Microsoft帐户,获取用户令牌,然后使用令牌调用Microsoft的Graph Api 下面是我获取Microsoft登录页的失败尝试使用Microsoft进行SwiftUI Firebase身份验证,swift,swiftui,Swift,Swiftui,目前,我正在尝试使用SwiftUI与Microsoft一起使用Firebase身份验证构建应用程序。我的目标是登录Microsoft帐户,获取用户令牌,然后使用令牌调用Microsoft的Graph Api 下面是我获取Microsoft登录页的失败尝试 import Foundation import Firebase import AuthenticationServices import FirebaseAuth class SignInWithMicrosoftCoordinator
import Foundation
import Firebase
import AuthenticationServices
import FirebaseAuth
class SignInWithMicrosoftCoordinator {
private var onSignIn: (() -> Void)?
var microsoftProvider : OAuthProvider?
let kGraphURI = "https://graph.microsoft.com/v1.0/me/"
var provider = OAuthProvider(providerID: "microsoft.com")
init() {
provider.customParameters = [
"prompt": "consent",
"login_hint": "user@firstadd.onmicrosoft.com"
]
provider.customParameters = [
// Optional "tenant" parameter in case you are using an Azure AD
// tenant. eg. '8eaef023-2b34-4da1-9baa-8bc8c9d6a490' or
// 'contoso.onmicrosoft.com' or "common" for tenant-independent
// tokens. The default value is "common".
"tenant": "TENANT_ID"
]
provider.scopes = ["mail.read", "calendars.read", "Directory.ReadWrite.Al"]
}
func logIn() {
self.microsoftProvider?.getCredentialWith(_: nil) { credential, error in
if error != nil {
//handle error
}
if let credential = credential {
Auth.auth().signIn(with: credential) { (authResult, error) in
if error != nil {
}
guard let authResult = authResult else {
print("Couln't get graph auth result")
return
}
let microsoftCredential = authResult.credential as! OAuthCredential
let token = microsoftCredential.accessToken!
//Using token to call Microsoft Graph API
self.getGraphContentWithToken(accessToken: token)
}
}
}
}
func getGraphContentWithToken(accessToken: String) {
// Specify the Graph API endpoint
let url = URL(string: kGraphURI)
var request = URLRequest(url: url!)
// Set the Authorization header for the request. We use Bearer tokens, so we specify Bearer + the token we got from the result
request.setValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization")
URLSession.shared.dataTask(with: request) { data, response, error in
if let error = error {
print("Couldn't get graph result: \(error)")
return
}
guard let result = try? JSONSerialization.jsonObject(with: data!, options: []) else {
print("Couldn't deserialize result JSON")
return
}
print("Result from Graph: \(result))")
}.resume()
}
}
我目前使用的是SwiftUI,没有应用程序代理,我看到其他表单使用这种方法来弹出Microsoft网站。我想知道如何使用按钮的操作打开Microsoft网站Url以登录。谢谢
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
for urlContext in URLContexts {
let url = urlContext.url
Auth.auth().canHandle(url)
}
}
func场景(scene:UIScene,openURLContexts:Set){
对于URLContexts中的urlContext{
让url=urlContext.url
Auth.Auth().canHandle(url)
}
}