Refresh Token uber swift returns nil - ios

I'm trying to authenticate with uber in my project, the path to go to the uber native app and then return to my application is ok. However, it is only returning the TokenString and the ExpirationDate and the refreshToken is returning as nil.
Here is my code
AuthorizationBaseViewController
class AuthorizationBaseViewController: UIViewController {
func delay(delay: Double, closure: ()->()) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(delay*Double(NSEC_PER_SEC))), dispatch_get_main_queue(), closure)
}
func showMessage(message: String) {
let alert = UIAlertController(title: nil, message: message, preferredStyle: UIAlertControllerStyle.Alert)
let okayAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)
alert.addAction(okayAction)
self.presentViewController(alert, animated: true, completion: nil)
}
func checkError(response: Response) {
// Unauthorized
if response.statusCode == 401 {
TokenManager.deleteToken()
dispatch_async(dispatch_get_main_queue(), {
self.reset()
})
}
}
func reset() {
}
// Mark: LoginButtonDelegate
func loginButton(button: LoginButton, didLogoutWithSuccess success: Bool) {
if success {
showMessage(NSLocalizedString("Integration with uber canceled.", comment: ""))
}
}
func loginButton(button: LoginButton, didCompleteLoginWithToken accessToken: AccessToken?, error: NSError?) {
if let _ = accessToken {
print(accessToken?.tokenString)
print(accessToken?.expirationDate)
print(accessToken?.refreshToken)
showMessage(NSLocalizedString("Uber user authenticated successfully.", comment: ""))
if let url = NSURL(string: "xxxxxxxxxxxxxx") {
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
let token = AccessToken?()
let jsonObject = ["token" : (token?.tokenString)!, "refresh_token" : (token?.refreshToken)!,"expires_in" : (token?.expirationDate)!, "user_id" : "uber_uuid" , "token_type" : "Bearer"] as Dictionary <String,AnyObject>
request.HTTPBody = try? NSJSONSerialization.dataWithJSONObject(jsonObject, options: [])
NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
guard
let data = data where
error == nil &&
(response as? NSHTTPURLResponse)?.statusCode == 200
else {
print((response as? NSHTTPURLResponse)?.statusCode ?? "no status code")
print(error?.localizedDescription ?? "no error description")
return
}
print(String(data: data, encoding: NSUTF8StringEncoding) ?? "no string from data")
}.resume()
}
showMessage((error?.localizedDescription)!)
} else if let error = error {
showMessage(error.localizedDescription)
} else {
showMessage("Error")
}
}
}
LoginViewController
class ImplicitGrantLoginViewController: AuthorizationBaseViewController, LoginButtonDelegate {
/// The LoginManager to use for login
let loginManager = LoginManager(loginType: .Native)
/// The RidesClient to use for endpoints
let ridesClient = RidesClient()
// The Uber button to use for UI
var uberLoginButton: LoginButton?
// The Uber Scopes
var uberScopes: [RidesScope]?
#IBOutlet weak var logoutBgView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
//UberScopes to get authentication
uberScopes = [.History, .Profile, .HistoryLite,.Places, .RideWidgets]
uberLoginButton = LoginButton(frame: CGRectZero,scopes:uberScopes! ,loginManager: loginManager)
// Uber Login Button Creation
let loginButton = LoginButton(frame: CGRectZero, scopes: uberScopes!, loginManager: loginManager)
loginButton.presentingViewController = self
loginButton.delegate = self
loginButton.frame = logoutBgView.bounds
loginButton.autoresizingMask =
[.FlexibleWidth, .FlexibleHeight]
logoutBgView.addSubview(loginButton)
}
Basically, if the refresh Token is returning to my application, a POST request is made along with the TokenString and the ExpirationDate
When authentication is done, before displaying the authorization view it appears the following error
MyApp[18136:615342] -canOpenURL: failed for URL: "uberauth://connect?third_party_app_name=MyApp &callback_uri_string=xxxxxx &client_id=xxxxxxxxxx &login_type=default&scope=history%20profile%20history_lite%20places%20ride_widgets&sdk=ios&sdk_version=0.6.0
Even with this error, the screen to authorize the scopes is displayed and when I hit Allow I see the return in the debug area however the app crashes due to the refreshtoken is nil and HTTP request not receiving it.
Error when App Crashes due to refreshtoken is nil
I already checked the plist file and fill according uber documentation/github repository's. Callback URI, LSApplicationQuerieScheme,Client ID,DisplayName it is correct.

