Search code examples
c#httpauthorizationhttpwebrequest

Connecting to Tagpacker.com from C# with a HttpWebRequest


Does anyone know how I can post to the tagpacker website (with the following API in .Net? Because somehow I don't understand the authorization towards their server...

The code I'm currently using is the following:

private readonly HttpWebRequest _httpWebRequest = (HttpWebRequest)
WebRequest.Create("https://tagpacker.com/api/users/{YourUserID}/tags");


    public void Load()
    {
        _httpWebRequest.ContentType = "application/json";
        _httpWebRequest.Method = "POST";

        using (var streamWriter = new StreamWriter(_httpWebRequest.GetRequestStream()))
        {
            var json = "{\"user\":\"test\"," +
                          "\"password\":\"bla\"}";

            streamWriter.Write(json);
            streamWriter.Flush();
            streamWriter.Close();
        }

        var httpResponse = (HttpWebResponse)_httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();
        }
    }

But I'm getting back the following message:

{
   "status": 401,
   "message": "User not authorized",
   "code": "USER_NOT_AUTHORIZED",
   "url": "/unauthorizedHandler"
}

The only thing else you get from them is an "API-Access" code like the following: 1b3f314dd132b0015b5415b1:bd0ffe4b-a01e-4bf5-b1ed-12b24145d12d where the first part is your user id and the second is your API-code I guess? Does anyone have an idea on how to use this? Tagpacker's Api FAQs are here.


Solution

  • Official answer from the tagpacker team is that you need to set the api key in the header like the following (Java example):

    URL url = new URL("https://tagpacker.com/api/users/<your_user_id>/links?limit=20");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("api_key", "<your_api_key>");
    

    I will try to do this in C# and edit the answer again afterwards.