Solved

how to call API in Winform C#

Posted on 2013-11-15
10
3,424 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

726 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