diff --git a/Vendoo.xcworkspace/xcuserdata/okechi.xcuserdatad/UserInterfaceState.xcuserstate b/Vendoo.xcworkspace/xcuserdata/okechi.xcuserdatad/UserInterfaceState.xcuserstate index 1c64daa..a990e79 100644 Binary files a/Vendoo.xcworkspace/xcuserdata/okechi.xcuserdatad/UserInterfaceState.xcuserstate and b/Vendoo.xcworkspace/xcuserdata/okechi.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/Vendoo.xcworkspace/xcuserdata/okechi.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/Vendoo.xcworkspace/xcuserdata/okechi.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index 943c478..6dc9aa6 100644 --- a/Vendoo.xcworkspace/xcuserdata/okechi.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/Vendoo.xcworkspace/xcuserdata/okechi.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -90,11 +90,11 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "Vendoo/EtsyRESTAPIManager.swift" - timestampString = "486467803.784775" + timestampString = "486486828.045437" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "52" - endingLineNumber = "52" + startingLineNumber = "67" + endingLineNumber = "67" landmarkName = "authorizeApp(_:)" landmarkType = "5"> @@ -106,11 +106,11 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "Vendoo/EtsyRESTAPIManager.swift" - timestampString = "486467838.042152" + timestampString = "486489301.995309" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "66" - endingLineNumber = "66" + startingLineNumber = "145" + endingLineNumber = "145" landmarkName = "authorizeApp(_:)" landmarkType = "5"> @@ -122,59 +122,11 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "Vendoo/EtsyRESTAPIManager.swift" - timestampString = "486468123.916868" + timestampString = "486489301.995309" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "82" - endingLineNumber = "82" - landmarkName = "authorizeApp(_:)" - landmarkType = "5"> - - - - - - - - - - @@ -186,11 +138,43 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "Vendoo/EtsyRESTAPIManager.swift" - timestampString = "486468341.941555" + timestampString = "486489480.473889" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "84" - endingLineNumber = "84" + startingLineNumber = "114" + endingLineNumber = "114" + landmarkName = "authorizeApp(_:)" + landmarkType = "5"> + + + + + + + + diff --git a/Vendoo/EtsyRESTAPIManager.swift b/Vendoo/EtsyRESTAPIManager.swift index c665515..b82e628 100644 --- a/Vendoo/EtsyRESTAPIManager.swift +++ b/Vendoo/EtsyRESTAPIManager.swift @@ -11,24 +11,29 @@ import Foundation import SwiftyJSON import OAuthSwift +import Locksmith typealias ServiceResponse = (JSON, NSError?) -> Void + class EtsyRESTAPIManager: NSObject { //API Manager class variables //----------------------------------------------// static let sharedInstance = EtsyRESTAPIManager() - let baseURL = "https://openapi.etsy.com/v2" + let baseURL = "https://openapi.etsy.com/v2/private" private var apiKey: String! private var apiSecret: String! + private var userEmail:String = (NSUserDefaults.standardUserDefaults().objectForKey("email") as? String)! var isAuthorized: Bool = NSUserDefaults.standardUserDefaults().boolForKey("etsyAuthorized") //---------------------------------------------// //User specific class variables private var etsyUser: String! + private var oauth_token: String! + private var oauth_secret: String! //private let oauthswift = OAuth1Swift(parameters: ["consumerKey":"snbs78qkfy3yqq6yhe6yv49b","consumerSecret":"4sbva4oqb6", "requestTokenUrl": "https://openapi.etsy.com/v2/oauth/request_token?scope=listings_w%20listings_r%20listings_d%20transactions_r%20transactions_w%20"]) override init(){ @@ -37,6 +42,16 @@ class EtsyRESTAPIManager: NSObject { self.apiKey = ((dict["Etsy"] as! Dictionary)["consumerKey"] as! String) self.apiSecret = ((dict["Etsy"] as! Dictionary)["consumerSecret"] as! String) + + if(self.isAuthorized){ + let dictionary = Locksmith.loadDataForUserAccount(self.userEmail) + let oauthDictionary = dictionary?["etsy_oauth"] as? Dictionary + self.oauth_token = oauthDictionary?["token"] as? String! + self.oauth_secret = oauthDictionary?["secret"] as? String! + } + + + } @@ -52,42 +67,84 @@ extension EtsyRESTAPIManager { func authorizeApp(viewcontroller: UIViewController){ - + if(!isAuthorized){ + let oauthswift = OAuth1Swift(parameters: ["consumerKey":self.apiKey,"consumerSecret":self.apiSecret, "requestTokenUrl" : "https://openapi.etsy.com/v2/oauth/request_token?scope=listings_w%20listings_r%20listings_d%20transactions_r%20transactions_w", + "authorizeUrl": "https://www.etsy.com/oauth/signin?", + "accessTokenUrl": "https://openapi.etsy.com/v2/oauth/access_token?"]) + + oauthswift!.authorize_url_handler = SafariURLHandler(viewController: viewcontroller) + + oauthswift!.client.get("https://openapi.etsy.com/v2/oauth/request_token?scope=listings_w%20listings_r%20listings_d%20transactions_r%20transactions_w", + success: { + data, response in + let dataString = NSString(data: data, encoding: NSUTF8StringEncoding) + print(dataString!) + print(response) + + + + + //getting error here when trying to retrieve the login url from the response + //var dataDictionary = self.convertStringToDictionary(dataString! as String) + //print(dataDictionary!["login_url"]) + } + , failure: { error in + print(error) + } + ) + + dispatch_async(dispatch_get_main_queue(), {() -> Void in + oauthswift!.authorizeWithCallbackURL( NSURL(string: "vendoo://oauth-callback/etsy")!, success: { + credential, response, parameters in + print(credential.oauth_token) + print(credential.oauth_token_secret) + print(response) + + + + }, failure: { error in + print(error) + + dispatch_async(dispatch_get_main_queue(), {() -> Void in + print(oauthswift!.client.credential.oauth_token) + print(oauthswift!.client.credential.oauth_token_secret) + //save data to keychain + let creds = oauthswift?.client.credential + let oauth: Dictionary? = [ "token" : creds!.oauth_token, "secret" : creds!.oauth_token_secret ] + do{ + try Locksmith.saveData(["etsy_oauth" : oauth!], forUserAccount: self.userEmail) + print("account credentials saved") + NSUserDefaults.standardUserDefaults().setBool(true, forKey: "etsyAuthorized") + self.isAuthorized = true + } + catch{ + (error) + print(error) + //could not save data to keychain + print("account credentials could not be saved") + + } + oauthswift!.client.get(self.baseURL, + success: { + data, response in + let dataString = NSString(data: data, encoding: NSUTF8StringEncoding) + print(dataString) + } + , failure: { error in + print(error) + } + ) + + + }) + }) + }) + } - let oauthswift = OAuth1Swift(parameters: ["consumerKey":"snbs78qkfy3yqq6yhe6yv49b","consumerSecret":"4sbva4oqb6", "requestTokenUrl" : "https://openapi.etsy.com/v2/oauth/request_token?scope=listings_w%20listings_r%20listings_d%20transactions_r%20transactions_w", - "authorizeUrl": "https://www.etsy.com/oauth/signin?", - "accessTokenUrl": "https://openapi.etsy.com/v2/oauth/access_token?"]) - oauthswift!.authorize_url_handler = SafariURLHandler(viewController: viewcontroller) - oauthswift!.client.get("https://openapi.etsy.com/v2/oauth/request_token?scope=listings_w%20listings_r%20listings_d%20transactions_r%20transactions_w", - success: { - data, response in - let dataString = NSString(data: data, encoding: NSUTF8StringEncoding) - print(dataString!) - print(response) - - - - //getting error here when trying to retrieve the login url from the response - //var dataDictionary = self.convertStringToDictionary(dataString! as String) - //print(dataDictionary!["login_url"]) - } - , failure: { error in - print(error) - } - ) - oauthswift!.authorizeWithCallbackURL( NSURL(string: "vendoo://oauth-callback/etsy")!, success: { - credential, response, parameters in - print(credential.oauth_token) - print(credential.oauth_token_secret) - print(response) - }, failure: { error in - print(error) - }) - //once everything is authorized save true value to the authorization boolean @@ -98,6 +155,10 @@ extension EtsyRESTAPIManager { } + func deAuthorizeApp(viewcontroller: UIViewController) -> Bool { + + return true + } } diff --git a/Vendoo/Info.plist b/Vendoo/Info.plist index f094d7c..b8074a3 100644 --- a/Vendoo/Info.plist +++ b/Vendoo/Info.plist @@ -56,6 +56,11 @@ LSRequiresIPhoneOS + NSAppTransportSecurity + + NSAllowsArbitraryLoads + + UILaunchStoryboardName LaunchScreen UIMainStoryboardFile diff --git a/Vendoo/NetworksTableViewController.swift b/Vendoo/NetworksTableViewController.swift index 02848bf..c1d830c 100644 --- a/Vendoo/NetworksTableViewController.swift +++ b/Vendoo/NetworksTableViewController.swift @@ -396,13 +396,17 @@ extension NetworksTableViewController: UITableViewDelegate { //OAuthorization code for etsy - if(cell.networkToggle.on == true){ + if(((self.tabBarController as? HomeViewController)?.fbGraphManager.isAuthorized)!){ dispatch_async(dispatch_get_main_queue(), { () -> Void in cell.networkToggle.setOn(false, animated: true) }) //code to deauthorize network + let tabBar = self.tabBarController + + let didSucceed:Bool = ((tabBar as? HomeViewController)?.etsyManager.deAuthorizeApp(self))! + cell.networkToggle.on = !didSucceed } else{ @@ -413,7 +417,7 @@ extension NetworksTableViewController: UITableViewDelegate { //this is the type of code desired to access the rest management classes - + let tabBar = self.tabBarController (tabBar as? HomeViewController)?.etsyManager.authorizeApp(self) @@ -433,6 +437,7 @@ extension NetworksTableViewController: UITableViewDelegate { //OAuthorization code for facebook if((self.tabBarController as? HomeViewController)?.fbGraphManager.isAuthorized)!{ + dispatch_async(dispatch_get_main_queue(), { () -> Void in cell.networkToggle.setOn(false, animated: true) }) diff --git a/Vendoo/Services.plist b/Vendoo/Services.plist index 0fa436c..e2be465 100644 --- a/Vendoo/Services.plist +++ b/Vendoo/Services.plist @@ -4,6 +4,10 @@ Etsy + accessToken + + accessSecret + consumerKey snbs78qkfy3yqq6yhe6yv49b consumerSecret