new to both Swift and Siesta... Trying to make a "password" grant type request. I used the code located here (the block at the very bottom). My code is:
var authToken: String??
var tokenCreationResource: Resource { return resource("oauth/v2/token") }
func refreshTokenOnAuthFailure(request: Request) -> Request {
return request.chained {
guard case .failure(let error) = $0.response, // Did request fail…
error.httpStatusCode == 401 else { // …because of expired token?
return .useThisResponse // If not, use the response we got.
}
return .passTo(
self.createAuthToken().chained { // If so, first request a new token, then:
if case .failure = $0.response { // If token request failed…
return .useThisResponse // …report that error.
} else {
//print($0.response)
return .passTo(request.repeated()) // We have a new token! Repeat the original request.
}
}
)
}
}
func userAuthData() -> [String: String] {
return [
"username": "username",
"password": "password",
"grant_type": "password",
"client_id": "abc1234567",
"client_secret": "1234567abc"
]
}
func createAuthToken() -> Request {
print("requestingToken")
return tokenCreationResource
.request(.post, urlEncoded: userAuthData())
.onSuccess {
self.authToken = $0.jsonDict["access_token"] as? String // Store the new token, then…
print($0.jsonDict) //*****SEE MY NOTE BELOW ABOUT THIS LINE
self.invalidateConfiguration() // …make future requests use it
}
}
The problem is that it doesn't seem to set the authToken variable... In troubleshooting the $0.jsonDict variable in the createAuthToken() function seems to be empty. The line noted prints [:]
If I change the print($0.jsonDict)
to print($0)
I see the full response including the "content" section which displays the results I would have expected to be in the jsonDict
If it matters, my server implementation is Symfony using FOSOauthServerBundle. All this works fine if I just manually do a request in the browser and like I said the "content" of the response shows my token, I just can't seem to access it via the .jsonDict["access_token"]
Had this exact same issue with the example code - you need to remove
from the Service constructor (or include .json).