如何在Swift中使用MSAL API对一个驱动器进行身份验证?

时间:2018-11-08 06:17:47

标签: ios swift onedrive msal

我正在使用MSAL Pod库对Microsoft帐户进行身份验证。之后,我想将特定的OneDrive帐户文件访问到我的ios应用中。

ServiceConstant类

struct ServiceConstants {
    static let kBaseURL = "https://graph.microsoft.com/v1.0"
    static let kDrive = "/users/fcd9cff6658ad065/drive"
}

ViewController.swift

override func viewDidLoad() {
        super.viewDidLoad()        

        getAllDocumentsFromOneDrive()
    }

func getAllDocumentsFromOneDrive() {
        // Setup OneDrive Linking
        let urlString = ServiceConstants.kBaseURL + ServiceConstants.kDrive
        print("URL : \(urlString)")
        print("Token : \(accessTokenMSAL)")

        let url = URL(string: urlString)
        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.httpMethod = "GET"
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        request.setValue("Bearer \(accessTokenMSAL)", forHTTPHeaderField: "Authorization")

        URLSession.shared.dataTask(with: request) { (data, response, error) in
            if let err = error {
                print("Error : \(err.localizedDescription)")
                return
            }

            guard let result = try? JSONSerialization.jsonObject(with: data!, options: []) else {
                print("Couldn't deserialize result JSON")
                return
            }

            print(response)
            print("Result from Graph: \(result))")

        }.resume()
    }

输出:这是访问特定用户onedrive文件时的输出。

Result from Graph: {
    error =     {
        code = unauthenticated;
        innerError =         {
            date = "2018-11-08T05:15:10";
            "request-id" = "b5372309-dc14-4d66-a274-9ca94fcbf04f";
        };
        message = "Must be authenticated to use '/drive' syntax";
    };
})

0 个答案:

没有答案