gpt4 book ai didi

ios - 从 Swift 5 iOS 应用程序进行 Mailchimp API 调用时“API key 丢失”

转载 作者:行者123 更新时间:2023-11-28 13:30:48 25 4
gpt4 key购买 nike

我正在尝试通过我的 Swift 5 iOS 应用程序将订阅者添加到我的邮寄列表中。我在尝试执行此操作时看到以下错误:

{
detail = "Your request did not include an API key.";
instance = "3f4cb654-c674-4a97-adb8-b4eb6d86053a";
status = 401;
title = "API Key Missing";
type = "http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/";
}

当然,这表明我丢失了我的 API key ,但我在授权 header 中指定了它(请参见下面的代码)。我尝试了混合答案 here和指南here但到目前为止我运气不佳。这是我当前用于设置请求的代码:

let mailchimpAPIURL = "https://us3.api.mailchimp.com/3.0"
let requestURL = NSURL(string: mailchimpAPIURL)!

let apiCredentials = "anystring:<API_KEY>"
let loginData = apiCredentials.data(using: String.Encoding.utf8)!.base64EncodedString()

let params = [
"list_id": "<LIST_ID>",
"email_address": email,
"status": "subscribed",
"merge_vars": [
"FNAME": firstName,
"LNAME": lastName
]
] as [String: Any]

let request = NSMutableURLRequest(url: requestURL as URL)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.setValue("Basic \(loginData)", forHTTPHeaderField: "Authorization")
do {
request.httpBody = try JSONSerialization.data(withJSONObject: params, options: [])
} catch {
return
}

最佳答案

You need to send api key in the authorization header like this:

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] {

}
}

关于ios - 从 Swift 5 iOS 应用程序进行 Mailchimp API 调用时“API key 丢失”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57366669/

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