Harvest(考勤卡应用)API
本文关键字:应用 API Harvest | 更新日期: 2023-09-27 17:53:50
Harvest是我在工作中使用的时间跟踪应用程序。虽然web UI非常简单,但我想添加一些自定义功能。我注意到他们有一个API…所以我想用c#为它做一个自定义桌面客户端。
光看这一页,信息并不丰富。您可以找到的c#示例(在进行一些挖掘之后)也没有多大帮助。所以…我该如何在c#中使用API ?
链接到API页面
Harvest使用REST API,所以它所做的是你做一个get/put/post请求到服务器上的web地址,它将返回一个结果(通常以XML或JSON格式(在这种情况下似乎是XML))。在Google上快速搜索一下,就会得到这个关于如何使用REST API的教程,希望这足以满足您的需要。如果没有,请随时向我们询问您在使用REST和c#时遇到的具体问题
这里我将尝试在他们的示例中添加更多注释:
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
class HarvestSample
{
//This is used to validate the certificate the server gives you,
//it allays assumes the cert is valid.
public static bool Validator (object sender, X509Certificate certificate,
X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}
static void Main(string[] args)
{
//setting up the initial request.
HttpWebRequest request;
HttpWebResponse response = null;
StreamReader reader;
StringBuilder sbSource;
//1. Set some variables specific to your account.
//This is the URL that you will be doing your REST call against.
//Think of it as a function in normal library.
string uri = "https://yoursubdomain.harvestapp.com/projects";
string username="youremail@somewhere.com";
string password="yourharvestpassword";
string usernamePassword = username + ":" + password;
//This checks the SSL cert that the server will give us,
//the function is above this one.
ServicePointManager.ServerCertificateValidationCallback = Validator;
try
{
//more setup of the connection
request = WebRequest.Create(uri) as HttpWebRequest;
request.MaximumAutomaticRedirections = 1;
request.AllowAutoRedirect = true;
//2. It's important that both the Accept and ContentType headers
//are set in order for this to be interpreted as an API request.
request.Accept = "application/xml";
request.ContentType = "application/xml";
request.UserAgent = "harvest_api_sample.cs";
//3. Add the Basic Authentication header with username/password string.
request.Headers.Add("Authorization", "Basic " + Convert.
ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));
//actually perform the GET request
using (response = request.GetResponse() as HttpWebResponse)
{
//Parse out the XML it returned.
if (request.HaveResponse == true && response != null)
{
reader = new StreamReader(response.GetResponseStream(),
Encoding.UTF8);
sbSource = new StringBuilder(reader.ReadToEnd());
//4. Print out the XML of all projects for this account.
Console.WriteLine(sbSource.ToString());
}
}
}
catch (WebException wex)
{
if (wex.Response != null)
{
using (HttpWebResponse errorResponse = (HttpWebResponse)wex.Response)
{
Console.WriteLine(
"The server returned '{0}' with the status code {1} ({2:d}).",
errorResponse.StatusDescription, errorResponse.StatusCode,
errorResponse.StatusCode);
}
}
else
{
Console.WriteLine( wex);
}
}
finally
{
if (response != null) { response.Close(); }
}
}
}
我也很纠结于他们的API。斯科特的回答很有用。
无论如何,有一个非常有用和简单的库,称为EasyHttp巫婆,你可以在NuGet中找到。这里的方法与Scott的相同,但更短:):
public static string getProjects()
{
string uri = "https://<companyname>.harvestapp.com/projects";
HttpClient http = new HttpClient();
//Http Header
http.Request.Accept = HttpContentTypes.ApplicationJson;
http.Request.ContentType = HttpContentTypes.ApplicationJson;
http.Request.SetBasicAuthentication(username, password);
http.Request.ForceBasicAuth = true;
HttpResponse response = http.Get(uri);
return response.RawText;
}
如果你想了解更多关于WebApi调用的知识,你可以使用Fidler或一个更简单的工具,以及一个Firefox插件RestClient。
使用RestClient,你可以直接与rest服务器对话,如果你想了解RESTful服务,这是非常有用的。