Outlook Rest calling form angularjs

2019-05-26 06:49发布

问题:

Using outlook I am trying to create event, When i send request using POSTMAN its working fine, But same code in Angularjs its not wotking. what is wrong with code. Please help.

 $scope.createEvents = function(){
    var url = "https://outlook.office.com/api/v2.0/$metadata#Me/Calendars";
    //var url = "https://outlook.office.com/api/v2.0/$metadata#me/Calendars";

    var add_events = {
              "Subject": "Discuss the Calendar REST API",
              "Body": {
                "ContentType": "HTML",
                "Content": "I think it will meet our requirements!"
              },
              "Start": {
                  "DateTime": "2016-10-10T18:00:00",
                  "TimeZone": "Pacific Standard Time"
              },
              "End": {
                  "DateTime": "2016-10-10T19:00:00",
                  "TimeZone": "Pacific Standard Time"
              },
              "Attendees": [
                {
                  "EmailAddress": {
                    "Address": "sathish.gopikrishnan@stradegi.com",
                    "Name": "Sathish Gopi"
                  },
                  "Type": "Required"
                }
              ]
            };
    $http({
        method: 'POST',
        url: url,
        headers:{
            'Authorization':'Bearer '+$scope.token,
            'Content-Type': "application/json",
            'Accept': 'application/json;odata.metadata=minimal',
            'Access-Control-Allow-Origin':'*'            
        },          
        data: add_events
    }).Succes(function (response) {
        alert("Saved")
    });   

I am getting. Failed to load resource: the server responded with a status of 406 (Not Acceptable). To solve this problem i am using this code now

$scope.createEvents = function(){
var url = "https://outlook.office.com/api/v2.0/$metadata#Me/Calendars";
//var url = "https://outlook.office.com/api/v2.0/$metadata#me/Calendars";

var add_events = {
          "Subject": "Discuss the Calendar REST API",
          "Body": {
            "ContentType": "HTML",
            "Content": "I think it will meet our requirements!"
          },
          "Start": {
              "DateTime": "2016-10-10T18:00:00",
              "TimeZone": "Pacific Standard Time"
          },
          "End": {
              "DateTime": "2016-10-10T19:00:00",
              "TimeZone": "Pacific Standard Time"
          },
          "Attendees": [
            {
              "EmailAddress": {
                "Address": "sathish.gopikrishnan@stradegi.com",
                "Name": "Sathish Gopi"
              },
              "Type": "Required"
            }
          ]
        };
$http({
    method: 'JSONP',
    url: url,
    headers:{
        'Authorization':'Bearer '+$scope.token,
        'Content-Type': "application/json",
        'Accept': 'application/json;odata.metadata=minimal',
        'Access-Control-Allow-Origin':'*'            
    },          
    data: add_events
}).Succes(function (response) {
    alert("Saved")
});

After Using Jsonp as method I am getting this error

Uncaught SyntaxError: Unexpected token <

回答1:

406 Not Acceptable is defined as the following:

The resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request.

see restapitutorial: http status codes

You send the following Accept header:

'Accept': 'application/json;odata.metadata=minimal'

The server says, it cannot produce this content type. If you got it running using postman, have a look at which accept header you've provided there.

Concerning your second try: jsonp is not an http method/verb (GET, PUT, POST, DELETE, PATCH). I'm not an angularjs expert but in case you need jsonp, there is a method you can call.

JSONP is a quite specific (and somewhat problematic) technique. So as long as you can do that, try to avoid it.



回答2:

i know its an old post, i've had the same problem and this is the solution if someone falls into the same problem, you should use https://graph.microsoft.com/v1.0/me/calendar/events as URL

Exemple tested code :

  var url = "https://graph.microsoft.com/v1.0/me/calendar/events";

     var add_events = {
                                          "Subject": "Title",
                                          "Body": {
                                            "ContentType": "HTML",
                                            "Content": " Exemple"
                                          },
                                          "Start": {
                                              "DateTime": "2010-05-17T18:00:00",
                                              "TimeZone": "UTC"
                                          },
                                          "End": {
                                              "DateTime": "2010-05-17T19:00:00",
                                              "TimeZone": "UTC"
                                          }
                                        };
     $http({
          method: 'POST',
          url: url,
          headers:{
                                           'Authorization':'Bearer '+localStorage.getItem('Your Token here'),
                                           'Content-Type':'application/json',
                                           'Accept': 'application/json;odata.metadata=minimal',
                                           'Access-Control-Allow-Origin':'*',
                                           'token_type':'Bearer'
                                       },
                                       data: add_events
                                     }).then(function successCallback(response) {

                                        console.log(response);

                                       }, function errorCallback(response) {

                                         localStorage.setItem('etat_mytoken', 0);
                                         $window.location.href =  url_base+'/'+url_base_v+'/app/assets/views/token.html';


                                       });