简体   繁体   中英

Outlook Rest API returning no results

I'm trying to display a list of events from an Outlook calendar using the example request microsoft have here

However, I've no proper experience with using any sort of REST APIs before. Using the URL they provide I should be getting something back but I'm not. Here is the code in my controller:

  string uri = "https://outlook.office365.com/api/v1.0/me/events";

    var webRequest = (HttpWebRequest)WebRequest.Create(uri);
    webRequest.Method = "GET";
    string result = "";
    try
    {
        WebClient webClient = new WebClient();
        webClient.Encoding = Encoding.UTF8;
        result = webClient.DownloadString(uri);
        try
        {
            string returnedString = result;
            TempData.Add("myval", result);
            ViewBag.result = "returned string " + result;

        }
        catch (Exception er2)
        {
            ViewBag.error = er2.Message;
        }

        ViewBag.secondresult = "first result " + result;
    }
    catch (Exception er)
    {


    }

    ViewBag.firstResult = "Outside try catch " + result;
    ViewBag.url = uri;
    return View();

Then in my view I'm calling the ViewBags like this:

<p> here </p>
@ViewBag.url
@ViewBag.firstResult                        
@ViewBag.result
@ViewBag.error
@ViewBag.secondresult
<p> end </p>

But besides my here and end I get nothing. This is a project that was set up without any input from myself so I'm having to do everything across a network which is why I'm using so many try catches .

Can someone with more experience with using REST APIs tell me if I'm messing something up somewhere?

I don't see any authentication in your code, so it's likely returning a 401. You need to use OAuth2 to get an access token and use that to authenticate your calls. Since you're doing this in C#, you might want to look at the API wrappers on NuGet that implement a lot of this for you. There are some sample starter apps on http://dev.office.com/code-samples that might be helpful too (search for ASP.NET MVC).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM