Solved

how to call API in Winform C#

Posted on 2013-11-15
10
2,784 Views
Last Modified: 2013-11-19
HI All,

i using third part website to enter my time sheet, now they released their API to call and get the report like below.

Doco: https://github.com/toggl/toggl_api_docs/blob/master/reports.md
.

Now i am trying to create a simple winform in C# and call the Toggl API to display the weekly time report for my time sheet.

Could you please advise me what's the best way to call the API and pull the report from Toggl and view in my winforms ?

Thanks in Advance
0
Comment
Question by:Sha1395
  • 6
  • 4
10 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39653472
You should be able to use an HttpWebRequest to make the necessary calls to what looks like a  REST web service, as shown in this example:

Make Yahoo! Web Service REST Calls With C#
http://developer.yahoo.com/dotnet/howto-rest_cs.html

using System;  
using System.IO;  
using System.Net;  
using System.Text;  
  
// Create the web request  
HttpWebRequest request = WebRequest.Create("http://developer.yahoo.com/") as HttpWebRequest;  
  
// 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());  
}  

Open in new window

0
 

Author Comment

by:Sha1395
ID: 39655249
Thanks learned one, you help is much appreciated . Here what i found from their link

Im trying to do simple thing call this third party API "https://github.com/toggl/toggl_api_docs/blob/master/reports.md"

The reports API base URL is https://toggl.com/reports/api/v2

Weekly report URL GET https://toggl.com/reports/api/v2/weekly

Detailed report URL: GET https://toggl.com/reports/api/v2/details

Summary report URL: GET https://toggl.com/reports/api/v2/summary

they provide three different API URL for different reports.

All im trying to get the weekly report pass the token.

I just look at their examples but its not very clear to me

This is what i try to do based on their examples

var apiKey="2d1d95cef10e17831ec505e9e6f9f7ea";
var usrSrv = new Toggl.Services.UserService(apiKey); //i have no idea what is this line means ???
var c = usrSrv.GetCurrent();
Console.WriteLine(c.FullName);
Console.WriteLine(c.Email);

Open in new window



They do have more code samples here but its very tough to follow for me  "http://litemedia.info/connect-to-toggl-api-with-net"

Please throw me some light all i want just call that api and pull the report. Thank you so much in advance
0
 

Author Comment

by:Sha1395
ID: 39655292
Here what i tried finally and i got

"The remote server returned an error: (407) Proxy Authentication Required." exception

 HttpWebRequest request
               = WebRequest.Create("https://toggl.com/reports/api/v2/weekly") as HttpWebRequest;
            request.Method = "GET";
            request.Accept = "application/atom+xml";
            request.Headers.Add(HttpRequestHeader.Authorization, "d17816281ed29c7a8460a36bfb59f262");
            // 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());
            }  

Open in new window

0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 39655452
Do you have a web proxy within your network?  

Here is one possible solution:

HttpWebRequest WebException: The remote server returned an error: (407) Proxy Authentication Required.
http://blogs.msdn.com/b/jpsanders/archive/2009/03/24/httpwebrequest-webexcepton-the-remote-server-returned-an-error-407-proxy-authentication-required.aspx
0
 

Author Comment

by:Sha1395
ID: 39655481
Thank again TheLearnedOne, i will change the proxy settings in my config and update ,Thanks again.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:Sha1395
ID: 39657994
I changed the code like below

 // create request
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.toggl.com/api/v2/me");
            //HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://toggl.com/reports/api/v2/details");
            IWebProxy theProxy = request.Proxy;
            if (theProxy != null)
            {
                theProxy.Credentials = CredentialCache.DefaultCredentials;
            }
            CookieContainer cookies = new CookieContainer();
            request.UseDefaultCredentials = true;
            request.CookieContainer = cookies;

            // build authentication token
            //string authToken = "15869be800840d58ed4ec1a9e5e32235:api_token";
            string authToken = "15869be800840d58ed4ec1a9e5e32235";

            // encode to Base64
            byte[] bytes = Encoding.ASCII.GetBytes(authToken);
            authToken = Convert.ToBase64String(bytes);

            // write the "Authorization" header
            request.Headers.Add("Authorization", "Basic " + authToken);
           //request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
            request.Method = "GET";

            // get the response
            WebResponse response = request.GetResponse();

Open in new window



App Config :

<configuration>
  <system.net>
    <defaultProxy useDefaultCredentials="true">
     </defaultProxy>
  </system.net>
</configuration>

Open in new window

and added app config what you mentioned in the previous post but i'm getting new exception

"The remote server returned an error: (403) Forbidden.".

here is the account details of my test account in toggl

UserName :Test1395@gmil.com
Pwd:Test1395
Token:c620724108921c4530e745798ed19104

Thanks
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39658341
The only thing that I can see is the you are using the ASCII encoder, and I would think that you would need to use the UTF-8 encoder:

 var bytes = Encoding.Utf8.GetBytes(authToken);
0
 

Author Comment

by:Sha1395
ID: 39658346
Thanks for all your help LearnedOne, you always rock. Finally figured out and its working.

here is the code
 // create request
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.toggl.com/api/v8/me");
            //HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://toggl.com/reports/api/v2/details");
            IWebProxy theProxy = request.Proxy;
            if (theProxy != null)
            {
                theProxy.Credentials = CredentialCache.DefaultCredentials;
            }
            CookieContainer cookies = new CookieContainer();
            request.UseDefaultCredentials = true;
            request.CookieContainer = cookies;
            request.ContentType = "application/json";
            request.CookieContainer = cookies;
            // build authentication token
            //string authToken = "15869be800840d58ed4ec1a9e5e32235:api_token";
            string authToken = "c620724108921c4530e745798ed19104:api_token";

            // encode to Base64
            byte[] bytes = Encoding.ASCII.GetBytes(authToken);
            authToken = Convert.ToBase64String(bytes);

            // write the "Authorization" header
            request.Headers.Add("Authorization", "Basic " + authToken);
           //request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
            request.Method = "GET";

            // get the response
            //WebResponse response = request.GetResponse();
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                StreamReader reader = new StreamReader(response.GetResponseStream());
                Console.Write(reader.ReadToEnd());
            }

Open in new window

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39659192
OK, so you had the wrong auth token...that would explain it!!
0
 

Author Comment

by:Sha1395
ID: 39661044
No i reset the authentication code again to check that helps me to get thru but the real catch is

https://www.toggl.com/api/v2/me to
https://www.toggl.com/api/v8/me

 request.ContentType = "application/json";

from their link

The API Format

The API accepts only JSON requests. Please make sure you're setting Content-type: application/jsonin your request header. Each request returns a JSON-encoded body.

The result of each action is communicated via standard HTTP response codes.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now