Ben Williams
Ben Williams

Reputation: 4695

Creating AccessToken from tokenString with Uber iOS SDK failing

I'm using custom authorisation with the Uber iOS SDK, and having trouble creating the AccessToken in my iOS code. This is the response I get from my server with what appears to be a valid access token:

{
"access_token":"token here",
"expires_in":2592000,
"token_type":"Bearer",
"scope":"all_trips request",
"refresh_token":"refresh token here",
"last_authenticated":0
}

I then pass this to the AccessToken initialiser, like so:

let jsonString = //response from server as above
let accessToken = AccessToken(tokenString: jsonString)

My access token is created (ie. non-nil), but none of the relevant property are populated.

accessToken //non-nil
accessToken.expirationDate //nil
accessToken.refreshToken //nil

Strangely, accessToken.tokenString contains the original jsonString from above.

Am I doing something wrong?

Edit Digging through the AccessToken.swift source file of the Uber project, I find this:

@objc public init(tokenString: String) {
        super.init()
        self.tokenString = tokenString
    }

It seems like it never actually creates the refreshToken etc.

Upvotes: 1

Views: 112

Answers (1)

Edward Jiang
Edward Jiang

Reputation: 2433

The tokenString is meant to be just the access token itself, as you observed. If you want to parse the JSON itself, I would suggest using the fact that the model conforms to the Decodable protocol, and pass in your JSON through that method.

let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .secondsSince1970
let accessToken = try? decoder.decode(AccessToken.self, from: jsonData)
// If you need to convert a string to data, use String.data(using: .utf8)!

Upvotes: 1

Related Questions