So reading through your question I see a few issues. It looks like you are trying to do Native login via the Uber app, which should return you an access token and a refresh token. If you are not doing Native login, then you will not get a refresh token, and that would be why it is nil.
The error message you listed:
MyApp[18136:615342] -canOpenURL: failed for URL:
"uberauth://connect?third_party_app_name=MyApp
&callback_uri_string=xxxxxx &client_id=xxxxxxxxxx
&login_type=default&scope=history%20profile%20history_lite%20places%20ride_widgets&sdk=ios&sdk_version=0.6.0
Means that you have not added uberauth into the plist of your app under LSApplicationQueriesSchemes. I noticed that you referred to it as LSApplicationQuerieScheme which is misspelled. So that might be your problem there.
You say that the authorization code screen is still shown, which makes me think that it is falling back to Implicit Grant in a webview, which will not return a refresh token to you.
Additionally, in the code you provided where you are making your post request, you never use the access token you get back:
let token = AccessToken?()
let jsonObject = ["token" : (token?.tokenString)!,
"refresh_token" : (token?.refreshToken)!,
"expires_in" : (token?.expirationDate)!,
"user_id" : "uber_uuid" ,
"token_type" : "Bearer"] as Dictionary <String,AnyObject>
You initialize a new empty AccessToken and then force unwrap several optional values on that token (not the one you got back from the initial login). So those values are almost certainly nil, and the force unwrap is causing the crash

Related

How to debug URLSession POST Authentication Login Failure

