1

How do I convert this oAuth curl request to swift syntax.

curl -X POST -vu clientapp:123456 http://localhost:8080/oauth/token -H "Accept: application/json" -d "password=spring&username=roy&grant_type=password&scope=read%20write&client_secret=123456&client_id=clientapp"

1 Answer 1

3

I was able to achieve this by encoding user credential in base 64 string.

    let parameters = ["password" : "spring", "username" : "roy", "grant_type" : "password",
        "scope" : "read write", "client_secret" : "123456", "client_id" : "clientapp"]

    let str = "clientapp:123456"
    let utf8str = str.dataUsingEncoding(NSUTF8StringEncoding)
    let basic_auth_token = utf8str?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))


    let headers = ["Accept" : "application/json", "Authorization" :"Basic "+basic_auth_token!]
    Alamofire.request(.POST, "http://localhost:8080/oauth/token", parameters: parameters, encoding:.URL, headers: headers)
        .responseJSON { response  in
            print("Response String: \(response.result.value)")                
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Great tip on encoding user credential in base 64 string. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.