带有凭证的JSON NSURLRequest [英] JSON NSURLRequest with credentials
本文介绍了带有凭证的JSON NSURLRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的快速应用程序中有一个api,需要一些额外的授权.该服务提供了一些示例,但没有迅速提供.
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.
我的代码:
let request = NSURLRequest(URL: NSURL(string: "https://www.kimonolabs.com/api/au4sl00w?apikey=iFotcJDm95fB6Ua7XiZRDZA0jl3uYWev")!)
Pyhton中的示例
Example in Pyhton
import urllib2
request = urllib2.Request("https://www.kimonolabs.com/api/au4sl00w? apikey=iFotcJDm95fB6Ua7XiZRDZA0jl3uYWev", headers={"authorization" : "Bearer A5ve02gq40itf0eoYfT5ny6drZwcysxx"})
contents = urllib2.urlopen(request).read()
Ruby中的示例
require 'rest_client'
response = RestClient.get('https://www.kimonolabs.com/api/au4sl00w?apikey=iFotcJDm95fB6Ua7XiZRDZA0jl3uYWev', {'authorization' => 'Bearer A5ve02gq40itf0eoYfT5ny6drZwcysxx'});
print(response);
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)
那么,如何在Swift中做到这一点?
So, how can I do this in Swift?
推荐答案
修改后的答案来自:.
Modified answer from: "How to make an HTTP request + basic auth in Swift".
我相信它会看起来像这样(并假设您的API_ID
是au4sl00w
):
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 :)
这篇关于带有凭证的JSON NSURLRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文