I am trying to mimic an HTTP Form POST in iOS to login to a server.
When I use Safari or Chrome and submit the login form outside of my app, I can login without issue. I am using Safari and Chrome dev tools to record/review the "correct" request and response headers, cookies and body during the GET and POST required to login.
When I run my app in Xcode, I use debug print statements or Instruments to review the headers, cookies and body.
Is there a tool/method that will allow me to compare my app's GET and POST header and body vs. what a web browser does? I want an "apples to apples" comparison that will allow me to determine what I am doing wrong...
My code is below. The POST header returns status code = 419. The post body includes the text "Page Expired", which leads me to believe I am not handling tokens or cookies correctly.
Code overview:
I press a UI button to invoke login(). This does a GET of login
page, and saves the hidden _token form input from the response body.
Cookies are saved to cookieStorage.
I press a UI button to invoke loginPost(). This submits a form with
a bogus email and password. I format headers and body. I expect to
get an error indicating email is not registered. POST adds _token to
body. This body seems to match Chrome dev tools for urlencode
formatting. Status code 419 is returned..
Code
class LoginAPI {
public let avLogin = "https://someDomain.com/login"
// save response, data from last getHTMLPage() GET
fileprivate var lastGetResponse: HTTPURLResponse? = nil
fileprivate var lastGetData: String? = nil
// test POST with saved values
var loginToken = ""
var cookies: [HTTPCookie] = []
// MARK: Login
func login() async -> String {
// GET login page,
let loginGetHTML = await self.getHTMLPage(url: self.avLogin)
let loginToken = self.scrapeLoginToken(html: loginGetHTML)
let cookies = self.getCookiesFromResponse(response: self.lastGetResponse)
if let lastResponse = self.lastGetResponse,
let lastURL = lastResponse.url {
HTTPCookieStorage.shared.setCookies(cookies,
for: lastURL, mainDocumentURL: nil)
}
// allow testing of Login, then POST
self.loginToken = loginToken
self.cookies = cookies
// TO DO: add delay, then call loginPost(), and return Data as String
return ""
}
// MARK: POST Login form
func loginPost(url: String, loginToken: String, cookies: [HTTPCookie]) async {
guard let loginURL = URL(string: url) else {return}
let email = "fake123#gmail.com"
let password = "pass123"
var request = URLRequest(url: loginURL)
request.httpMethod = "POST"
request.url = loginURL
// header
request.httpShouldHandleCookies = true
// body
let loginInfo = [
("_token" , loginToken),
("email" , email),
("password", password)
]
let body = urlEncode(loginInfo)
request.httpBody = Data(body.utf8)
let session = URLSession.shared
session.configuration.httpCookieStorage = HTTPCookieStorage.shared
session.configuration.httpCookieAcceptPolicy = .always
session.configuration.httpShouldSetCookies = true
let task = session.dataTask(with: request) { (data, response, error) in
if let error = error {
print ("POST error: \(error)")
}
guard let response = response as? HTTPURLResponse else {
print("invalid POST response")
return
}
print("response")
let statusCode = response.statusCode
let headerFields = response.allHeaderFields
let cookies = headerFields["Set-Cookie"]
// let cookie = response.value(forKey: "Set-Cookie")
print(" status code = \(statusCode)")
print(" cookies = \(cookies.debugDescription)")
print(response)
if let mimeType = response.mimeType,
let data = data,
let page = String(data: data, encoding: .utf8) {
print("mimeType \(mimeType)")
print("page as UTF-8")
print(page)
}
}
task.resume()
}
// MARK: GET
public func getHTMLPage(url urlString: String) async -> String {
var statusCode = 0 // HTTP Response status code
// void prior cached response, data
self.lastGetResponse = nil
self.lastGetData = nil
guard let url = URL(string: urlString) else {
print("Error: Invalid URL: '\(urlString)'")
return ""
}
do {
let (data, response) = try await URLSession.shared.data(from: url)
if let httpResponse = response as? HTTPURLResponse {
statusCode = httpResponse.statusCode
self.lastGetResponse = httpResponse
print("GET response")
print(response)
} else {
print("Error: couldn't get HTTP Response")
return ""
}
guard statusCode == 200 else {
print("Error: Bad HTTP status code. code=\(statusCode)")
return ""
}
let page = String(decoding: data, as: UTF8.self)
self.lastGetData = page
return page
} catch {
print("Error: catch triggerred")
return ""
}
}
// MARK: Login Helper Functions
private func getCookiesFromResponse(response: HTTPURLResponse?) -> [HTTPCookie] {
guard let response = response,
let responseURL = response.url else {
return []
}
guard let responseHeaderFields = response.allHeaderFields as? [String : String] else {
return []
}
let cookies = HTTPCookie.cookies(
withResponseHeaderFields: responseHeaderFields,
for: responseURL)
return cookies
}
// MARK: Login token
public func scrapeLoginToken(html: String) -> String {
look for name="_token", value="40-char-string"
return <40-char-string
}
// MARK: Login urlEncode
public func urlEncode(_ params: [(String, String)]) -> String {
var paramArray: [String] = []
for param in params {
let (name, value) = param
let valueEnc = urlEncode(value)
paramArray.append("\(name)=\(valueEnc)")
}
let body = paramArray.joined(separator: "&")
return body
}
private func urlEncode(_ string: String) -> String {
let allowedCharacters = CharacterSet.alphanumerics
return string.addingPercentEncoding(
withAllowedCharacters: allowedCharacters) ?? ""
}
}
Any debug help or direction would be appreciated!
I was able to resolve this issue with Proxyman, a Mac app that places itself between your mac and the internet, and saves all activity from apps, like Safari and Chrome, as well as apps being developed in Xcode.
Proxyman allowed me to make a simple "apples to apples" comparison, showing me all internet activity made by a browser, my app, or any other app running on my mac, in a common easy to read format.
I am not a web developer, so I didn't realize how easy it would be to debug URLSession headers, cookies, responses, etc. using a proxy server app.
My real problem: I had a simple typo in the web form I was sending to the server, so the server wasn't receiving all necessary form fields.
Days of debug looking into caches, cookies, headers, url encoding, responses, etc. I just didn't see the typo!

AddCardViewController not going away after completion

