Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sending accessToken to server using swift ios

I am trying to use facebook ios login sdk. I get the access Token using below code. Now i want to send this token to a server so that server can validate this token by making a call to facebook server.

I am using the below code to get the accessToken , i am using swift in ios 8.

var accessToken = FBSession.activeSession().accessTokenData

When i am trying to send this token to server getting an error saying that type of accessToken is not convertible to NSString.

Please help me where i am wrong.

like image 771
Rajesh Singh Avatar asked Feb 11 '23 22:02

Rajesh Singh


1 Answers

First, make sure that you have an open session. I use this approach in my AppDelegate:

FBSession.openActiveSessionWithAllowLoginUI(false)

Second, you can get accessToken as a string from accessTokenData:

var myToken = FBSession.activeSession().accessTokenData.accessToken

From there, you can send it to your server however you want. I tried a couple request wrappers until I settled on Net. Getting your token to your server is pretty easy if you have a library like Net so that you don't have to handle the low level network request interfaces:

func doLogin() -> Void {

    let net = Net(baseUrlString: "http://myhost.com/")

    let url = "auth/facebook_access_token"

    let params = ["access_token": myToken]

    net.GET(url, params: params, successHandler: { responseData in
        let result = responseData.json(error: nil)
        // Do something with whatever you got back
        NSLog("result \(result)")
    }, failureHandler: { error in
        NSLog("Error")
    })
}

Good luck! I hope I was able to help!

like image 54
Jonathan Avatar answered Feb 16 '23 03:02

Jonathan