Solved

how to call API in Winform C#

Posted on 2013-11-15
10
3,231 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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
 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

808 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