JSON NSURLRequest with authority

I have an api in my quick application that requires additional authorization. There are several examples provided by the service, but none of them are quick.

My code is:

let request = NSURLRequest(URL: NSURL(string: "https://www.kimonolabs.com/api/au4sl00w?apikey=iFotcJDm95fB6Ua7XiZRDZA0jl3uYWev")!) 

Pyhton example

 import urllib2 request = urllib2.Request("https://www.kimonolabs.com/api/au4sl00w? apikey=iFotcJDm95fB6Ua7XiZRDZA0jl3uYWev", headers={"authorization" : "Bearer A5ve02gq40itf0eoYfT5ny6drZwcysxx"}) contents = urllib2.urlopen(request).read() 

Ruby example

 require 'rest_client' response = RestClient.get('https://www.kimonolabs.com/api/au4sl00w?apikey=iFotcJDm95fB6Ua7XiZRDZA0jl3uYWev', {'authorization' => 'Bearer A5ve02gq40itf0eoYfT5ny6drZwcysxx'}); print(response); 

Example in R

 library('RCurl') library('rjson') json <- getURL('https://www.kimonolabs.com/api/au4sl00w?apikey=iFotcJDm95fB6Ua7XiZRDZA0jl3uYWuc', httpheader = c(authorization='Bearer A5ve02gq40itf0eoYfT5ny6drZwcysxx')) obj <- fromJSON(json) print(obj) 

So how can I do this in Swift?

+2
source share
1 answer

Modified response from "How to make an HTTP request + basic auth in Swift" .

I believe this will look something like this (and if your API_ID is au4sl00w ):

 let token = "yourToken" // create the request let url = NSURL(string: "https://www.kimonolabs.com/api/au4sl00w?apikey=iFotcJDm95fB6Ua7XiZRDZA0jl3uYWev") let request = NSMutableURLRequest(URL: url) request.HTTPMethod = "GET" request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization") 

And be sure to create a new access token, now this is public :)

+7
source

All Articles