I'm pretty new to iOS Programming, and I'm stuck on this little portion here.
I'm basically trying to use Stripe in my iOS application. The user inputs an amount they wish to pay, press NEXT and they input their card details. All of this is going well so far - I can get the AddCardViewController to show up, and input their card details.
The code makes it all the way to verifying the card token and accepting the payment; which is wonderful, but then the problem comes that the AddCardViewController doesn't go away afterward.
When I was trying it out on a demo, it worked perfectly fine, but on my app it doesn't work at all! The app just gets stuck there on the AddCardViewController. Hitting cancel does nothing, there's no display message, and submitting the card details again just double bills the user.
What am I doing wrong?
For further info, this is my code:
StripeClient.shared.sendPostRequest(with: token, amount: amount, description: description) { result in
switch result {
// 1
case .success:
completion(nil)
let alertController = UIAlertController(title: "Congrats",
message: "Your payment was successful!",
preferredStyle: .alert)
let alertAction = UIAlertAction(title: "OK", style: .default, handler: { _ in
self.navigationController?.popViewController(animated: true)
})
alertController.addAction(alertAction)
self.present(alertController, animated: true)
self.view.makeToast("Your payment was successful! We will be sending you a receipt to your email shortly.")
// 2
case .failure(let error):
completion(error)
}
}
I made my custom post request because I couldn't get the one offered by the demo to work, but I can say with 100% certainty that the code enters case .success successfully - I tested this already. But just in case, this is my .sendPostRequest method:
func sendPostRequest(with token: STPToken, amount: Double, description: String, completion: #escaping (Result) -> Void) {
//declare parameter as a dictionary which contains string as key and value combination. considering inputs are valid
let params: [String: Any] = [
"stripeToken": token.tokenId,
"amount": amount,
"currency": Constants.defaultCurrency,
"description": description
]
//create the url with URL
let url = URL(string: "<this-is-my-url.com>")! //change the url
//create the session object
let session = URLSession.shared
//now create the URLRequest object using the url object
var request = URLRequest(url: url)
request.httpMethod = "POST" //set http method as POST
do {
request.httpBody = try JSONSerialization.data(withJSONObject: params, options: .prettyPrinted) // pass dictionary to nsdata object and set it as request body
} catch let error {
print(error.localizedDescription)
}
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
//create dataTask using the session object to send data to the server
let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in
guard error == nil else {
return
}
guard let data = data else {
return
}
do {
//create json object from data
//print(data)
if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
//print(json)
// handle json...
//print(json["response"]!)
if let responseString = try json["response"] as? String {
if responseString == "SUCCESS" {
completion(Result.success)
} else {
completion(Result.failure(IntParsingError.overflow))
}
}
}
} catch let error {
print(error.localizedDescription)
}
})
task.resume()
}
I would really, really, REALLY appreciate the help!
1- You need
DispatchQueue.main.async {
completion(Result.success)
}
As session.dataTask(with: runs in background thread
2- For this to run
self.navigationController?.popViewController(animated: true)
verify the vc is embedded inside a navigation and you presented it with push not present func of the previous vc
Edit:
When you present do
self.dismiss(animated: true, completion: nil)

Filter string from JSON from asp.net web api using swift 3

I am new to web development and Swift
I created a web api based on ASP.NET and I connected my ios app so I can do GET, POST, PUT, DELETE.
When I send GET request with specific ID number
I get output in Xcode as following:
Data:
Optional("{\"Id\":1,\"UserName\":\"codeinflash\",\"UserPassword\":\"Wldnrodxxxx\",\"UserEmail\":\"codeinflash#gmail.com\",\"Rebate\":0.00,\"MemCom\":123.44}")
Here is function in Swift:
//GET /api/account/{id}
#IBAction func GetAccount(_ sender: Any) {
let _accountIdNumber = AccountIdNumber.text
if (_accountIdNumber!.isEmpty){
createAlert(title: "Warning", message: "Account ID # is required")
return
}
let restEndPoinst: String = "http://tresmorewebapi2.azurewebsites.net/api/account/" + _accountIdNumber!;
guard let url = URL(string: restEndPoinst) else {
print("Error creating URL")
return
}
var urlRequest = URLRequest(url: url)
urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
// api key need urlRequest.setValue(<#T##value: String?##String?#>, forHTTPHeaderField: "APIKey")
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
var userEmail = ""
var rebate = ""
var memcom = ""
let task = session.dataTask(with: urlRequest, completionHandler:
{
(data, response, error) in
print("Error:")
print(error)
print("response:")
print(response)
print("Data:")
print(String(data: data!, encoding: String.Encoding.utf8))
//////////////I think I need logic here to filter the data
userEmail = data.substring of find emailaddress
rebate = data.substring find users' rebate
memcom = same logic
then show alert window with his info and I will show his info on next page which is a Dashboard page view
})
task.resume()
}
Honestly I am not sure the Data is JSON data but the output in Xcode is in string.
My purpose is get uer's data and store in local variables that passes them to next view(Dashboard) in ios Swift.
Thank you!
Added
Here is the task getting data from web api in login fuction swift
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
var userEmail = ""
var rebate = ""
var memcom = ""
let task = session.dataTask(with: urlRequest, completionHandler:
{
(data: Data?, response: URLResponse?, error: Error?) in
print("Error:")
print(error)
print("response:")
print(response)
print("Data:")
print(String(data: data!, encoding: String.Encoding.utf8))
let json = try? JSONSerialization.jsonObject(with: data!) as! [String: AnyObject] ?? [:];
userEmail = json?["UserEmail"] as? String ?? ""
createAlert(title: "Got User Eamil Address!", message: userEmail)
})
task.resume()
But I get nothing in my alert view. The alert view working fine I tested.
Here is my createAlert fuction in Swift
func createAlert(title:String, message:String){
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (anction) in
alert.dismiss(animated: true, completion: nil)}))
self.present(alert, animated: true, completion: nil)
}
You have answer in comments already, but this is less "swifty" way, more readable for someone new to Swift (pay attention and try to understand what is going on and how you can make this more compact):
do {
guard let unwrappedData = data else{
//data is nil - handle case here
return //exit scope
}
guard let dictionary = try JSONSerialization.jsonObject(with: unwrappedData) as? [String:AnyObject] else {
//you cannot cast this json object to dictionary - handle case here
return //exit scope
}
userEmail = dictionary["UserEmail"]
DispatchQueue.main.async { [weak self] in
self?.createAlert(title: "Got User Eamil Address!", message: userEmail)
}
} catch _ {
//handle error
}

