Not signed in (Sign In)
The Intervals Forum is read-only
Please head to help.myintervals.com for help articles and guides. If you have any questions, please contact our support team.

API

C# Example to get a persons time

Bottom of Page

1 to 7 of 7

  1.  
  1.  

    Hi,
    I am new to intervals and need help? I need to get a persons time using C#. Can someone please help? I'd really appreciate it. I am creating a local windows app in c#, This is what i have so far and don't know how to proceed further. I want to use date range to get specific time entered by the user.

    var request = (HttpWebRequest)HttpWebRequest.Create("https://api.myintervals.com/person/");

    request.Accept = "application/xml";

    request.ContentType = "application/xml";

    request.Proxy = (WebProxy)WebProxy.GetDefaultProxy();

    request.Method = "GET";

    request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes("APIToken")));

    what comes next??? please help!!!!!!!

    Thank you in advance

    • cameron
    • May 4th 2011 edited @ 05/04/2011 11:25 am
     

    Were you able to connect to the Intervals API and successfully get a list of people?

    Have you written C# code in the past where you were successfully able to connect to a RESTful API?

    Unfortunately, C# is not my forte, but here's some sample code I found for making a sample GET request using C#:


    HttpWebRequest request = WebRequest.Create("https://api.myintervals.com/person/") as HttpWebRequest;
    request.Accept = "application/xml";
    request.ContentType = "application/xml";
    request.Credentials = new NetworkCredential("API_TOKEN", "x");

    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) {
    StreamReader reader = new StreamReader(response.GetResponseStream());
    // do something with the response
    Console.WriteLine(reader.ReadToEnd());
    }


    Of course, you'll have to add your own code to parse the response from the StreamReader object, including appropriate error handling to account for failed requests. And I would try bypassing the proxy until you've made a successful connection, and I wouldn't use it unless you absolutely need to.

    If you get that to work, the person resource used in the code above will give you a list of people, along with their personids. Then, you can use the time resource to get the list of time for a particular user for a date range. The URI for that request will look something like this:


    https://api.myintervals.com/time/?personid=9999&datebegin=2011-01-01&dateend=2001-04-30


    But let's not get ahead of ourselves. Work on making the simple request for the list of persons work first.

    Cameron

    • imran.silat
    • May 4th 2011 edited @ 05/04/2011 11:38 am
     

    Thank you Cameron for a detailed response. I have never connected to a RESTful API but i have written C# code:

    here is my latest code on a button event handler: which errors out as "401 unauthorized", API Token I am using is generated thru timetask.

    HttpWebRequest request =
    (HttpWebRequest)WebRequest.Create("https://api.myintervals.com/person/");

    request.Accept = "application/xml";

    request.ContentType = "application/xml";

    request.Credentials = new NetworkCredential("<APITOKEN>", "x");

    IWebProxy aProxy = WebRequest.DefaultWebProxy;

    //request.Proxy = (WebProxy)WebProxy.GetDefaultProxy();

    request.Proxy = aProxy;

    request.Method = "GET";

    request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes("<APITOKEN>")));


    HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //this where the error occurs

    Stream receiveStream = response.GetResponseStream();

    response.Close();
    receiveStream.Close();

    Any suggestions?
    Imran

    • cameron
    • May 4th 2011 edited @ 05/04/2011 1:51 pm
     

    Hey Imran,

    Try removing the following line:


    request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes("<APITOKEN>")));


    The reason is that you're supplying authorization twice, and your second implementation is incorrect and overriding the first implementation. Use the NetworkCredential object rather than manually entering the Authorization header.

    Cameron

  2.  

    Cameron,

    I did try that and either way I get the same error.

    Any other ideas? I thought it was going to be simple call to an API to get a response :-)

    -Imran

  3.  

    imran,

    The token needs a : (colon) right after. The left side is the user name and the password is to the right. We don't make use of the password so the string should look like:

    System.Text.Encoding.ASCII.GetBytes("APITOKEN:")

    // Create the web request
    HttpWebRequest request = WebRequest.Create("https://api.myintervals.com/person/") as HttpWebRequest;
    request.Method = "GET";
    request.Accept = "application/xml";
    request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes("APITOKEN:")));

    // Get response
    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
    {
    // Get the response stream
    StreamReader reader = new StreamReader(response.GetResponseStream());

    // Console application output
    Console.WriteLine(reader.ReadToEnd());
    }

  4.  

    jpardo,
    Thank you very much sir
    that did the trick. Awesome!!!!

    -Imran

Comments are closed.
For more Intervals help documentation, please visit help.myintervals.com