HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 接收MailChimp错误“您的请求未包含API密钥.”即使使用Alamofire包含API密钥大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用Alamofire向MailChimp发送请求以将用户添加到列表中

@H_90_5@mailChimp的文档说:

我为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密钥未包含在内:

我哪里出错了?

解决方法@H_489_24@
斯威夫特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密钥所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。