2

I have an api in my swift app that needs some extra authorization. There are some examples provided by the service but none in swift.

My Code:

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

Example in Python

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

Example in Ruby

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?

1 Answer 1

7

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

I believe it would look something like this (and assuming 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 that this one is public :)

Sign up to request clarification or add additional context in comments.

5 Comments

I am not sure what you mean. I don't want a username and password. I need this token Bearer A5ve02gq40itf0eoYfT5ny6drZwcysxx as authorisation. Can you still help?
Oh! Im sorry, I misread. But I think it's about the same steps, just update: request.setValue("Bearer \(yourToken)", forHTTPHeaderField: "Authorization" (I'll be sure to update my answer if this was in fact what you were looking for)
Thanks for that. With my request (First code in question) what do I need to change.
Hello, Don't worry. I figured it out. let request = NSMutableURLRequest(URL: NSURL(string: "kimonolabs.com/api/…)
Thanks for updating your question also. Very helpful!

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.