Solved

how to call API in Winform C#

Posted on 2013-11-15
10
3,089 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Suggested Solutions

Title # Comments Views Activity
.net and XML report from SCCM 5 24
Visual Studio 2013 and Eclipse Neon 6 37
Help with exporting to excel 4 27
JSON  parse help 7 25
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

773 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