gpt4 book ai didi

angularjs - Mailgun + AngularJS + 用于 http post 请求的 Auhtentication

转载 作者:行者123 更新时间:2023-12-01 10:35:12 25 4
gpt4 key购买 nike

我正在尝试在 Angular 请求中转换以下示例代码。

https://documentation.mailgun.com/user_manual.html#sending-via-api

curl -s --user 'api:YOUR_API_KEY' \
https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages \
-F from='Excited User <mailgun@YOUR_DOMAIN_NAME>' \
-F to=YOU@YOUR_DOMAIN_NAME \
-F to=bar@example.com \
-F subject='Hello' \
-F text='Testing some Mailgun awesomness!'

我在下面尝试了授权 header ,但仍然返回未授权错误。我看到请求 header 的授权字段设置了值。我做错了什么?

    var url = "https://api.mailgun.net/v3/sandboxXXXXXXXXXXXXXXXXX.mailgun.org/messages";
var dataFields = {
to: "verified recepient",
subject: "subject",
text: "text",
from: "postmaster address of sandbox domain"
}

var req = {
method : 'POST',
url: url,
headers : {
'Authorization' : 'Basic api:key-XXXXXXXXXXXXXXXX'
},
data: dataFields
}
$http(req).then(function(data){
console.log(data);
}, function(data){
console.log(data);
})

最佳答案

终于让它在本地机器上工作了——从不同的帖子收集信息并使用这个插件——https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi - 我能够让它发挥作用。那么这个插件有什么作用呢?我可以在我的发帖请求中这样做吗?

没有它会给我错误

XMLHttpRequest cannot load https://api.mailgun.net/v3/sandboxXXXXXXXXXX.mailgun.org/messages. Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response.

var url = "https://api.mailgun.net/v3/sandboxXXXXXXXXXXXXXXX.mailgun.org/messages";
var dataJSON = {
from: "postmaster@sandboxXXXXXXXXXXXXXXX.mailgun.org",
to: "registered recepient",
subject: "Subject text",
text: "Body text",
multipart: true
}

var req = {
method : 'POST',
url: url,
headers : {
'content-type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + $base64.encode('api:key-XXXXXXXXXXXXX')
},
transformRequest: function(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data: dataJSON
}
$http(req).then(function(data){
console.log(data);
}, function(data){
console.log(data);
})
  • 我遗漏的东西。
  • 多部分
  • 上下文类型
  • encodedURIComponent - 用于参数
  • base64 编码的 api key

关于angularjs - Mailgun + AngularJS + 用于 http post 请求的 Auhtentication,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36815470/

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