MailChimp的文档说:
There are 2 authentication methods for the API: http Basic authentication and OAuth2. The easIEst way to authenticate is using http Basic authentication. Enter any string as your username and supply your API Key as the password.
我为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密钥未包含在内:
Your request dID not include an API key.
我哪里出错了?
解决方法 斯威夫特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) }}总结
以上是内存溢出为你收集整理的ios – 接收MailChimp错误“您的请求未包含API密钥.”即使使用Alamofire包含API密钥全部内容,希望文章能够帮你解决ios – 接收MailChimp错误“您的请求未包含API密钥.”即使使用Alamofire包含API密钥所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)