我正在使用Alamofire向MailChimp发送请求以将用户添加到列表中
MailChimp的文档说:
API有两种身份验证方法:HTTP基本身份验证和OAuth2。最简单的身份验证方法是使用HTTP基本身份验证。输入任何字符串作为您的用户名,并提供您的API密钥作为密码。
我为Alamofire写的请求:
let params: [String : AnyObject] = ["email_address": email, "status": "subscribed", "merge_fields": [ "FNAME": name]]
guard let url = "https://us10.api.mailchimp.com/3.0/lists/<listID>/members/".stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) else { return }
Alamofire.request(.POST, url, parameters: params, encoding: .URL)
.authenticate(user: "apiKey", password: "<apikey>")
.responseJSON { response in
if response.result.isFailure {
}
else if let responseJSON = response.result.value as? [String: AnyObject] {
}
}
我通过使用它来访问他们的游乐场来检查API密钥是否正确: https://us1.api.mailchimp.com/playground/
我回复的回复表明没有包含API密钥:
您的请求未包含API密钥。
我哪里出错?
答案 0 :(得分:5)
因此,MailChimp实际上需要在授权标头中发送的api密钥,如下所示:
let params: [String: AnyObject] = ["email_address": email, "status": "subscribed"]
guard let url = "https://us10.api.mailchimp.com/3.0/lists/<listID>/members/".stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) else { return }
let credentialData = "user:<apikey>".dataUsingEncoding(NSUTF8StringEncoding)!
let base64Credentials = credentialData.base64EncodedStringWithOptions([])
let headers = ["Authorization": "Basic \(base64Credentials)"]
Alamofire.request(.POST, url, headers: headers, parameters: params, encoding: .URL)
.responseJSON { response in
if response.result.isFailure {
}
else if let responseJSON = response.result.value as? [String: AnyObject] {
}
}
编辑:请参阅下面的Derek Soike对Swift 3的回答
答案 1 :(得分:3)
Swift 3
请务必查看MailChimp的Error Glossary。 401 表示您的API密钥未正确读取。
对于Swift 3,Abbey Jackson's answer中的标题结构需要更新为此。否则它完全有效。
let credentialData = "AnyString:\(apiKey)".data(using: String.Encoding.utf8)!
let base64Credentials = credentialData.base64EncodedString()
let headers = ["Authorization": "Basic \(base64Credentials)"]
以下是使用Request.authorizationHeader
的示例。
let apiKey: String = "xxxxxxxxxxxxxxx2b-us11" // note the 'us11'
let baseUrl: String = "https://us11.api.mailchimp.com/3.0" // note the 'us11'
let listId: String = "xxxxxx2f"
func createListMember() {
let url = "\(baseUrl)/lists/\(listId)/members"
guard let authorizationHeader = Request.authorizationHeader(user: "AnyString", password: apiKey) else {
print("!authorizationHeader")
return
}
let headers: HTTPHeaders = [
authorizationHeader.key: authorizationHeader.value
]
let parameters: Parameters = [
"email_address": email,
"status": "subscribed"
]
// perform request (make sure you're using JSONEncoding.default)
Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers)
//.authenticate(user: "AnyString", password: apiKey) // this doesn't work
.validate()
.responseJSON {(response) in
print(response)
}
}
答案 2 :(得分:0)
Swift 3 截至2017年9月5日
它说方法是偶然的额外参数。您的编码部分很可能已关闭。它应该是这样的:
编码:JSONEncoding.default
答案 3 :(得分:-1)
如果您像bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx5dxf-us10
那样传递apikey,那么您将收到指定的错误。
尝试通过跳过连字符后面的字符来传递apikey(例如&#34; bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx5dxf
&#34;)并检查它是否有效。