gpt4 book ai didi

ios - 带有凭据的 JSON NSURLRequest

转载 作者:可可西里 更新时间:2023-11-01 00:16:59 25 4
gpt4 key购买 nike

我的 swift 应用程序中有一个 api 需要一些额外的授权。该服务提供了一些示例,但没有在 swift 中提供。

我的代码:

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

Python 示例

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 中执行此操作?

最佳答案

修改后的答案来自:"How to make an HTTP request + basic auth in Swift" .

我相信它看起来像这样(假设您的 API_IDau4sl00w):

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")

并且一定要创建一个新的访问 token ,因为它是公开的:)

关于ios - 带有凭据的 JSON NSURLRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27956943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com