在 Swift 中添加 API X-Auth-Token 密钥?
Add API Key in X-Auth-Token in Swift?
所以,我正在使用这个网站 football-data.org API 来获取 JSON 数据。
但是,只有 50 个 req/day 的限制,除非您获得 API 密钥并将其集成。我很乐意这样做,问题是我不知道该怎么做。
我已经注册并获得了我的 API 密钥,并且在电子邮件中还说:
Please modify your client to use
a header-field named "X-Auth-Token" with the underneath personal token as value.
您的 API 代币:4180b1645f624408b6291349204122344
他给出了其他编程语言的示例代码,例如 PHP:
<?php
$uri = 'http://api.football-data.org/alpha/soccerseasons/354/fixtures/?matchday=22';
$reqPrefs['http']['method'] = 'GET';
$reqPrefs['http']['header'] = 'X-Auth-Token: YOUR_TOKEN';
$stream_context = stream_context_create($reqPrefs);
$response = file_get_contents($uri, false, $stream_context);
$fixtures = json_decode($response);
?>
但这对我来说毫无意义。
我们如何在 Swift 中做到这一点?我尝试 google 但找不到任何东西。
假设您使用本机 iOS API 来发出 HTTP 请求:
let request = NSMutableURLRequest(URL: NSURL(string: "http://endpoint")!)
request.addValue("4180b1645f624408b6291349204122344", forHTTPHeaderField: "X-Auth-Token")
request.HTTPMethod = "GET" // or POST or whatever
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response, data, error) in
// handle your data here
}
所以,我正在使用这个网站 football-data.org API 来获取 JSON 数据。
但是,只有 50 个 req/day 的限制,除非您获得 API 密钥并将其集成。我很乐意这样做,问题是我不知道该怎么做。
我已经注册并获得了我的 API 密钥,并且在电子邮件中还说:
Please modify your client to use a header-field named "X-Auth-Token" with the underneath personal token as value.
您的 API 代币:4180b1645f624408b6291349204122344
他给出了其他编程语言的示例代码,例如 PHP:
<?php
$uri = 'http://api.football-data.org/alpha/soccerseasons/354/fixtures/?matchday=22';
$reqPrefs['http']['method'] = 'GET';
$reqPrefs['http']['header'] = 'X-Auth-Token: YOUR_TOKEN';
$stream_context = stream_context_create($reqPrefs);
$response = file_get_contents($uri, false, $stream_context);
$fixtures = json_decode($response);
?>
但这对我来说毫无意义。
我们如何在 Swift 中做到这一点?我尝试 google 但找不到任何东西。
假设您使用本机 iOS API 来发出 HTTP 请求:
let request = NSMutableURLRequest(URL: NSURL(string: "http://endpoint")!)
request.addValue("4180b1645f624408b6291349204122344", forHTTPHeaderField: "X-Auth-Token")
request.HTTPMethod = "GET" // or POST or whatever
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response, data, error) in
// handle your data here
}