如何获取Outlook日历事件

时间:2018-08-24 06:23:49

标签: swift4 msal outlook-calendar

这就是我获取身份验证和访问令牌的方式。

 import MSAL

    class ViewController: UIViewController, UITextFieldDelegate, URLSessionDelegate {

        let kClientID = "my-Client-Id"
        let kAuthority = "https://login.microsoftonline.com/common/v2.0"

        let kGraphURI = "https://graph.microsoft.com/v1.0/me/"
        let kScopes: [String] = ["https://graph.microsoft.com/user.read"]

        var accessToken = string()
        var applicationContext = MSALPublicClientApplication.init()

        @IBOutlet weak var loggingText: UITextView!
        @IBOutlet weak var signoutButton: UIButton!

        // This button will  invoke the call to the Microsoft Graph API. It uses the
        // built in Swift libraries to create a connection.

        @IBAction func callGraphButton(_ sender: UIButton) {

            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 and interactionRequired 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.kScopes, user: applicationContext.users().first) { (result, error) in

                        if error == nil {
                            self.accessToken = (result?.accessToken)!
                            self.loggingText.text = "Refreshing token sliently)"
                            self.loggingText.text = "Refreshed Access token is \(self.accessToken)"

                            self.singoutButton.isEnabled = true;
                            self.getContentWithToken()

                        } else {

                            self.loggingText.text = "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 possibilities

                if error.code == MSALErrorCode.iteractionRequire.rawValue {

                    self.applicationContext.acquireToken(forScopes: self.kScopes) { (result, error) in
                        if error == nil {
                            sel.accessToken = (result?.accessToken)!
                            self.loggingText.text = "Access token is \(self.accessToken)"
                            self.signoutButton.isEnabled = true;
                            self.getConetnWithToken()
                        } else {
                            self.loggingtext.text = "Could not acquire token: \(error ?? "No error information" as! Error)"
                        }
                    }
                }
            } catch {

                // This is the catch all error.

                self.loggingText.text = "Unable to acquire token. Got error: \(error)"
            }
        }

        override func viewDidLoad() {
            super.viewDidLoad()


            do {
                // Inititalize a MSALPublicClinetApplication with a given clientID and authority
                self.applicationContext = try MSALPublicClinetApplication.init(clientId: kClientID, authority: kAuthority)
            } catch {
                self.loggingText.text = "Unable to create Appliocation context. Error \(error)"
            }
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }

        override func viewWillAppear(_ animated: Bool) {

            if self.accessToken.isEmpty {
                singoutButton.isEnabled = false;
            }
        }

        func getContentWithToken() {

            let sessionConfig = URLSessionConfiguration.default

            // 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 \(self.accessToken)", 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 {

                        self.loggingText.text = result.debugDescription
                    }
                }.resume()
        }

如何从Outlook日历获取日历事件。我试过了,但是没有声明范围。 我正在使用与getContentWithToken()相同的方法来获取日历事件。 当我在登录请求中定义范围时,它将引发错误-声明了一个或多个范围,登录成功后必须声明所请求的范围。 谢谢。

0 个答案:

没有答案