2016-04-23 132 views
3

我試圖在角度請求中轉換下面的示例代碼。Mailgun + AngularJS + Auhtentication爲http post請求

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 <[email protected]_DOMAIN_NAME>' \ 
    -F [email protected]_DOMAIN_NAME \ 
    -F [email protected] \ 
    -F subject='Hello' \ 
    -F text='Testing some Mailgun awesomness!' 

我曾與仍回來了未經授權的錯誤授權頭下方嘗試。我看到請求頭具有設置值的授權字段。 我在做什麼錯?

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); 
    }) 

回答

0

嘗試添加 用戶名: 'API', 密碼: 'yourapikey',

在你的頭的請求

+0

變種REQ = { 方法: 'POST', 網址:網址, 用戶名: 「API」 , 密碼:「API密鑰」, data:dataFields } –

+0

不起作用仍然說禁止 – Sumant

6

終於得到它從本地機器的工作 - 來自不同崗位的集體信息和使用這個插件 - 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: "[email protected]", 
      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密鑰