Saving username information to a server instead of NSDefaults (iOS)

I have a server for storing username and password data in my application. When testing the app I had everything save to the device locally using NSDefaults, but now that the app is close to being fully launched, I am trying to save them to the server instead, as it is safer that way for the user's information.
When I had it save to NSDefaults, it was easy and short work. Now however, I am trying to POST the data to the server and keep getting build errors. What do I need to change for this to work? Am I not fully understanding how POST and GET works? Thanks. Using Swift 2 as of right now, not my choice, I prefer 3, but my boss isn't letting us update it yet.
The current error is coming from the POST USER DATA TO SERVER section, where xcode claims that userNmeTxt cannot be converted into NSData. Thank you in advance.
EDIT: Error is on line 87: "Cannot convert value of type UITextField! to expected argument type NSData!"
import UIKit
class UserNameViewController: AuthorizationViewController {
#IBOutlet weak var userNameTxt: UITextField!
#IBOutlet weak var continueBtn: UIButton!
var userModel: ProfileModel!
//MARK: - SYSTEMS METHODS
override func viewDidLoad() {
super.viewDidLoad()
userNameTxt.delegate = self
userNameTxt.autocapitalizationType = .Sentences
setEnabledButton()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBarHidden = false
self.navigationItem.leftBarButtonItem = getBackButton()
self.title = ""
}
override func viewWillDisappear(animated: Bool) {
self.navigationController?.navigationBarHidden = true
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
continueBtn.layer.cornerRadius = 10
}
override func popToRoot(sender:UIBarButtonItem){
self.navigationController!.popViewControllerAnimated(true)
}
//MARK: - CHECK FOR AVALABILITY
func setEnabledButton(){
if userNameTxt.text == "" {
continueBtn.backgroundColor = UIColor.lightGrayColor()
} else {
continueBtn.backgroundColor = UIColor(colorLiteralRed: 63.0/255.0, green: 220.0/255.0, blue: 236.0/255.0, alpha: 1.0)
}
continueBtn.userInteractionEnabled = userNameTxt.text != ""
}
//MARK: - POST USER DATA TO SERVER
func postData(url: String, params: Dictionary<String, String>, completionHandler: (data: NSData?, response: NSURLResponse?, error: NSError?) -> ()) {
// Indicate download
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
let url = NSURL(string: "myPlaceholderURLgoesHere")!
// print("URL: \(url)")
let request = NSMutableURLRequest(URL: url)
let session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
// Verify downloading data is allowed
do {
request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(params, options: [])
} catch let error as NSError {
print("Error in request post: \(error)")
request.HTTPBody = nil
} catch {
print("Catch all error: \(error)")
}
// Post the data
let task = session.dataTaskWithRequest(request) { data, response, error in
completionHandler(data: userNameTxt, response: userModel, error: error)
// Stop download indication
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
// Stop download indication
}
task.resume()
}
//MARK: - SEGUE
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toPassword"{
let controller = segue.destinationViewController as! PasswordViewController
controller.userModel = userModel
}
}
//MARK: - IB ACTIONS
#IBAction func continuePressed(sender: AnyObject) {
userModel.userNickName = userNameTxt.text!
performSegueWithIdentifier("toPassword", sender: self)
}
}
extension UserNameViewController: UITextFieldDelegate{
func textFieldDidEndEditing(textField: UITextField) {
self.setEnabledButton()
}
}
There are a couple of things you need to change.
userNameTxt is not the username, it's the UITextField containing the username. The text you need is userNameTxt.text?
If the function is expecting Data, you have to convert your text to Data first
let task = session.dataTaskWithRequest(request) { data, response, error in
completionHandler(data: userNameTxt.text?.data(using: .utf8), response: userModel, error: error)
I assume you have to send the data to the server.
If you don't have too you can save the data in keychain access, see: SO: Keychain Access
In order to resolve the error please edit the question with the error message and line of code (if possible).
I would suggest that you use Alamofire for POST/GET (REST). To use Alamofire you need basic knowledge of Cocoapods. It's better in the long term.
NOTE: There can be two possible error outcomes when you make the request.
1) Incorrect data format or bug from your side
2) Server error due backend bug from server side.
The data can be sent from your device with POST where the data is in the BODY or HEADER of the request. Usually it is in the body (parameters in the alamofire methods).
Here is an example:
import Alamofire
...
// MARK:- Login Feature - Universal Met for login
internal static func loginWith(serverUrl: String, parameters: [String: AnyObject]?, headers: [String: String]?, notificationName: String, serviceType: LoginService)
{
Alamofire.request(.POST, serverUrl, parameters: parameters, headers: headers).responseJSON
{ (response) in
print("\n Login feature - \n")
print(" Login url - \(serverUrl)\n")
print(" Login parameters - \(parameters)\n")
print(" Login notificationName - \(notificationName)\n")
print(" Login response - \(response)\n")
EXCDataParserH.parseResponseFrom(ServiceType.Login(type: serviceType),
operation: nil,
data: response.data,
notification: notificationName)
}
}
Instead of writing the whole thing every time you make a server request, try to do as follows:
import Foundation
import SystemConfiguration
class HTTPHelper{
class func httpPostDataDic(postURL:NSURL,postString:NSString,completionHandler:#escaping (NSDictionary?, NSError?) -> Void ) -> URLSessionTask{
var responseResultData: NSDictionary = NSDictionary()
let request = NSMutableURLRequest(url:postURL as URL);
request.httpMethod = "POST";// Compose a query string
request.httpBody = postString.data(using: String.Encoding.utf8.rawValue);
print(request)
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if error != nil
{
print("error=\(error)")
completionHandler(nil, error as NSError?)
return
}
let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
if let responseString = responseString {
print("responseString = \(responseString)")
}
do {
let myJSON = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
responseResultData=myJSON!
completionHandler(responseResultData, nil)
} catch {
print(error)
}
}
task.resume()
return task
}
}
Now whenever you need to make a server POST request,in your ViewController class, do as follows:
//Requesting server
func requestServer() -> Void{
let postvariable = "Value"
let url = URL(string: "your url")!
let postString = "postkey=\(postvariable)"
HTTPHelper.httpPostDataDic(postURL: url as NSURL, postString: postString) {
(responseResult, error) -> Void in
DispatchQueue.main.async {
if error != nil{
print(error ?? "unknown")
}
else{
print(responseResult ?? "unknown result")
//Parse your response
self.parseResult(result: responseResult!);
}
}
}
}
May I ask you one thing that I didn't understand in your question.
How exactly would you save the login credentials in a server? I mean, if you save the login credentials in the server, how would you authenticate user access to these saved credentials?

