使用Clockify API进行身份验证?

时间:2019-04-11 21:30:07

标签: c# json api cloud clockify

我正在开发一个需要使用Clockify API的新应用程序。当我为概念证明进行测试应用程序时,我注意到我收到401错误,作为对使用其基本功能之一的回应,请按工作空间获取客户。我在身份验证方面缺少什么吗?我的个人资料上是否需要设置?我收到的错误是:System.Net.WebException:'远程服务器返回错误:(401)未经授权。谢谢。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace Api
{
    public class ApiHelper
    {
        public static HttpClient ApiClient { get; set; } = new HttpClient();

        public static void InitializeClient(string username, string password)
        {
            ApiClient = new HttpClient();
            ApiClient.BaseAddress = new Uri("https://api.clockify.me/api/");
            ApiClient.DefaultRequestHeaders.Accept.Clear();
            ApiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        }

        public static void GetClientsFromWorkspace(string workspace)
        {
            ApiClient.DefaultRequestHeaders.Add("X-Api-Key", "*********");
            var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.clockify.me/api/workspaces/" + workspace + "/clients");
            httpWebRequest.ContentType = "text/json";
            httpWebRequest.Method = "GET";
            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

        }
    }
}

1 个答案:

答案 0 :(得分:1)

您要在ApiClient上设置api密钥标头,然后使用新创建的HttpWebRequest进行请求,而新创建的ApiClient没有所需的api密钥标头。

您应该使用X-Api-Key发出请求,或将HttpWebRequest标头添加到httpWebRequest.Headers.Add(“X-Api-Key”, “********”) ,如下所示:

var arr = new Array();
function loadNextQues(flag) {
  var Selected = document.querySelectorAll('[name="op"]:checked');
  if (Selected != null) {
     Selected.forEach(function(radio) {
        arr.push(radio.value);
     });
  }
  console.log(arr);

  quesno++;
  if (flag == 1) {
    //add css to the button for review
  } else {
    //add css for the button as answered
  }
  url = "http://localhost/assignments/load-questions.php?qno=" + quesno;
  $.get(url, function(data, status) {
    response = JSON.parse(data);
    console.log(response);
    var quest = "<p>" + response.qno + " " + response.question + "</p>";
    quest += "<form>";
    quest +=
      '<input type="radio" name="op" value=\'' +
      response.opA +
      "'>A." +
      response.opA +
      "<br>";
    quest +=
      '<input type="radio" name="op" value=\'' +
      response.opB +
      "'>B." +
      response.opB +
      "<br>";
    quest +=
      '<input type="radio" name="op" value=\'' +
      response.opC +
      "'>C." +
      response.opC +
      "<br>";
    quest +=
      '<input type="radio" name="op" value=\'' +
      response.opD +
      "'>D." +
      response.opD +
      "<br>";
    quest += "</form>";
    document.getElementById("questBox").innerHTML = quest;
  });

  if (quesno == 15) {
    document.getElementById("mark").style.visibility = "hidden";
    document.getElementById("save").style.visibility = "hidden";
    document.getElementById("water").style.visibility = "visible";
  }
  var myArr = [];