如何使用Oauth swift方法POST身份验证登录

时间:2017-03-20 02:58:16

标签: ios swift authentication oauth

我尝试构建登录身份验证而没有oauth工作,然后我构建其他项目只有oauth代码请求roken也工作,现在我想用oauth swift进行项目身份验证登录。我真的坚持了我必须做的事情。

这是我尝试的代码:

import UIKit
import OAuthSwift

class ViewController: UIViewController {

@IBOutlet weak var passwordTxt: UITextField!
@IBOutlet weak var phonenumberTxt: UITextField!

var oauthswift: OAuth1Swift?
let dataShared = UserDefaults.standard

let fileUrl = NSURL(string: "8villages://app/oauth-callback")

override func viewDidLoad() {
    super.viewDidLoad()
    requestToken()

}


@IBAction func doLogin(_ sender: Any) {


    let phonenumber = phonenumberTxt.text
    let password = passwordTxt.text

    if(phonenumber == "" || password == "")
    {
        return
    }

    login(phonenumber!, password!)

}

func login(_ number:String, _ pswrd:String) {
    let url = URL(string: "https://accounts.8villages.com/authentication")
    let session = URLSession.shared

    let request = NSMutableURLRequest(url: url!)
    request.httpMethod = "POST"

    let paramToSend = "identifier=" + number + "&password=" + pswrd
    request.httpBody = paramToSend.data(using: String.Encoding.utf8)

    let task = session.dataTask(with: request as URLRequest, completionHandler: {
        (data, response, error) in

        guard let _:Data = data else
        {
            return
        }

        let json:Any?
        do {

            json = try JSONSerialization.jsonObject(with: data!, options: [])
        }
        catch {
            return
        }

        guard let server_response = json as? NSDictionary else {
            return
        }

        if let data_block = server_response["data"] as? NSDictionary {
            if let session_data = data_block["session"] as? String {
                let preference = UserDefaults.standard
                preference.set(session_data, forKey: "session")

                DispatchQueue.main.async(
                    execute:self.LoginDone
                )//

            }
        }
    })

    task.resume()
}

func LoginDone() {
    self.performSegue(withIdentifier: "loginView", sender: self)
}



func requestToken() {

    print ("hei")
    var is8VillagesInstalled: Bool = {
        let appUrl = URL(string: "8villages://app")
        return UIApplication.shared.canOpenURL(appUrl!)
    }()

    self.oauthswift = OAuth1Swift.init(consumerKey: "**********",
                                       consumerSecret: "**********",
                                       requestTokenUrl: "https://oauth.8villages.com/tokens/request-token",
                                       authorizeUrl: "8villages://app/?oauth_token_secret=",
                                       accessTokenUrl: "https://accounts.8villages.com/tokens/access-token")

    self.oauthswift?.authorizeURLHandler = OAuthSwiftOpenURLExternally.sharedInstance
    self.oauthswift?.allowMissingOAuthVerifier = true

    self.oauthswift?.postOAuthRequestToken(
        callbackURL: fileUrl as! URL,
        success: { credentials, response, parameters in
            print("credentials", credentials)
            print("response =", response!)
            print("parameters =", parameters)

            // request ke api login


    }, failure: { error in
        print("the error is", error.description, error.errorCode)
    });


}


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


 }

所以,我不知道如何使用oauth进行身份验证登录。如何使它们发挥作用。

0 个答案:

没有答案