How to generate a signed POST request for Tumblr using OAuth Swift

I am trying to get a sample Tumblr client up and running. I am using the OAuth Swift library to do my OAuth.
Right now my project has one button. This button checks to see if OAuth has been authorized or not. If it is, then it is set up to post some hard coded data to a test Tumblr account I set up.
I am getting this output on the console:
Top of authorizeWithCallbackURL
OAuth successfully authorized
Request error
Server Response:
{"meta":{"status":401,"msg":"Not Authorized"},"response":[]}
It appears that my token is accepted and an OAuth token is generated, but for some reason it is not being included in the POST request I am trying to call once I have been authorized.
This is my code with the appropriate redactions:
import UIKit
import OAuthSwift
class ViewController: UIViewController {
var session:NSURLSession!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func postToTumblr(sender: AnyObject) {
let oauthSwift = OAuth1Swift(
consumerKey: "***",
consumerSecret: "***",
requestTokenUrl: "https://www.tumblr.com/oauth/request_token",
authorizeUrl: "https://www.tumblr.com/oauth/authorize",
accessTokenUrl: "https://www.tumblr.com/oauth/access_token"
)
oauthSwift.authorizeWithCallbackURL(NSURL(string: "tumblrsampleapp://oauth-callback")!,
success: { credential, response in
// post to Tumblr
print("OAuth successfully authorized")
// Configure NSURLSession
let config = NSURLSessionConfiguration.ephemeralSessionConfiguration()
config.HTTPAdditionalHeaders = ["Authorization":credential]
self.session = NSURLSession(configuration: config)
// Post hardcoded data to Tumblr
let request = self.request("consumerSecretKey&\(credential.oauth_token_secret)")
let uploadTask = self.session.dataTaskWithRequest(request!) { (responseData, response, error) in
// Check on some response headers (if it's HTTP)
if let httpResponse = response as? NSHTTPURLResponse {
switch httpResponse.statusCode {
case 200..<300:
print("Success")
case 400..<500:
print("Request error")
case 500..<600:
print("Server error")
case let otherCode:
print("Other code: \(otherCode)")
}
}
// Do something with the response data
if let responseData = responseData,
responseString = String(data: responseData, encoding: NSUTF8StringEncoding) {
print("Server Response:")
print(responseString)
}
// Do something with the error
if let error = error {
print(error.localizedDescription)
}
}
uploadTask.resume()
}, failure: {(error:NSError!) -> Void in
self.presentAlert("Error", message: error!.localizedDescription)
})
}
func request(credential:String) -> NSURLRequest? {
guard let url = NSURL(string: "https://api.tumblr.com/v2/blog/{hostname}/post") else {return nil}
let request = NSMutableURLRequest(URL: url)
let requestData = self.buildRequestData()
request.HTTPMethod = "POST"
request.HTTPBody = requestData
let postDataLengthString = String(format:"%d", requestData.length)
let credentialString = String(format:"%d", credential)
request.setValue(postDataLengthString, forHTTPHeaderField:"Content-Length")
request.setValue(credentialString, forHTTPHeaderField: "Authorization")
return request
}
func buildRequestData() -> NSData {
// Getting these parameters from this site:
// https://www.tumblr.com/docs/en/api/v2#posting
// It looks like there are a bunch of optional paramters
let requestDictionary = [
["type":"text"],
["title": "Hello, World!"],
["body": "Hello world. This is my first post."]
]
let data = try? NSJSONSerialization.dataWithJSONObject(requestDictionary, options: NSJSONWritingOptions())
return data!
}
func presentAlert(title: String, message: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
I believe the issue involves the request function not generating the header properly. I did not see anything in the Tumblr documentation saying that I need to include an authorization token with the request, so I am assuming that they are assuming I am supposed to know that.
Any insight as to what I am doing wrong would be greatly appreciated. Thanks!

Resources