代码之家  ›  专栏  ›  技术社区  ›  teja_D

正在获取Outlook日历事件

  •  0
  • teja_D  · 技术社区  · 8 年前

    我正在尝试使用MSAL获取Outlook日历事件。单击“同步”按钮后,应用程序重定向到“Outlook登录”页并成功登录。 当我试图获取事件时,抛出错误。 这是我到目前为止所做的尝试-

    //获取Outlook用户帐户的访问权限

        let kClientID = "my_Client_ID"
        let kAuthority = "https://login.microsoftonline.com/common/v2.0"
    
        let kGraphURI = "https://graph.microsoft.com/v1.0/me/"
        let kCalScopes: [String] = ["https://graph.microsoft.com/calendars.readwrite"]
    
        var accessToken = String()
        var applicationContext = MSALPublicClientApplication.init()
        let kCalEventsURI = "https://outlook.office.com/api/v2.0/me/events"   //?$select=Subject,Organizer,Start,End"
    
    
     override func viewDidLoad() {
            super.viewDidLoad()
            do {
                // Initialize a MSALPublicClientApplication with a given clientID and authority
                self.applicationContext = try MSALPublicClientApplication.init(clientId: kClientID, authority: kAuthority)
            } catch {
                print("Unable to create Application Context. Error: \(error)")
            }
    
    
        }
    

    这是签到功能

    func outLookSignIn() {
            do {
    
                // We check to see if we have a current logged in user. If we don't, then we need to sign someone in.
                // We throw an interaction required so that we trigger the interactive sign in .
                if try self.applicationContext.users().isEmpty {
                    throw NSError.init(domain: "MSALErrorDomain", code: MSALErrorCode.interactionRequired.rawValue, userInfo: nil)
                } else {
    
                    // Acquire a token for an existing user silently
    
                    try self.applicationContext.acquireTokenSilent(forScopes: self.kCalScopes, user: applicationContext.users().first) { (result, error) in
                        if error == nil {
    
    
                            self.accessToken = (result?.accessToken)!
                            print("Refreshing token silently")
                            print("Refreshed Access token is \(self.accessToken)")
                            UserDefaults.standard.set(self.accessToken, forKey: "outlook_access_token")
                            self.showAlert(title: "Acoount Sync Succesfully!", message:"")
                            self.menuItems[9] = "Remove Synced Account"
                        } else {
                            print("Could not acquire token silently: \(error ?? "No error information" as! Error)")
    
                        }
                    }
                }
            }  catch let error as NSError {
    
                // interactionRequired means we need to ask the user to sign-in. This usually happens
                // when the user's Refresh Token is expired or if the user has changed their password
                // among other possible reasons.
                if error.code == MSALErrorCode.interaction
    Required.rawValue {
    
                    self.applicationContext.acquireToken(forScopes: self.kCalScopes) { (result, error) in
                        if error == nil {
                            self.accessToken = (result?.accessToken)!
                            print("Access token is \(self.accessToken)")
                            self.showAlert(title: "Acoount Sync Succesfully!", message:"")
                            self.menuItems[9] = "Remove Synced Account"
                            UserDefaults.standard.set(self.accessToken, forKey: "outlook_access_token")
                            self.tokenDeletegate?.userToken(token: self.accessToken)
                        } else  {
                            print("Could not acquire token: \(error ?? "No error information" as! Error)")
                        }
                    }
    
                }
    
            } catch {
    
                // This is the catch-all error.
    
                print("Unable to acquire token. Got error: \(error)")
    
            }
        }
    

    这是获取日历事件的功能

      func getContentOutlookWithToken() {
    
            let sessionConfig = URLSessionConfiguration.default
            // Specify the Graph API endpoint
            let url = URL(string: kCalEventsURI)
            var request = URLRequest(url: url!)
    
            if let token = UserDefaults.standard.value(forKey: "outlook_access_token") as? String {
                self.outlookUserToken = token
            }
    
    
            // 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 \(self.outlookUserToken)", forHTTPHeaderField: "Authorization")
            let urlSession = URLSession(configuration: sessionConfig, delegate: self, delegateQueue: OperationQueue.main)
    
            urlSession.dataTask(with: request) { data, response, error in
                let result = try? JSONSerialization.jsonObject(with: data!, options: [])
                if result != nil {
    
                    print(result.debugDescription)
                }
                }.resume()
        }
    

    我犯的错误-

    CredStore - performQuery - Error copying matching creds.  Error=-25300, query={
        class = inet;
        "m_Limit" = "m_LimitAll";
        ptcl = htps;
        "r_Attributes" = 1;
        sdmn = "";
        srvr = "outlook.office.com";
        sync = syna;
    }
    

    请帮助解决这个问题。

    谢谢。

    1 回复  |  直到 8 年前
        1
  •  0
  •   teja_D    8 年前

    哦!!我解决了,我不知道为什么会这样。

    但是在删除了迦太基文件并重新安装了迦太基之后,错误就消失了。